Skip to content

Commit dc6e536

Browse files
author
jwildfire
authored
Merge pull request #211 from SafetyGraphics/dev-v0.10.0
v0.10.0 Dev branch
2 parents 7948468 + f998f39 commit dc6e536

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2328
-518
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: safetyGraphics
22
Title: Create Interactive Graphics Related to Clinical Trial Safety
3-
Version: 0.9.1
3+
Version: 0.10.0
44
Authors@R: c(
55
person("Jeremy", "Wildfire", email = "jeremy_wildfire@rhoworld.com", role = c("cre","aut")),
66
person("Becca", "Krouse", role="aut"),

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export(generateSettings)
77
export(getRequiredSettings)
88
export(getSettingsMetadata)
99
export(renderEDISH)
10+
export(renderSafetyHistogram)
1011
export(safetyGraphicsApp)
12+
export(safetyHistogram)
13+
export(safetyHistogramOutput)
1114
export(validateSettings)
1215
import(DT)
1316
import(dplyr)
@@ -16,6 +19,7 @@ import(rmarkdown)
1619
import(shinyjs)
1720
importFrom(dplyr,"filter")
1821
importFrom(dplyr,filter)
22+
importFrom(dplyr,full_join)
1923
importFrom(haven,read_sas)
2024
importFrom(magrittr,"%>%")
2125
importFrom(purrr,keep)

R/checkColumn.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@
1616
#' testSettings$filters[[1]]<-list(value_col="RACE",label="Race")
1717
#' testSettings$filters[[2]]<-list(value_col=NULL,label="No Column")
1818
#' testSettings$filters[[3]]<-list(value_col="NotAColumn",label="Invalid Column")
19-
#'
19+
#'
2020
#' #pass ($valid == TRUE)
2121
#' safetyGraphics:::checkColumn(key=list("id_col"),
22-
#' settings=testSettings, adlbc)
23-
#'
22+
#' settings=testSettings, adlbc)
23+
#'
2424
#' #pass
2525
#' safetyGraphics:::checkColumn(key=list("filters",1,"value_col"),
26-
#' settings=testSettings, adlbc)
27-
#'
26+
#' settings=testSettings, adlbc)
27+
#'
2828
#' #NULL column pass
2929
#' safetyGraphics:::checkColumn(key=list("filters",2,"value_col"),
30-
#' settings=testSettings, adlbc)
31-
#'
30+
#' settings=testSettings, adlbc)
31+
#'
3232
#' #invalid column fails
3333
#' safetyGraphics:::checkColumn(key=list("filters",3,"value_col"),
34-
#' settings=testSettings, adlbc)
34+
#' settings=testSettings, adlbc)
3535
#' @keywords internal
3636

3737
checkColumn <- function(key, settings, data){
3838
stopifnot(typeof(key)=="list",typeof(settings)=="list")
39-
39+
4040
current <- list(key=key)
4141
current$text_key <- paste( unlist(current$key), collapse='--')
4242
current$type <- "column"

R/checkNumeric.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#' @examples
1111
#' testSettings<-generateSettings(standard="AdAM")
1212
#' #pass ($valid == FALSE)
13-
#' safetyGraphics:::checkNumeric(key=list("id_col"),settings=testSettings, data=adlbc)
14-
#'
13+
#' safetyGraphics:::checkNumeric(key=list("id_col"),settings=testSettings, data=adlbc)
14+
#'
1515
#' #pass ($valid == TRUE)
16-
#' safetyGraphics:::checkNumeric(key=list("value_col"),settings=testSettings, data=adlbc)
17-
#'
16+
#' safetyGraphics:::checkNumeric(key=list("value_col"),settings=testSettings, data=adlbc)
17+
#'
1818
#' @keywords internal
1919

2020
checkNumeric <- function(key, settings, data){

R/data_checks.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#Statistical Checks

R/generateSettings.R

Lines changed: 106 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,138 @@
11
#' Generate a settings object based on a data standard
22
#'
3-
#' This function returns a settings object for the eDish chart based on the specified data standard.
3+
#' This function returns a settings object for the eDish chart based on the specified data standard.
44
#'
55
#' The function is designed to work with the SDTM and AdAM CDISC(<https://www.cdisc.org/>) standards for clinical trial data. Currently, eDish is the only chart supported.
66
#'
7-
#' @param standard The data standard for which to create settings. Valid options are "SDTM", "AdAM" or "None". Default: \code{"SDTM"}
8-
#' @param chart The chart for which standards should be generated ("eDish" only for now) . Default: \code{"eDish"}.
7+
#' @param standard The data standard for which to create settings. Valid options are "SDTM", "AdAM" or "None". Default: \code{"None"}.
8+
#' @param charts The chart or chart(s) for which settings should be generated. Default: \code{NULL} (uses all available charts).
9+
#' @param useDefaults Specifies whether default values from settingsMetadata should be included in the settings object. Default: \code{TRUE}.
910
#' @param partial Boolean for whether or not the standard is a partial standard. Default: \code{FALSE}.
1011
#' @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}.
12+
#' @param custom_settings a tibble with text_key and customValue columns specifiying customizations to be applied to the settings object. Default: \code{NULL}.
1113
#' @return A list containing the appropriate settings for the selected chart
12-
#'
13-
#' @examples
14-
#'
15-
#' generateSettings(standard="SDTM")
14+
#'
15+
#' @examples
16+
#'
17+
#' generateSettings(standard="SDTM")
1618
#' generateSettings(standard="SdTm") #also ok
1719
#' generateSettings(standard="ADaM")
1820
#' pkeys<- c("id_col","measure_col","value_col")
1921
#' generateSettings(standard="adam", partial=TRUE, partial_keys=pkeys)
20-
#'
21-
#' generateSettings(standard="a different standard")
22+
#'
23+
#' generateSettings(standard="a different standard")
2224
#' #returns shell settings list with no data mapping
23-
#'
25+
#'
2426
#' \dontrun{
25-
#' generateSettings(standard="adam",chart="AEExplorer") #Throws error. Only eDish supported so far.
27+
#' generateSettings(standard="adam",chart="AEExplorer") #Throws error. Only eDish supported so far.
2628
#' }
27-
#'
28-
#' @importFrom dplyr "filter"
29+
#'
30+
#' @importFrom dplyr "filter" full_join
2931
#' @importFrom stringr str_split
3032
#' @importFrom rlang .data
31-
#'
33+
#'
3234
#' @export
3335

34-
generateSettings <- function(standard="None", chart="eDish", partial=FALSE, partial_keys=NULL){
35-
if(tolower(chart)!="edish"){
36-
stop(paste0("Can't generate settings for the specified chart ('",chart,"'). Only the 'eDish' chart is supported for now."))
37-
}
38-
36+
generateSettings <- function(standard="None", charts=NULL, useDefaults=TRUE, partial=FALSE, partial_keys=NULL, custom_settings=NULL){
37+
3938
# Check that partial_keys is supplied if partial is true
4039
if (is.null(partial_keys) & partial ) {
4140
stop("partial_keys must be supplied if the standard is partial")
4241
}
43-
42+
4443
# Coerce options to lowercase
4544
standard<-tolower(standard)
46-
chart<-tolower(chart)
47-
45+
if(!is.null(charts)){
46+
charts<-tolower(charts)
47+
}
48+
49+
#############################################################################
50+
# get keys & default values for settings using a data standard (data and field mappings)
51+
############################################################################
4852
# Build a table of data mappings for the selected standard and partial settings
49-
standardList<-c("adam","sdtm") #TODO: automatically generate this from metadata
53+
standardList<-safetyGraphics::standardsMetadata%>%select(-.data$text_key)%>%names
54+
5055
if(standard %in% standardList){
51-
dataMappings <- safetyGraphics::getSettingsMetadata(
52-
charts = chart,
56+
dataDefaults <- safetyGraphics::getSettingsMetadata(
57+
charts = charts,
5358
cols=c("text_key",standard,"setting_required")
54-
) %>%
59+
) %>%
5560
filter(.data$setting_required)%>%
56-
rename("column_name" = standard)%>%
57-
filter(.data$column_name != '')
58-
59-
if(partial){
60-
dataMappings<-dataMappings%>%filter(.data$text_key %in% partial_keys)
61-
}
61+
select(-.data$setting_required)%>%
62+
rename("dataDefault" = standard)%>%
63+
filter(.data$dataDefault != '') %>%
64+
as_tibble
65+
}else{
66+
dataDefaults<-tibble(text_key=character(),dataDefault=character(), .rows=0)
6267
}
63-
64-
# build shell settings for each chart
65-
# TODO: move these to `/data` eventually
66-
shells<-list()
67-
shells[["edish"]]<-list(
68-
id_col = NULL,
69-
value_col = NULL,
70-
measure_col = NULL,
71-
normal_col_low = NULL,
72-
normal_col_high = NULL,
73-
studyday_col=NULL,
74-
visit_col = NULL,
75-
visitn_col = NULL,
76-
filters = NULL,
77-
group_cols = NULL,
78-
measure_values = list(ALT = NULL,
79-
AST = NULL,
80-
TB = NULL,
81-
ALP = NULL),
82-
baseline = list(value_col=NULL,
83-
values=list()),
84-
analysisFlag = list(value_col=NULL,
85-
values=list()),
86-
87-
x_options = c("ALT", "AST", "ALP"),
88-
y_options = c("TB", "ALP"),
89-
visit_window = 30,
90-
r_ratio_filter = TRUE,
91-
r_ratio_cut = 0,
92-
showTitle = TRUE,
93-
warningText = "Caution: This interactive graphic is not validated. Any clinical recommendations based on this tool should be confirmed using your organizations standard operating procedures."
94-
)
95-
96-
# loop through dataMappings and apply them to the shell
97-
if(standard %in% standardList){
98-
for(row in 1:nrow(dataMappings)){
99-
shells[[chart]]<-setSettingsValue(settings = shells[[chart]], key = textKeysToList(dataMappings[row,"text_key"])[[1]], value = dataMappings[row, "column_name"])
100-
}
68+
69+
if(partial){
70+
dataDefaults <-dataDefaults%>%filter(.data$text_key %in% partial_keys)
71+
}
72+
#############################################################################
73+
# get keys & default values for settings not using a data standard
74+
#############################################################################
75+
if(useDefaults){
76+
otherDefaults <- safetyGraphics::getSettingsMetadata(
77+
charts = charts,
78+
filter = !.data$column_mapping & !.data$field_mapping,
79+
cols=c("text_key","default")
80+
)%>%
81+
rename("otherDefault"="default")
82+
}else{
83+
otherDefaults <- tibble(text_key=character(),otherDefault=character(), .rows=0)
84+
}
85+
86+
#############################################################################
87+
# merge all keys & default values
88+
#############################################################################
89+
key_values <- full_join(dataDefaults, otherDefaults, by="text_key")
90+
key_values <- key_values %>% mutate(default=ifelse(is.na(.data$dataDefault),.data$otherDefault,.data$dataDefault))
91+
92+
#############################################################################
93+
# Apply custom settings (if any)
94+
#############################################################################
95+
if(!is.null(custom_settings)){
96+
key_values<-full_join(key_values, custom_settings, by="text_key")
97+
} else {
98+
key_values$customValue<-NA
99+
}
100+
101+
key_values<- key_values %>% mutate(value=ifelse(is.na(.data$customValue),.data$default,.data$customValue))
102+
103+
#############################################################################
104+
# create shell settings object
105+
#############################################################################
106+
shell<-generateShell(charts=charts)
107+
108+
#########################################################################################
109+
# populate the shell settings by looping through key_values and apply them to the shell
110+
#########################################################################################
111+
#print(key_values)
112+
for(row in 1:nrow(key_values)){
113+
text_key<-key_values[row,]%>%pull("text_key")
114+
key<- textKeysToList(text_key)[[1]]
115+
type <- safetyGraphics::getSettingsMetadata(text_keys=text_key,cols="setting_type")
116+
value <- key_values[row,"value"][[1]]
117+
finalValue <- value[[1]]
118+
119+
#print(paste(text_key," (",type,"):",toString(value),typeof(value),length(value),"->",finalValue,typeof(finalValue),length(finalValue)))
120+
shell<-setSettingsValue(
121+
settings = shell,
122+
key = key,
123+
value = finalValue
124+
)
125+
}
126+
127+
#Coerce empty string to NULL
128+
for (i in names(shell)){
129+
if (!is.null(shell[[i]])){
130+
if (shell[[i]][1]==""){
131+
shell[i] <- list(NULL)
132+
}
133+
}
101134
}
102135

103-
return(shells[[chart]])
104-
}
136+
#print(shell)
137+
return(shell)
138+
}

R/generateShell.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#' Generate a default settings shell based on settings metadata
2+
#'
3+
#' This function returns a default settings object based on the chart(s) specified.
4+
#'
5+
#' The function is designed to work with valid safetyGraphics charts.
6+
#'
7+
#' @param charts The chart or chart(s) to include in the shell settings object
8+
#' @return A list containing a setting shell (all values = NA) for the selected chart(s)
9+
#'
10+
#' @examples
11+
#'
12+
#' safetyGraphics:::generateShell(charts = "eDish")
13+
#'
14+
#' @keywords internal
15+
16+
generateShell <- function(charts=NULL){
17+
keys <- safetyGraphics::getSettingsMetadata(
18+
charts = charts,
19+
cols=c("text_key")
20+
) %>% textKeysToList()
21+
22+
shell <- list()
23+
24+
for (i in 1:length(keys) ) {
25+
shell<-setSettingsValue(
26+
key=keys[[i]],
27+
value=NULL,
28+
settings=shell,
29+
forceCreate=TRUE
30+
)
31+
}
32+
33+
return(shell)
34+
}

R/getChartSettings.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' Get chart-specific settings
2+
#'
3+
#' Subset master settings list to chart-specific settings list only
4+
#'
5+
#' @param settings Settings list containing settings for all selected charts.
6+
#' @param chart The chart for which settings should be returned.
7+
#'
8+
#' @return Chart-specific settings
9+
#'
10+
#' @examples
11+
#' settings <- safetyGraphics::generateSettings(standard="ADaM")
12+
#' safetyGraphics:::getChartSettings(settings = settings, chart = "edish")
13+
#'
14+
#' @keywords internal
15+
#'
16+
getChartSettings <- function(settings, chart){
17+
settings_names <- names(safetyGraphics::generateSettings("None",chart=chart))
18+
return(settings[settings_names])
19+
}

R/getSettingValue.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ getSettingValue <- function(key,settings){
2929
#If there are more keys and the value is a list, iterate
3030
if(typeof(value)=="list"){
3131
value<-getSettingValue(key[2:length(key)],value)
32+
#If position is provided and the value is a vector
33+
}else if(is.numeric(key[[2]]) & length(value)>=key[[2]] & length(key)==2){
34+
value<-value[[key[[2]]]]
3235
}else{
3336
#If there are more keys, but the value is not a list, return NULL
3437
value<-NULL

0 commit comments

Comments
 (0)