Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions R/meta_boxly.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@
#' observation_term = "wk12"
#' )
meta_boxly <- function(
dataset_adsl,
dataset_param,
population_term,
population_subset = SAFFL == "Y",
observation_term,
observation_subset = SAFFL == "Y",
parameters = unique(dataset_param$PARAMCD)) {
dataset_adsl,
dataset_param,
population_term,
population_subset = SAFFL == "Y",
observation_term,
observation_subset = SAFFL == "Y",
parameters = unique(dataset_param$PARAMCD)
) {
# Input Checking
require_param <- c("PARAM", "PARAMCD", "AVISITN", "CHG")

Expand Down
27 changes: 21 additions & 6 deletions R/prepare_boxly.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#' The term name is used as key to link information.
#' @param analysis A character value of analysis term name.
#' The term name is used as key to link information.
#' @param filter_var A character value of variable name used for filtering.
#' Default is "PARAM".
#' @param hover_var_outlier A character vector of hover variables for outlier.
#'
#' @return Metadata list with plotting dataset.
Expand All @@ -48,6 +50,7 @@ prepare_boxly <- function(meta,
population = NULL,
observation = NULL,
analysis = NULL,
filter_var = "PARAM",
hover_var_outlier = c("USUBJID", metalite::collect_adam_mapping(meta, analysis)$y)) {
if (is.null(population)) {
if (length(meta$population) == 1) {
Expand Down Expand Up @@ -109,11 +112,23 @@ prepare_boxly <- function(meta,
obs[[obs_group]] <- factor(obs[[obs_group]], levels = sort(unique(obs[[obs_group]])))
}

if (!"factor" %in% class(obs[[obs_var]])) {
message("In observation level data, the facet variable '", obs_var, "' is automatically transformed into a factor.")
obs[[obs_var]] <- factor(obs[[obs_var]], levels = sort(unique(obs[[obs_var]])))
if (!filter_var %in% obs_var) {
stop(paste(
"The filter variable '", filter_var, "' is not found in the observation data.",
Comment on lines +116 to +117
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses inconsistent spacing in the paste() function, resulting in extra spaces around the filter variable name in the output. Remove the trailing space after the filter_var placeholder:

stop(paste(
  "The filter variable '", filter_var, "' is not found in the observation data.",
  "Please check the metadata for observation and `filter_var`."
))

Should be:

stop(paste0(
  "The filter variable '", filter_var, "' is not found in the observation data. ",
  "Please check the metadata for observation and `filter_var`."
))

Using paste0() gives better control over spacing.

Suggested change
stop(paste(
"The filter variable '", filter_var, "' is not found in the observation data.",
stop(paste0(
"The filter variable '", filter_var, "' is not found in the observation data. ",

Copilot uses AI. Check for mistakes.
"Please check the metadata for observation and `filter_var`."
))
}

obs[, obs_var] <- lapply(obs_var, function(var) {
x <- obs[[var]]
if (!is.factor(x)) {
message("In observation level data, the facet variable '", var, "' is automatically transformed into a factor.")
factor(x, levels = sort(unique(x)))
} else {
x
}
})
Comment on lines +122 to +130
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignment obs[, obs_var] <- lapply(...) will create a list column in the data frame instead of properly updating the individual columns. This is because lapply() returns a list, and assigning a list to multiple columns creates a single list-column.

To fix this, use a loop to assign each column individually:

for (var in obs_var) {
  x <- obs[[var]]
  if (!is.factor(x)) {
    message("In observation level data, the facet variable '", var, "' is automatically transformed into a factor.")
    obs[[var]] <- factor(x, levels = sort(unique(x)))
  }
}

Alternatively, use obs[obs_var] <- lapply(...) (without the comma) to properly assign the list elements to respective columns.

Suggested change
obs[, obs_var] <- lapply(obs_var, function(var) {
x <- obs[[var]]
if (!is.factor(x)) {
message("In observation level data, the facet variable '", var, "' is automatically transformed into a factor.")
factor(x, levels = sort(unique(x)))
} else {
x
}
})
for (var in obs_var) {
x <- obs[[var]]
if (!is.factor(x)) {
message("In observation level data, the facet variable '", var, "' is automatically transformed into a factor.")
obs[[var]] <- factor(x, levels = sort(unique(x)))
}
}

Copilot uses AI. Check for mistakes.

if (!"factor" %in% class(obs[[x]])) {
message("In observation level data, the group variable '", x, "' is automatically transformed into a factor.")
obs[[x]] <- factor(obs[[x]], levels = sort(unique(obs[[x]])))
Expand Down Expand Up @@ -163,8 +178,8 @@ prepare_boxly <- function(meta,
ans
}
},
split(tbl, list(tbl[[obs_var]], tbl[[obs_group]], tbl[[x]])),
names(split(tbl, list(tbl[[obs_var]], tbl[[obs_group]], tbl[[x]]), sep = ", ")),
split(tbl, tbl[, c(obs_var, obs_group, x)]),
names(split(tbl, tbl[, c(obs_var, obs_group, x)], sep = ", ")),
SIMPLIFY = FALSE
)

Expand Down Expand Up @@ -198,7 +213,7 @@ prepare_boxly <- function(meta,
# Return value
metalite::outdata(meta, population, observation, parameter,
x_var = x, y_var = y, group_var = obs_group,
param_var = obs_var, hover_var_outlier = hover_var_outlier,
param_var = filter_var, hover_var_outlier = hover_var_outlier,
n = n_tbl, order = NULL, group = NULL, reference_group = NULL,
plotds = plotds
)
Expand Down
4 changes: 4 additions & 0 deletions man/prepare_boxly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.