diff --git a/R/generateSettings.R b/R/generateSettings.R index c10a4c70..ac5530d1 100644 --- a/R/generateSettings.R +++ b/R/generateSettings.R @@ -7,15 +7,16 @@ #' @param standard The data standard for which to create settings. Valid options are "SDTM", "AdAM" or "None". Default: \code{"SDTM"} #' @param chart The chart for which standards should be generated ("eDish" only for now) . Default: \code{"eDish"}. #' @param partial Boolean for whether or not the standard is a partial standard. Default: \code{FALSE}. -#' @param partial_cols Optional character vector of the matched cols if partial is TRUE. It will not be used if partial is FALSE Default: \code{NULL}. +#' @param partial_keys Optional character vector of the matched settings if partial is TRUE. Settings should be identified using the text_key format described in ?settingsMetadata. Setting is ignored when partial is FALSE. Default: \code{NULL}. #' @return A list containing the appropriate settings for the selected chart #' #' @examples #' #' generateSettings(standard="SDTM") #' generateSettings(standard="SdTm") #also ok -#' generateSettings(standard="SDTM", partial=TRUE, partial_cols = c("USUBJID","TEST","STRESN")) #' generateSettings(standard="ADaM") +#' pkeys<- c("id_col","measure_col","value_col") +#' generateSettings(standard="adam", partial=TRUE, partial_keys=pkeys) #' #' generateSettings(standard="a different standard") #' #returns shell settings list with no data mapping @@ -30,26 +31,40 @@ #' #' @export -generateSettings <- function(standard="None", chart="eDish", partial=FALSE, partial_cols=NULL){ +generateSettings <- function(standard="None", chart="eDish", partial=FALSE, partial_keys=NULL){ if(tolower(chart)!="edish"){ stop(paste0("Can't generate settings for the specified chart ('",chart,"'). Only the 'eDish' chart is supported for now.")) } - # Check that partial_cols is supplied if partial is true - if (is.null(partial_cols) & partial ) { - stop("partial_cols must be supplied if the standard is partial") + # Check that partial_keys is supplied if partial is true + if (is.null(partial_keys) & partial ) { + stop("partial_keys must be supplied if the standard is partial") } - metadata <- safetyGraphics::getSettingsMetadata( - charts = chart, - cols=c("text_key","adam","sdtm"), - filter_expr = .data$adam != '' & .data$sdtm != '' - ) - - # Split on -- for multi-level handling - hierarchical_metadata <- str_split(metadata$text_key, "--") + # Coerce options to lowercase + standard<-tolower(standard) + chart<-tolower(chart) + + # Build a table of data mappings for the selected standard and partial settings + standardList<-c("adam","sdtm") #TODO: automatically generate this from metadata + if(standard %in% standardList){ + dataMappings <- safetyGraphics::getSettingsMetadata( + charts = chart, + cols=c("text_key",standard,"setting_required") + ) %>% + filter(.data$setting_required)%>% + rename("column_name" = standard)%>% + filter(.data$column_name != '') + + if(partial){ + dataMappings<-dataMappings%>%filter(.data$text_key %in% partial_keys) + } + } - settings<-list( + # build shell settings for each chart + # TODO: move these to `/data` eventually + shells<-list() + shells[["edish"]]<-list( id_col = NULL, value_col = NULL, measure_col = NULL, @@ -69,7 +84,7 @@ generateSettings <- function(standard="None", chart="eDish", partial=FALSE, part analysisFlag = list(value_col=NULL, values=list()), - x_options = c("LT", "AST", "ALP"), + x_options = c("ALT", "AST", "ALP"), y_options = c("TB", "ALP"), visit_window = 30, r_ratio_filter = TRUE, @@ -78,42 +93,12 @@ generateSettings <- function(standard="None", chart="eDish", partial=FALSE, part warningText = "Caution: This interactive graphic is not validated. Any clinical recommendations based on this tool should be confirmed using your organizations standard operating procedures." ) - potential_settings <- settings - - standard_low <- tolower(standard) - - if (standard_low == "adam" | standard_low == "sdtm") { - - for (row in hierarchical_metadata) { - if (length(row) == 1) { - potential_settings[row] <- filter(metadata,.data$text_key == !!row)[[standard_low]] - } else if (length(row) == 2) { - potential_settings[row[[1]]][[1]][row[[2]]] <- filter(metadata, grepl(!!row[[2]],.data$text_key))[[standard_low]] - } else{ - stop("Three level setting nests are not currently supported") - } - - } - - } - - if(partial) { - - settings_names <- names(settings) - - potential_names <- names(potential_settings) - - for(i in 1:length(settings)) { - if (potential_settings[i] %in% partial_cols) { - settings[[which(settings_names == potential_names[i])]] <- potential_settings[[i]] - } - } - - } else { - - settings <- potential_settings - + # loop through dataMappings and apply them to the shell + if(standard %in% standardList){ + for(row in 1:nrow(dataMappings)){ + shells[[chart]]<-setSettingsValue(settings = shells[[chart]], key = textKeysToList(dataMappings[row,"text_key"])[[1]], value = dataMappings[row, "column_name"]) + } } - - return(settings) + + return(shells[[chart]]) } \ No newline at end of file diff --git a/R/setSettingsValue.R b/R/setSettingsValue.R new file mode 100644 index 00000000..8052d237 --- /dev/null +++ b/R/setSettingsValue.R @@ -0,0 +1,37 @@ +#' Set the value for a given named parameter +#' +#' Sets the value for a named parameter (\code{key}) to given \code{value} in a list (\code{settings}) +#' +#' @param key a list (like those provided by \code{getSettingKeys()}) defining the position of parameter in the settings object. +#' @param value the value to set +#' @param settings The settings list used to generate a chart like \code{eDISH()} +#' @return the updated settings object +#' +#' @examples +#' testSet<-list(a=list(b="myValue")) +#' safetyGraphics:::setSettingsValue(key=list("a","b"), value="notMyValue", settings=testSet) +#' #returns list(a=list(b="notMyValue"))) +#' +#' adamSettings<-generateSettings(standard="AdAM") +#' safetyGraphics:::setSettingsValue(list("id_col"),"customID",adamSettings) +#' safetyGraphics:::setSettingsValue(list("measure_values","ALP"),"Alanine Aminotrans",adamSettings) +#' safetyGraphics:::setSettingsValue(list("myCustomSetting"),"customized",adamSettings) +#' #adds myCustomSetting to adamSettings +#' +#' @keywords internal + + +setSettingsValue <- function(key, value, settings){ + stopifnot( + typeof(settings)=="list" + ) + + firstKey <- key[[1]] + if(length(key)==1){ + settings[[firstKey]]<-value + return(settings) + }else{ + settings[[firstKey]]<-setSettingsValue(settings = settings[[firstKey]],key = key[2:length(key)], value) + return(settings) + } +} \ No newline at end of file diff --git a/man/detectStandard.Rd b/man/detectStandard.Rd index 2d95265e..8536045e 100644 --- a/man/detectStandard.Rd +++ b/man/detectStandard.Rd @@ -23,7 +23,6 @@ This function attempts to detect the data CDISC clinical standard used in a give This function compares the columns in the provided \code{"data"} with the required columns for a given data standard/domain combination. The function is designed to work with the SDTM and AdAM CDISC() standards for clinical trial data. Currently, "labs" is the only domain supported. } \examples{ - detectStandard(adlbc)[["standard"]] #AdAM detectStandard(iris)[["standard"]] #none diff --git a/man/generateSettings.Rd b/man/generateSettings.Rd index 60223b46..f4bb5e9c 100644 --- a/man/generateSettings.Rd +++ b/man/generateSettings.Rd @@ -5,7 +5,7 @@ \title{Generate a settings object based on a data standard} \usage{ generateSettings(standard = "None", chart = "eDish", partial = FALSE, - partial_cols = NULL) + partial_keys = NULL) } \arguments{ \item{standard}{The data standard for which to create settings. Valid options are "SDTM", "AdAM" or "None". Default: \code{"SDTM"}} @@ -14,7 +14,7 @@ generateSettings(standard = "None", chart = "eDish", partial = FALSE, \item{partial}{Boolean for whether or not the standard is a partial standard. Default: \code{FALSE}.} -\item{partial_cols}{Optional character vector of the matched cols if partial is TRUE. It will not be used if partial is FALSE Default: \code{NULL}.} +\item{partial_keys}{Optional character vector of the matched settings if partial is TRUE. Settings should be identified using the text_key format described in ?settingsMetadata. Setting is ignored when partial is FALSE. Default: \code{NULL}.} } \value{ A list containing the appropriate settings for the selected chart @@ -29,8 +29,9 @@ The function is designed to work with the SDTM and AdAM CDISC(%keep(~!.x[["valid"]]) expect_false(fieldFailed[["valid"]])