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,23 @@ 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(
117+ " The filter variable '" , filter_var , " ' is not found in the observation data." ,
118+ " Please check the metadata for observation and `filter_var`."
119+ ))
115120 }
116121
122+ obs [, obs_var ] <- lapply(obs_var , function (var ) {
123+ x <- obs [[var ]]
124+ if (! is.factor(x )) {
125+ message(" In observation level data, the facet variable '" , var , " ' is automatically transformed into a factor." )
126+ factor (x , levels = sort(unique(x )))
127+ } else {
128+ x
129+ }
130+ })
131+
117132 if (! " factor" %in% class(obs [[x ]])) {
118133 message(" In observation level data, the group variable '" , x , " ' is automatically transformed into a factor." )
119134 obs [[x ]] <- factor (obs [[x ]], levels = sort(unique(obs [[x ]])))
@@ -163,8 +178,8 @@ prepare_boxly <- function(meta,
163178 ans
164179 }
165180 },
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 = " , " )),
181+ split(tbl , tbl [, c( obs_var , obs_group , x )] ),
182+ names(split(tbl , tbl [, c( obs_var , obs_group , x )] , sep = " , " )),
168183 SIMPLIFY = FALSE
169184 )
170185
@@ -198,7 +213,7 @@ prepare_boxly <- function(meta,
198213 # Return value
199214 metalite :: outdata(meta , population , observation , parameter ,
200215 x_var = x , y_var = y , group_var = obs_group ,
201- param_var = obs_var , hover_var_outlier = hover_var_outlier ,
216+ param_var = filter_var , hover_var_outlier = hover_var_outlier ,
202217 n = n_tbl , order = NULL , group = NULL , reference_group = NULL ,
203218 plotds = plotds
204219 )
0 commit comments