2525# ' The term name is used as key to link information.
2626# ' @param analysis A character value of analysis term name.
2727# ' The term name is used as key to link information.
28+ # ' @param filter_var A character value of variable name used for filtering.
29+ # ' Default is "PARAM".
2830# ' @param hover_var_outlier A character vector of hover variables for outlier.
2931# '
3032# ' @return Metadata list with plotting dataset.
@@ -48,6 +50,7 @@ prepare_boxly <- function(meta,
4850 population = NULL ,
4951 observation = NULL ,
5052 analysis = NULL ,
53+ filter_var = " PARAM" ,
5154 hover_var_outlier = c(" USUBJID" , metalite :: collect_adam_mapping(meta , analysis )$ y )) {
5255 if (is.null(population )) {
5356 if (length(meta $ population ) == 1 ) {
@@ -109,11 +112,21 @@ prepare_boxly <- function(meta,
109112 obs [[obs_group ]] <- factor (obs [[obs_group ]], levels = sort(unique(obs [[obs_group ]])))
110113 }
111114
112- if (! " factor " %in% class( obs [[ obs_var ]]) ) {
113- message( " In observation level data, the facet variable '" , obs_var , " ' is automatically transformed into a factor. " )
114- obs [[ obs_var ]] <- factor ( obs [[ obs_var ]], levels = sort(unique( obs [[ obs_var ]]) ))
115+ if (! filter_var %in% obs_var ) {
116+ stop(paste( " The filter variable '" , filter_var , " ' is not found in the observation data. " ,
117+ " Please check the metadata for observation and `filter_var`. " ))
115118 }
116119
120+ obs [, obs_var ] <- lapply(obs_var , function (var ) {
121+ x <- obs [[var ]]
122+ if (! is.factor(x )) {
123+ message(" In observation level data, the facet variable '" , var , " ' is automatically transformed into a factor." )
124+ factor (x , levels = sort(unique(x )))
125+ } else {
126+ x
127+ }
128+ })
129+
117130 if (! " factor" %in% class(obs [[x ]])) {
118131 message(" In observation level data, the group variable '" , x , " ' is automatically transformed into a factor." )
119132 obs [[x ]] <- factor (obs [[x ]], levels = sort(unique(obs [[x ]])))
@@ -163,8 +176,8 @@ prepare_boxly <- function(meta,
163176 ans
164177 }
165178 },
166- split(tbl , list ( tbl [[ obs_var ]], tbl [[ obs_group ]], tbl [[ x ]]) ),
167- names(split(tbl , list ( tbl [[ obs_var ]], tbl [[ obs_group ]], tbl [[ x ]]) , sep = " , " )),
179+ split(tbl , tbl [, c( obs_var , obs_group , x )] ),
180+ names(split(tbl , tbl [, c( obs_var , obs_group , x )] , sep = " , " )),
168181 SIMPLIFY = FALSE
169182 )
170183
@@ -198,7 +211,7 @@ prepare_boxly <- function(meta,
198211 # Return value
199212 metalite :: outdata(meta , population , observation , parameter ,
200213 x_var = x , y_var = y , group_var = obs_group ,
201- param_var = obs_var , hover_var_outlier = hover_var_outlier ,
214+ param_var = filter_var , hover_var_outlier = hover_var_outlier ,
202215 n = n_tbl , order = NULL , group = NULL , reference_group = NULL ,
203216 plotds = plotds
204217 )
0 commit comments