From f06973bd9145d147ba2dcd4f4f188b8a11e59b6d Mon Sep 17 00:00:00 2001 From: Jeremy Wildfire Date: Fri, 11 Mar 2022 16:37:45 -0500 Subject: [PATCH 1/2] add framework for rendering widgets --- NAMESPACE | 5 +++ R/hepExplorer.R | 74 ++++++++++++++++++++++++++++++++++++++++++++ R/render_widget.R | 33 ++++++++++++++++++++ man/hepExplorer.Rd | 49 +++++++++++++++++++++++++++++ man/render_widget.Rd | 18 +++++++++++ 5 files changed, 179 insertions(+) create mode 100644 R/hepExplorer.R create mode 100644 R/render_widget.R create mode 100644 man/hepExplorer.Rd create mode 100644 man/render_widget.Rd diff --git a/NAMESPACE b/NAMESPACE index cc000ba..65fe2a2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(QT_Outlier_Explorer) export(demogRTF_server) export(demogRTF_table) export(demogRTF_ui) +export(hepExplorer) export(init_aeExplorer) export(init_paneledOutlierExplorer) export(init_safetyOutlierExplorer) @@ -13,6 +14,7 @@ export(init_safetyResultsOverTime) export(init_safetyShiftPlot) export(lab_distribution_server) export(lab_distribution_ui) +export(render_widget) export(safetyOutlierExplorer_server) export(safetyOutlierExplorer_ui) export(safety_outlier_explorer) @@ -22,6 +24,7 @@ import(Tendril) import(Tplyr) import(dplyr) import(ggplot2) +import(htmlwidgets) import(rlang) import(shiny) importFrom(huxtable,as_hux) @@ -32,6 +35,7 @@ importFrom(huxtable,set_col_width) importFrom(huxtable,set_escape_contents) importFrom(huxtable,set_valign) importFrom(huxtable,set_width) +importFrom(jsonlite,toJSON) importFrom(pharmaRTF,add_footnotes) importFrom(pharmaRTF,add_titles) importFrom(pharmaRTF,hf_line) @@ -44,6 +48,7 @@ importFrom(plotly,layout) importFrom(plotly,plot_ly) importFrom(plotly,plotlyOutput) importFrom(plotly,renderPlotly) +importFrom(purrr,list_modify) importFrom(rlang,.data) importFrom(stringr,str_detect) importFrom(utils,hasName) diff --git a/R/hepExplorer.R b/R/hepExplorer.R new file mode 100644 index 0000000..8a7d1cd --- /dev/null +++ b/R/hepExplorer.R @@ -0,0 +1,74 @@ +#' Convience mapping of render_widget for hepExplorer +#' +#' @details The [data](https://github.com/SafetyGraphics/hep-explorer/wiki/Data-Guidelines) and [mapping](https://github.com/SafetyGraphics/hep-explorer/wiki/Configuration) should match the specs described in the [hepExplorer](https://github.com/SafetyGraphics/hep-explorer/wiki/Configuration) javascript library. Items passed in ... are added to mapping, and then the list is converted to json via `jsonlite::toJSON(mapping, auto_unbox=TRUE, null="null"). +#' +#' The default mapping - designed to work with safetyata::adam_adlbc is: +#' ``` +#' mapping <- list( +#' measure_col = "PARAM", +#' measure_values = list( +#' ALT = "Alanine Aminotransferase (U/L)", +#' AST = "Aspartate Aminotransferase (U/L)", +#' TB = "Bilirubin (umol/L)", +#' ALP = "Alkaline Phosphatase (U/L)" +#' ), +#' baseline_flag_col = "", +#' baseline_flag_values = "", +#' analysis_flag_col = "", +#' analysis_flag_values = "", +#' id_col = "USUBJID", +#' value_col = "AVAL", +#' normal_col_low = "A1LO", +#' normal_col_high = "A1HI", +#' studyday_col = "ADY", +#' visit_col = "VISIT", +#' visitn_col = "VISITNUM", +#' unit_col = "" +#' ) +#' ``` +#' +#' @examples +#' hepExplorer() #render widget with defaults +#' hepExplorer(group_cols=c("SEX","AGEGR1")) # Adding age group +#' +#' @param df data frame for hep-explorer +#' @param mapping named list with the current data mappings. See details for default mapping. +#' @param ... additional options to be added to mapping. Will overwrite mapping. +#' +#' @importFrom purrr list_modify +#' +#' @export + +hepExplorer <- function(df=safetyData::adam_adlbc, mapping=NULL, ...){ + #default mapping + if(is.null(mapping)){ + mapping <- list( + measure_col = "PARAM", + measure_values = list( + ALT = "Alanine Aminotransferase (U/L)", + AST = "Aspartate Aminotransferase (U/L)", + TB = "Bilirubin (umol/L)", + ALP = "Alkaline Phosphatase (U/L)" + ), + baseline_flag_col = "", + baseline_flag_values = "", + analysis_flag_col = "", + analysis_flag_values = "", + id_col = "USUBJID", + value_col = "AVAL", + normal_col_low = "A1LO", + normal_col_high = "A1HI", + studyday_col = "ADY", + visit_col = "VISIT", + visitn_col = "VISITNUM", + unit_col = "" + ) + } + + # add ... to mapping + if(length(list(...))>0){ + mapping <- purrr::list_modify(mapping, !!!list(...)) + } + + render_widget("hepExplorer" ,df, mapping) +} \ No newline at end of file diff --git a/R/render_widget.R b/R/render_widget.R new file mode 100644 index 0000000..6658e4d --- /dev/null +++ b/R/render_widget.R @@ -0,0 +1,33 @@ +#' Render an htmlwidget using standard safetyGraphics workflow +#' +#' @param widgetName name of the widget saved in safetyCharts +#' @param data named list of current data sets +#' @param mapping named list with the current data mappings +#' +#' @import htmlwidgets +#' @importFrom jsonlite toJSON +#' +#' @export +#' +render_widget <- function(widgetName, data, mapping){ + params<-list(data=data, settings=mapping) + + widgetParams <- list( + name=widgetName, + package='safetyCharts', + sizingPolicy = htmlwidgets::sizingPolicy(viewer.suppress=TRUE, browser.external = TRUE), + x=list() + ) + widgetParams$x$data <- params$data + widgetParams$x$rSettings <- params$settings + widgetParams$x$settings <- jsonlite::toJSON( + params$settings, + auto_unbox = TRUE, + null = "null" + ) + params <- widgetParams + + # Run the chart + do.call(htmlwidgets::createWidget, params) +} + diff --git a/man/hepExplorer.Rd b/man/hepExplorer.Rd new file mode 100644 index 0000000..d37122e --- /dev/null +++ b/man/hepExplorer.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hepExplorer.R +\name{hepExplorer} +\alias{hepExplorer} +\title{Convience mapping of render_widget for hepExplorer} +\usage{ +hepExplorer(df = safetyData::adam_adlbc, mapping = NULL, ...) +} +\arguments{ +\item{df}{data frame for hep-explorer} + +\item{mapping}{named list with the current data mappings. See details for default mapping.} + +\item{...}{additional options to be added to mapping. Will overwrite mapping.} +} +\description{ +Convience mapping of render_widget for hepExplorer +} +\details{ +The \href{https://github.com/SafetyGraphics/hep-explorer/wiki/Data-Guidelines}{data} and \href{https://github.com/SafetyGraphics/hep-explorer/wiki/Configuration}{mapping} should match the specs described in the \href{https://github.com/SafetyGraphics/hep-explorer/wiki/Configuration}{hepExplorer} javascript library. Items passed in ... are added to mapping, and then the list is converted to json via `jsonlite::toJSON(mapping, auto_unbox=TRUE, null="null"). + +The default mapping - designed to work with safetyata::adam_adlbc is:\preformatted{mapping <- list( + measure_col = "PARAM", + measure_values = list( + ALT = "Alanine Aminotransferase (U/L)", + AST = "Aspartate Aminotransferase (U/L)", + TB = "Bilirubin (umol/L)", + ALP = "Alkaline Phosphatase (U/L)" + ), + baseline_flag_col = "", + baseline_flag_values = "", + analysis_flag_col = "", + analysis_flag_values = "", + id_col = "USUBJID", + value_col = "AVAL", + normal_col_low = "A1LO", + normal_col_high = "A1HI", + studyday_col = "ADY", + visit_col = "VISIT", + visitn_col = "VISITNUM", + unit_col = "" + ) +} +} +\examples{ +hepExplorer() #render widget with defaults +hepExplorer(group_cols=c("SEX","AGEGR1")) # Adding age group + +} diff --git a/man/render_widget.Rd b/man/render_widget.Rd new file mode 100644 index 0000000..429b555 --- /dev/null +++ b/man/render_widget.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/render_widget.R +\name{render_widget} +\alias{render_widget} +\title{Render an htmlwidget using standard safetyGraphics workflow} +\usage{ +render_widget(widgetName, data, mapping) +} +\arguments{ +\item{widgetName}{name of the widget saved in safetyCharts} + +\item{data}{named list of current data sets} + +\item{mapping}{named list with the current data mappings} +} +\description{ +Render an htmlwidget using standard safetyGraphics workflow +} From af8c18729506fca5df158ab9e4e4109887d6c905 Mon Sep 17 00:00:00 2001 From: Jeremy Wildfire Date: Tue, 15 Mar 2022 10:53:39 -0400 Subject: [PATCH 2/2] update dependencies --- DESCRIPTION | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c7d146c..82ae53d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,19 +20,22 @@ Roxygen: list(markdown = TRUE) Imports: dplyr, DT, - Tplyr, + forcats, ggplot2, + htmlwidgets, + huxtable, + jsonlite, + kableExtra, + knitr, + pharmaRTF, plotly, + purrr, + RColorBrewer, rlang, shiny, - knitr, - RColorBrewer, stringr, - forcats, Tendril, - kableExtra, - huxtable, - pharmaRTF + Tplyr Suggests: testthat, shinytest,