|
| 1 | +#' @export |
| 2 | +tm_p_lineplot <- function(label = "Line Plot", |
| 3 | + plot_dataname, |
| 4 | + x_var, |
| 5 | + y_var, |
| 6 | + transformators = list()) { |
| 7 | + module( |
| 8 | + label = label, |
| 9 | + ui = ui_p_lineplot, |
| 10 | + server = srv_p_lineplot, |
| 11 | + ui_args = list(), |
| 12 | + server_args = list( |
| 13 | + plot_dataname = plot_dataname, |
| 14 | + x_var = x_var, |
| 15 | + y_var = y_var |
| 16 | + ), |
| 17 | + transformators = transformators |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +ui_p_lineplot <- function(id) { |
| 22 | + ns <- NS(id) |
| 23 | + bslib::page_fluid( |
| 24 | + tags$div( |
| 25 | + # trigger_tooltips_deps(), |
| 26 | + plotly::plotlyOutput(ns("plot"), height = "100%") |
| 27 | + ) |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +srv_p_lineplot <- function(id, data, plot_dataname, x_var, y_var) { |
| 32 | + moduleServer(id, function(input, output, session) { |
| 33 | + plotly_q <- reactive({ |
| 34 | + df <- data()[[plot_dataname]] |
| 35 | + |
| 36 | + validate(need(nrow(df) > 0, "No data after applying filters.")) |
| 37 | + |
| 38 | + # TODO: implement the high/low lines with annotations |
| 39 | + y_low_last <- if ("si_low" %in% names(df)) utils::tail(stats::na.omit(df[["si_low"]]), 1) else NA |
| 40 | + y_high_last <- if ("si_high" %in% names(df)) utils::tail(stats::na.omit(df[["si_high"]]), 1) else NA |
| 41 | + |
| 42 | + p <- plotly::plot_ly(data = df, x = df[[x_var]]) |> |
| 43 | + plotly::add_trace( |
| 44 | + y = df[[y_var]], |
| 45 | + mode = "lines+markers", type = "scatter", name = "Lab Result", |
| 46 | + line = list(color = "green"), |
| 47 | + marker = list(color = "green"), |
| 48 | + showlegend = FALSE |
| 49 | + ) |> |
| 50 | + # plotly::add_trace( |
| 51 | + # y = df[["si_low"]], |
| 52 | + # mode = "lines", |
| 53 | + # line = list(color = "red", dash = "dash"), |
| 54 | + # showlegend = FALSE |
| 55 | + # ) |> |
| 56 | + # plotly::add_annotations( |
| 57 | + # x = max(df[[x_var]], na.rm = TRUE), |
| 58 | + # y = y_low_last, |
| 59 | + # yshift = 15, |
| 60 | + # text = "Original LLN", |
| 61 | + # showarrow = FALSE |
| 62 | + # ) |> |
| 63 | + # plotly::add_trace( |
| 64 | + # y = df[["si_high"]], |
| 65 | + # mode = "lines", |
| 66 | + # line = list(color = "red", dash = "solid"), |
| 67 | + # showlegend = FALSE |
| 68 | + # ) |> |
| 69 | + # plotly::add_annotations( |
| 70 | + # x = max(df[[x_var]], na.rm = TRUE), |
| 71 | + # y = y_high_last, |
| 72 | + # yshift = -15, |
| 73 | + # text = "Original ULN", |
| 74 | + # showarrow = FALSE |
| 75 | + # ) |> |
| 76 | + plotly::layout( |
| 77 | + xaxis = list(title = "Study Day of Sample Collection", zeroline = FALSE), |
| 78 | + yaxis = list(title = "Original Result") |
| 79 | + ) |
| 80 | + |
| 81 | + p |
| 82 | + }) |
| 83 | + |
| 84 | + |
| 85 | + output$plot <- plotly::renderPlotly({ |
| 86 | + p <- plotly_q() |
| 87 | + plotly::event_register(p, "plotly_selected") |
| 88 | + p |
| 89 | + }) |
| 90 | + }) |
| 91 | +} |
0 commit comments