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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ Encoding: UTF-8
Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
30 changes: 30 additions & 0 deletions R/call_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions R/data_extract_filter_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -106,7 +106,6 @@ data_extract_filter_srv <- function(id, datasets, filter) {
} else {
choices[1]
}

} else {
choices <- character(0)
selected <- character(0)
Expand Down
6 changes: 6 additions & 0 deletions R/merge_datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions man/call_uses_magrittr_pipe.Rd

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

1 change: 1 addition & 0 deletions tests/testthat/test-dplyr_call_examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-merge_expression_srv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading