Skip to content

Commit 1765036

Browse files
authored
Merge pull request #46 from Merck/44-a-bug-in-prepare_boxly
Fix bug for multiple variables in observation
2 parents f585e7c + f48a7bd commit 1765036

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

R/meta_boxly.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@
4141
#' observation_term = "wk12"
4242
#' )
4343
meta_boxly <- function(
44-
dataset_adsl,
45-
dataset_param,
46-
population_term,
47-
population_subset = SAFFL == "Y",
48-
observation_term,
49-
observation_subset = SAFFL == "Y",
50-
parameters = unique(dataset_param$PARAMCD)) {
44+
dataset_adsl,
45+
dataset_param,
46+
population_term,
47+
population_subset = SAFFL == "Y",
48+
observation_term,
49+
observation_subset = SAFFL == "Y",
50+
parameters = unique(dataset_param$PARAMCD)
51+
) {
5152
# Input Checking
5253
require_param <- c("PARAM", "PARAMCD", "AVISITN", "CHG")
5354

R/prepare_boxly.R

Lines changed: 21 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,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
)

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)