Skip to content

Commit ab59560

Browse files
committed
Fix bug for multiple variables in observation
1 parent f585e7c commit ab59560

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

R/prepare_boxly.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
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
)

man/prepare_boxly.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)