Skip to content

Commit

Permalink
Merge branch 'main' into 211_subset@main
Browse files Browse the repository at this point in the history
  • Loading branch information
m7pr authored Oct 28, 2024
2 parents bdcb768 + da3be66 commit 1bd270d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 40 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: teal
Title: Exploratory Web Apps for Analyzing Clinical Trials Data
Version: 0.15.2.9076
Date: 2024-10-25
Version: 0.15.2.9077
Date: 2024-10-28
Authors@R: c(
person("Dawid", "Kaledkowski", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9533-457X")),
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# teal 0.15.2.9076
# teal 0.15.2.9077

### New features

Expand Down
13 changes: 10 additions & 3 deletions R/module_init_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,18 @@ srv_init_data <- function(id, data) {
vapply(
datanames,
function(dataname, datasets) {
hash <- rlang::hash(data[[dataname]])
x <- data[[dataname]]

code <- if (is.function(x) && !is.primitive(x)) {
x <- deparse1(x)
bquote(rlang::hash(deparse1(.(as.name(dataname)))))
} else {
bquote(rlang::hash(.(as.name(dataname))))
}
sprintf(
"stopifnot(%s == %s) # @linksto %s",
deparse1(bquote(rlang::hash(.(as.name(dataname))))),
deparse1(hash),
deparse1(code),
deparse1(rlang::hash(x)),
dataname
)
},
Expand Down
155 changes: 121 additions & 34 deletions tests/testthat/test-module_teal.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ testthat::describe("srv_teal lockfile", {
"creation process is invoked for teal.lockfile.mode = \"enabled\" ",
"and snapshot is copied to teal_app.lock and removed after session ended"
), {
testthat::skip_if_not_installed("mirai")
testthat::skip_if_not_installed("renv")
withr::with_options(
list(teal.lockfile.mode = "enabled"),
{
Expand All @@ -95,6 +97,8 @@ testthat::describe("srv_teal lockfile", {
)
})
testthat::it("creation process is not invoked for teal.lockfile.mode = \"disabled\"", {
testthat::skip_if_not_installed("mirai")
testthat::skip_if_not_installed("renv")
withr::with_options(
list(teal.lockfile.mode = "disabled"),
{
Expand Down Expand Up @@ -2280,44 +2284,127 @@ testthat::describe("Datanames with special symbols", {
})

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
)
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")
}
),
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()
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))"
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))"
)
)
}
)
})
})

testthat::describe("teal.data code with a function defined", {
testthat::it("is fully reproducible", {
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = reactive(within(teal.data::teal_data(), {
fun <- function(x) {
y <- x + 1
y + 3
}
})),
modules = modules(module("module_1", server = function(id, data) data))
), ,
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()

# Need to evaluate characters to preserve indentation
local_env <- new.env(parent = .GlobalEnv)
dat <- modules_output$module_1()()

eval(
parse(text = teal.code::get_code(dat)),
envir = local_env
)

testthat::expect_identical(local_env$fun(1), 5)
testthat::expect_identical(local_env$fun(1), dat[["fun"]](1))
}
)
})

testthat::it("has the correct code (with hash)", {
shiny::testServer(
app = srv_teal,
args = list(
id = "test",
data = reactive(within(teal.data::teal_data(), {
fun <- function(x) {
y <- x + 1
y + 3
}
})),
modules = modules(module("module_1", server = function(id, data) data))
), ,
expr = {
session$setInputs("teal_modules-active_tab" = "module_1")
session$flushReact()

# Need to evaluate characters to preserve indentation
local_env <- new.env(parent = .GlobalEnv)
eval(
parse(
text = paste(
sep = "\n",
"fun <- function(x) {",
" y <- x + 1",
" y + 3",
"}"
)
),
envir = local_env
)
local(hash <- rlang::hash(deparse1(fun)), envir = local_env)

testthat::expect_setequal(
trimws(strsplit(
x = teal.code::get_code(modules_output$module_1()()),
split = "\n"
)[[1]]),
c(
"fun <- function(x) {",
"y <- x + 1",
"y + 3",
"}",
sprintf("stopifnot(rlang::hash(deparse1(fun)) == \"%s\")", local_env$hash),
".raw_data <- list2env(list(fun = fun))",
"lockEnvironment(.raw_data)"
)
}
),
"'package:teal' may not be available when loading"
)
}
)
})
})
1 change: 1 addition & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
testthat::test_that("get_teal_bs_theme", {
testthat::skip_if_not_installed("bslib")
testthat::expect_true(is.null(get_teal_bs_theme()))
withr::with_options(list("teal.bs_theme" = bslib::bs_theme(version = "5")), {
testthat::expect_s3_class(get_teal_bs_theme(), "bs_theme")
Expand Down

0 comments on commit 1bd270d

Please sign in to comment.