diff --git a/DESCRIPTION b/DESCRIPTION index 6d5247c2..f1f60a1d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -58,4 +58,4 @@ Encoding: UTF-8 Language: en-US LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 diff --git a/R/call_utils.R b/R/call_utils.R index 8811c04a..9731e96e 100644 --- a/R/call_utils.R +++ b/R/call_utils.R @@ -374,3 +374,33 @@ calls_combine_by <- function(operator, calls) { f = function(x, y) call(operator, x, y) ) } + +#' Check if a call or list of calls uses the pipe operator (%>%) +#' +#' Recursively searches through a call or list of calls to determine +#' if the pipe operator `%>%` is used anywhere. +#' +#' @param x (`call`, `name`, or `list` of calls) The call(s) to check. +#' +#' @return `logical(1)` `TRUE` if `%>%` is found, `FALSE` otherwise. +#' +#' @keywords internal +#' +call_uses_magrittr_pipe <- function(x) { + if (is.call(x)) { + # Check if there is the pipe operator + # Handle both quote(`%>%`) and as.name("%>%") cases + return(any(grepl("%>%", x, fixed = TRUE))) + } + + if (is.list(x) && length(x)) { + # Check all elements in the list + for (call_x in x) { + if (Recall(call_x)) { + return(TRUE) + } + } + } + + FALSE +} diff --git a/R/data_extract_filter_module.R b/R/data_extract_filter_module.R index d87a6d85..00bcea1c 100644 --- a/R/data_extract_filter_module.R +++ b/R/data_extract_filter_module.R @@ -70,7 +70,7 @@ data_extract_filter_srv <- function(id, datasets, filter) { # when the filter is initialized with a delayed spec, the choices and selected are NULL # here delayed are resolved and the values are set up # Begin by resolving delayed choices. - if (inherits(filter$selected, "delayed_choices")) { + if (inherits(filter$selected, "delayed_choices")) { filter$selected <- filter$selected(filter$choices) } teal.widgets::updateOptionalSelectInput( @@ -106,7 +106,6 @@ data_extract_filter_srv <- function(id, datasets, filter) { } else { choices[1] } - } else { choices <- character(0) selected <- character(0) diff --git a/R/merge_datasets.R b/R/merge_datasets.R index d19e1974..2b4752a5 100644 --- a/R/merge_datasets.R +++ b/R/merge_datasets.R @@ -154,6 +154,12 @@ merge_datasets <- function(selector_list, datasets, join_keys, merge_function = all_calls_expression <- c(dplyr_calls, anl_merge_calls, anl_relabel_call) + # Ensure dplyr is loaded if any of the generated calls use the pipe operator + if (call_uses_magrittr_pipe(all_calls_expression)) { + library_dplyr_call <- call("library", as.name("magrittr")) + all_calls_expression <- c(list(library_dplyr_call), all_calls_expression) + } + # keys in each merged_selector_list element should be identical # so take first one keys <- merged_selector_list[[1]]$keys diff --git a/man/call_uses_magrittr_pipe.Rd b/man/call_uses_magrittr_pipe.Rd new file mode 100644 index 00000000..33b1b813 --- /dev/null +++ b/man/call_uses_magrittr_pipe.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/call_utils.R +\name{call_uses_magrittr_pipe} +\alias{call_uses_magrittr_pipe} +\title{Check if a call or list of calls uses the pipe operator (\%>\%)} +\usage{ +call_uses_magrittr_pipe(x) +} +\arguments{ +\item{x}{(\code{call}, \code{name}, or \code{list} of calls) The call(s) to check.} +} +\value{ +\code{logical(1)} \code{TRUE} if \verb{\%>\%} is found, \code{FALSE} otherwise. +} +\description{ +Recursively searches through a call or list of calls to determine +if the pipe operator \verb{\%>\%} is used anywhere. +} +\keyword{internal} diff --git a/tests/testthat/test-dplyr_call_examples.R b/tests/testthat/test-dplyr_call_examples.R index be040516..cca037f8 100644 --- a/tests/testthat/test-dplyr_call_examples.R +++ b/tests/testthat/test-dplyr_call_examples.R @@ -3372,6 +3372,7 @@ testthat::test_that("Universal example", { paste(merged_datasets$expr), paste( c( + "library(magrittr)", "ANL_1 <- X %>% dplyr::select(A, B, D, E)", "ANL_2 <- Y %>% dplyr::select(A, B, C, G) %>% dplyr::rename(y.G = G)", "ANL_3 <- Z %>% dplyr::select(D, C, F, G) %>% dplyr::rename(z.G = G)", diff --git a/tests/testthat/test-merge_expression_srv.R b/tests/testthat/test-merge_expression_srv.R index 3d8101ae..caa3f3b1 100644 --- a/tests/testthat/test-merge_expression_srv.R +++ b/tests/testthat/test-merge_expression_srv.R @@ -208,9 +208,10 @@ testthat::test_that("merge_expression_srv returns merge expression when passing args = list(selector_list = selector_list, datasets = data_list, join_keys = join_keys), expr = { testthat::expect_true(inherits(session$returned()$expr, "list")) - testthat::expect_true(inherits(session$returned()$expr[[1]], "<-")) + testthat::expect_true(inherits(session$returned()$expr[[2]], "<-")) testthat::expect_identical( c( + "library(magrittr)", "ANL_1 <- ADSL %>% dplyr::select(STUDYID, USUBJID, AGE)", "ANL_2 <- ADLB %>% dplyr::select(STUDYID, USUBJID, AVAL, CHG)", "ANL <- ANL_1",