|
| 1 | +#' Create an interactive graphics widget |
| 2 | +#' |
| 3 | +#' This function creates an nice interactive widget. See this vingette for more details regarding how to customize charts. |
| 4 | +#' |
| 5 | +#' @param data A data frame containing the labs data. Data must be structured as one record per study participant per time point per lab measure. |
| 6 | +#' @param debug_js print settings in javascript before rendering chart. Default: \code{FALSE}. |
| 7 | +#' @param settings Optional list of settings arguments to be converted to JSON using \code{jsonlite::toJSON(settings, auto_unbox = TRUE, dataframe = "rows", null = "null")}. Default: \code{NULL}. |
| 8 | +#' @param chart name of the chart to render |
| 9 | +#' |
| 10 | +#' @examples |
| 11 | +#' \dontrun{ |
| 12 | +#' |
| 13 | +#' ## Create Histogram figure using a premade settings list |
| 14 | +#' details_list <- list( |
| 15 | +#' list(value_col = "TRTP", label = "Treatment"), |
| 16 | +#' list(value_col = "SEX", label = "Sex"), |
| 17 | +#' list(value_col = "AGEGR1", label = "Age group") |
| 18 | +#' ) |
| 19 | +#' |
| 20 | +#' |
| 21 | +#' filters_list <- list( |
| 22 | +#' list(value_col = "TRTA", label = "Treatment"), |
| 23 | +#' list(value_col = "SEX", label = "Sex"), |
| 24 | +#' list(value_col = "RACE", label = "RACE"), |
| 25 | +#' list(value_col = "AGEGR1", label = "Age group") |
| 26 | +#' ) |
| 27 | +#' |
| 28 | +#' settingsl <- list(id_col = "USUBJID", |
| 29 | +#' value_col = "AVAL", |
| 30 | +#' measure_col = "PARAM", |
| 31 | +#' unit_col = "PARAMCD", |
| 32 | +#' normal_col_low = "A1LO", |
| 33 | +#' normal_col_high = "A1HI", |
| 34 | +#' details = details_list, |
| 35 | +#' filters = filters_list) |
| 36 | +#' |
| 37 | +#' chartRenderer(data=adlbc, settings = settingsl, chart=safetyhistogram) |
| 38 | +#' |
| 39 | +#' } |
| 40 | +#' |
| 41 | +#' @import htmlwidgets |
| 42 | +#' |
| 43 | +#' @export |
| 44 | +chartRenderer <- function(data, debug_js = FALSE, settings = NULL, chart=NULL) { |
| 45 | + # Chart specific customizastions (to be removed after js updates) |
| 46 | + if(chart %in% c("paneledoutlierexplorer","safetyoutlierexplorer")){ |
| 47 | + settings$time_cols <- list(list(),list()); |
| 48 | + settings$time_cols[[1]]<-list( |
| 49 | + type= "ordinal", |
| 50 | + value_col= settings[["visit_col"]], |
| 51 | + label= "Visit", |
| 52 | + order_col= settings[["visitn_col"]], |
| 53 | + order= NULL, |
| 54 | + rotate_tick_labels= TRUE, |
| 55 | + vertical_space= 100 |
| 56 | + ) |
| 57 | + settings$time_cols[[2]]<-list( |
| 58 | + type= "linear", |
| 59 | + value_col= settings[["studyday_col"]], |
| 60 | + label= "Study Day", |
| 61 | + order_col= settings[["studyday_col"]], |
| 62 | + order= NULL, |
| 63 | + rotate_tick_labels= FALSE, |
| 64 | + vertical_space= 0 |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + if(chart=="paneledoutlierexplorer"){ |
| 69 | + settings$lln_col <- settings[["normal_col_low"]] |
| 70 | + settings$uln_col <- settings[["normal_col_high"]] |
| 71 | + } |
| 72 | + |
| 73 | + if(chart=="safetyshiftplot"){ |
| 74 | + settings$time_col<-settings[["visit_col"]] |
| 75 | + } |
| 76 | + |
| 77 | + if(chart=="safetyresultsovertime"){ |
| 78 | + settings$time_settings=list( |
| 79 | + value_col= settings[["visit_col"]], |
| 80 | + label= "Visit", |
| 81 | + order_col= settings[["visitn_col"]], |
| 82 | + order= NULL, |
| 83 | + rotate_tick_labels= TRUE, |
| 84 | + vertical_space= 100 |
| 85 | + ) |
| 86 | + |
| 87 | + settings$groups = settings$group_cols |
| 88 | + } |
| 89 | + |
| 90 | + #Set Chart Width |
| 91 | + chartMaxWidth<- safetyGraphics::chartsMetadata %>% filter(.data$chart==!!chart) %>% pull(.data$maxWidth) |
| 92 | + settings$max_width <- chartMaxWidth |
| 93 | + |
| 94 | + #Renderer |
| 95 | + chartFunction<- safetyGraphics::chartsMetadata %>% filter(.data$chart==!!chart) %>% pull(.data$main) |
| 96 | + rSettings = list( |
| 97 | + data = data, |
| 98 | + debug_js=debug_js, |
| 99 | + chartFunction = chartFunction, |
| 100 | + settings = jsonlite::toJSON( |
| 101 | + settings, |
| 102 | + auto_unbox = TRUE, |
| 103 | + null = "null" |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + # create widget |
| 108 | + htmlwidgets::createWidget( |
| 109 | + name = 'chartRenderer', |
| 110 | + rSettings, |
| 111 | + # width = width, |
| 112 | + # height = height, |
| 113 | + package = 'safetyGraphics', |
| 114 | + sizingPolicy = htmlwidgets::sizingPolicy(viewer.suppress=TRUE, browser.external = TRUE) |
| 115 | + ) |
| 116 | +} |
| 117 | + |
| 118 | +#' Shiny bindings for chartRenderer |
| 119 | +#' |
| 120 | +#' Output and render functions for using safetyhistogram within Shiny |
| 121 | +#' applications and interactive Rmd documents. |
| 122 | +#' |
| 123 | +#' @param outputId output variable to read from |
| 124 | +#' @param width,height Must be a valid CSS unit (like \code{'100\%'}, |
| 125 | +#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a |
| 126 | +#' string and have \code{'px'} appended. |
| 127 | +#' @param expr An expression that generates a chart |
| 128 | +#' @param env The environment in which to evaluate \code{expr}. |
| 129 | +#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This |
| 130 | +#' is useful if you want to save an expression in a variable. |
| 131 | +#' |
| 132 | +#' @name chartRenderer-shiny |
| 133 | +#' |
| 134 | +#' @export |
| 135 | +output_chartRenderer <- function(outputId, width = '100%', height = '400px'){ |
| 136 | + htmlwidgets::shinyWidgetOutput(outputId, 'chartRenderer', width, height, package = 'safetyGraphics') |
| 137 | +} |
| 138 | + |
| 139 | +#' @rdname chartRenderer-shiny |
| 140 | +#' @export |
| 141 | +render_chartRenderer <- function(expr, env = parent.frame(), quoted = FALSE) { |
| 142 | + if (!quoted) { expr <- substitute(expr) } # force quoted |
| 143 | + htmlwidgets::shinyRenderWidget(expr, output_chartRenderer, env, quoted = TRUE) |
| 144 | +} |
0 commit comments