From 147eef3d3235d8454ac20b31a571a61fba27a255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Fri, 25 Oct 2024 09:15:39 +0100 Subject: [PATCH] Allow non-standard datanames in teal .raw_data (#1382) wip # Pull Request Fixes #1366 ### Related: - https://github.com/insightsengineering/teal/pull/1382 - https://github.com/insightsengineering/teal.slice/pull/622 - https://github.com/insightsengineering/teal.data/pull/340 ### Changes description - `.raw_data` is created and supports non-standard R names, such as: - `%pipe%` - `assigns<-` - ... --- R/module_filter_data.R | 2 +- tests/testthat/test-module_teal.R | 105 ++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/R/module_filter_data.R b/R/module_filter_data.R index cb60a08d5..efe4a53bd 100644 --- a/R/module_filter_data.R +++ b/R/module_filter_data.R @@ -56,7 +56,7 @@ srv_filter_data <- function(id, datasets, active_datanames, data_rv, is_active) data, paste0( ".raw_data <- list2env(list(", - toString(sprintf("%1$s = %1$s", datanames)), + toString(sprintf("%1$s = %1$s", sapply(datanames, as.name))), "))\n", "lockEnvironment(.raw_data) #@linksto .raw_data" # this is environment and it is shared by qenvs. CAN'T MODIFY! ) diff --git a/tests/testthat/test-module_teal.R b/tests/testthat/test-module_teal.R index e18c975e1..b8878e566 100644 --- a/tests/testthat/test-module_teal.R +++ b/tests/testthat/test-module_teal.R @@ -2216,3 +2216,108 @@ testthat::describe("srv_teal snapshot manager", { ) }) }) + +testthat::describe("Datanames with special symbols", { + testthat::it("are detected as datanames when defined as 'all'", { + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = teal.data::teal_data( + iris = iris, + `%a_pipe%` = function(lhs, rhs) paste(lhs, rhs) + ), + modules = modules(module("module_1", server = function(id, data) data)), + filter = teal_slices( + module_specific = TRUE + ) + ), + expr = { + session$setInputs("teal_modules-active_tab" = "module_1") + session$flushReact() + + testthat::expect_setequal( + ls( + teal.code::get_env(modules_output$module_1()()), + all.names = TRUE + ), + c(".raw_data", "iris", "%a_pipe%") + ) + } + ) + }) + + testthat::it("are present in datanames when used in pre-processing code", { + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = within( + teal.data::teal_data(), + { + iris <- iris + mtcars <- mtcars + `_a variable with spaces_` <- "new_column" # nolint: object_name. + iris <- cbind(iris, data.frame(`_a variable with spaces_`)) + } + ), + modules = modules( + module("module_1", server = function(id, data) data, datanames = c("iris", "_a variable with spaces_")) + ) + ), + expr = { + session$setInputs("teal_modules-active_tab" = "module_1") + session$flushReact() + testthat::expect_setequal( + ls( + teal.code::get_env(modules_output$module_1()()), + all.names = TRUE + ), + c(".raw_data", "iris", "_a variable with spaces_") + ) + } + ) + }) + + testthat::it("(when used as non-native pipe) are present in datanames in the pre-processing code", { + testthat::expect_warning( + shiny::testServer( + app = srv_teal, + args = list( + id = "test", + data = within( + teal.data::teal_data(), + { + iris <- iris + mtcars <- mtcars + `%cbind%` <- function(lhs, rhs) cbind(lhs, rhs) + iris <- iris %cbind% data.frame("new_column") + } + ), + modules = modules( + module("module_1", server = function(id, data) data, , datanames = c("iris")) + ), + filter = teal_slices( + module_specific = TRUE + ) + ), + expr = { + session$setInputs("teal_modules-active_tab" = "module_1") + session$flushReact() + + testthat::expect_contains( + strsplit( + x = teal.code::get_code(modules_output$module_1()()), + split = "\n" + )[[1]], + c( + "`%cbind%` <- function(lhs, rhs) cbind(lhs, rhs)", + ".raw_data <- list2env(list(iris = iris))" + ) + ) + } + ), + "'package:teal' may not be available when loading" + ) + }) +})