Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds within and eval_code to teal_data_module #983

Merged
merged 29 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
03d5b7e
quick prototype for eval_code and teal
averissimo Nov 23, 2023
dc6739f
feat: adds documentation and tests
averissimo Nov 28, 2023
293f25a
remove code attribute
averissimo Nov 28, 2023
3a8579d
docs: corrects documentation to pass checks
averissimo Nov 28, 2023
c04eb56
checks: corrects note with methods
averissimo Nov 28, 2023
2992271
lintr: ignores name linter and implements suggestion
averissimo Nov 28, 2023
f65a389
docs: updates pkgdown
averissimo Nov 28, 2023
d8a095e
fix: corrects example
averissimo Nov 28, 2023
417c2ee
fix: use event_reactive to avoid problems when data is NULL
averissimo Nov 28, 2023
9605f05
docs: update teal_data_module vignette
averissimo Nov 28, 2023
fa6ef28
tests: minor changes
averissimo Nov 28, 2023
441652d
Merge branch 'main' into ddl@main
m7pr Nov 29, 2023
8bf8aaa
fix: only performs eval_code if teal_data
averissimo Dec 1, 2023
51dcadf
tests: implement @gogonzo suggestion about indirect tests
averissimo Dec 1, 2023
da3c100
fix: eval_code only accepts teal_data or propagates error messages
averissimo Dec 1, 2023
c15c106
tests: rename test
averissimo Dec 1, 2023
c2f708a
fix: swaps validate with qenv.error
averissimo Dec 1, 2023
34db6c6
tests: using quote on an eval_code test
averissimo Dec 1, 2023
ef1877c
fix: typo and removed repeated test
averissimo Dec 1, 2023
a861bec
fix: using lang2calls
averissimo Dec 1, 2023
e31ae85
Update R/teal_data_module-eval_code.R
averissimo Dec 4, 2023
3b9da92
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Dec 4, 2023
71cfbf9
pr: implements suggestions
averissimo Dec 4, 2023
fc10479
tests: rewrite tests to use testServer and adds splash
averissimo Dec 4, 2023
af76edc
tests: add expect_no_error to testServer calls
averissimo Dec 4, 2023
d9fdbb9
pr: remove lingering dontrun
averissimo Dec 5, 2023
c012daf
pr: remove magrittr pipe usage
averissimo Dec 5, 2023
f661743
lintr: corrects problem
averissimo Dec 5, 2023
b1625ba
pr: remove magrittr pipe usage and native
averissimo Dec 5, 2023
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
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ Imports:
lifecycle (>= 0.2.0),
logger (>= 0.2.0),
magrittr (>= 1.5),
methods,
rlang (>= 1.0.0),
shinyjs,
stats,
teal.code (>= 0.3.0.9009),
teal.logger (>= 0.1.1),
teal.reporter (>= 0.2.0),
teal.widgets (>= 0.4.0),
Expand All @@ -56,7 +58,6 @@ Suggests:
R6,
rmarkdown (>= 2.19),
shinyvalidate,
teal.code (>= 0.3.0.9009),
testthat (>= 3.1.5),
withr (>= 2.1.0),
yaml (>= 1.1.0)
Expand Down Expand Up @@ -98,6 +99,7 @@ Collate:
'show_rcode_modal.R'
'tdata.R'
'teal.R'
'teal_data_module-eval_code.R'
'teal_data_module.R'
'teal_reporter.R'
'teal_slices-store.R'
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ S3method(toString,teal_modules)
S3method(ui_nested_tabs,default)
S3method(ui_nested_tabs,teal_module)
S3method(ui_nested_tabs,teal_modules)
S3method(within,teal_data_module)
export("%>%")
export(TealReportCard)
export(as.teal_slices)
Expand Down Expand Up @@ -42,10 +43,13 @@ export(validate_inputs)
export(validate_n_levels)
export(validate_no_intersection)
export(validate_one_row_per_id)
exportMethods(eval_code)
import(shiny)
import(teal.data)
import(teal.slice)
import(teal.transform)
importFrom(lifecycle,deprecate_soft)
importFrom(magrittr,"%>%")
importFrom(methods,setMethod)
importFrom(stats,setNames)
importMethodsFrom(teal.code,eval_code)
104 changes: 104 additions & 0 deletions R/teal_data_module-eval_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
setOldClass("teal_data_module")

#' Evaluate the code in the qenv environment
#' @name eval_code
#' @description
#' Given code is evaluated in the `qenv` environment of `teal_data` reactive defined in `teal_data_module`.
#' @param object (`teal_data_module`)
#' @inheritParams teal.code::eval_code
#' @return Returns a `teal_data_module` object.
#' @importMethodsFrom teal.code eval_code
#' @importFrom methods setMethod
NULL

#' @rdname eval_code
#' @export
#'
#' @examples
#' tdm <- teal_data_module(
#' ui = function(id) div(id = shiny::NS(id)("div_id")),
#' server = function(id) {
#' shiny::moduleServer(id, function(input, output, session) {
#' shiny::reactive(teal_data(IRIS = iris))
#' })
#' }
#' )
#' eval_code(tdm, "IRIS <- subset(IRIS, Species == 'virginica')")
setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) {
teal_data_module(
ui = function(id) {
ns <- NS(id)
object$ui(ns("mutate_inner"))
},
server = function(id) {
moduleServer(id, function(input, output, session) {
teal_data_rv <- object$server("mutate_inner")

if (!is.reactive(teal_data_rv)) {
stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)
}

td <- eventReactive(teal_data_rv(),
{
if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {
eval_code(teal_data_rv(), code)
} else {
averissimo marked this conversation as resolved.
Show resolved Hide resolved
teal_data_rv()
}
},
ignoreNULL = FALSE
)
td
})
}
)
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) {
eval_code(object, code = paste(lang2calls(code), collapse = "\n"))
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) {
eval_code(object, code = paste(lang2calls(code), collapse = "\n"))
})

#' @inherit teal.code::within.qenv params title details
#' @description
#' Convenience function for evaluating inline code inside the environment of a
#' `teal_data_module`
#'
#' @param data (`teal_data_module`) object
#' @return Returns a `teal_data_module` object with a delayed evaluation of `expr`
#' when module.
#' @export
#' @seealso [base::within()], [teal_data_module()]
#' @examples
#' tdm <- teal_data_module(
#' ui = function(id) div(id = shiny::NS(id)("div_id")),
#' server = function(id) {
#' shiny::moduleServer(id, function(input, output, session) {
#' shiny::reactive(teal_data(IRIS = iris))
#' })
#' }
#' )
#' within(tdm, IRIS <- subset(IRIS, Species == "virginica"))
within.teal_data_module <- function(data, expr, ...) {
expr <- substitute(expr)
extras <- list(...)

# Add braces for consistency.
if (!identical(as.list(expr)[[1L]], as.symbol("{"))) {
expr <- call("{", expr)
}

calls <- as.list(expr)[-1]

# Inject extra values into expressions.
calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras)))

eval_code(object = data, code = as.expression(calls))
}
Comment on lines +89 to +104
Copy link
Contributor Author

@averissimo averissimo Nov 28, 2023

Choose a reason for hiding this comment

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

The body is a copy from teal.code:::within.qenv, it just prepares the arguments to call eval_code() (data is unchanged)

So in practice it could be replaced with code below (reducing duplicated code accross {teal.*}):

Thoughts?

Suggested change
within.teal_data_module <- function(data, expr, ...) {
expr <- substitute(expr)
extras <- list(...)
# Add braces for consistency.
if (!identical(as.list(expr)[[1L]], as.symbol("{"))) {
expr <- call("{", expr)
}
calls <- as.list(expr)[-1]
# Inject extra values into expressions.
calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras)))
eval_code(object = data, code = as.expression(calls))
}
within.teal_data_module <- function(data, expr, ...) {
within.qenv <- getFromNamespace("within.qenv", "teal.code")
within.qenv(data, expr, ...)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I think getS3method("within", "qenv", envir = as.environment(getNamespace("teal.code"))) would be a better choice.
  2. It shouldn't be necessary anyway because teal.code must be installed when teal is.
  3. At the moment within only processes the expression and calls eval_code. In this context this is a valid simplification. I'm not sure, though 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I think getS3method("within", "qenv", envir = as.environment(getNamespace("teal.code"))) would be a better choice.
  2. It shouldn't be necessary anyway because teal.code must be installed when teal is.

That's better! Is the envir parameter required? (as you said, teal.code is imported and it seems to find the within)

  1. At the moment within only processes the expression and calls eval_code. In this context this is a valid simplification. I'm not sure, though 🤔

@vedhav brough a smilar concern, I'm on the fence as I prefer to minimize repetitive code, but I don't think this solution is "best practice" (calling within.qenv with a different data object seems like a red flag).

I would prefer if there was within.default (non-exported) or .generic_within in {teal.code} that was the backbone of both of those calls and still allow for deviations in the future of within.qenv that wouldn't affect within.teal_data_module.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's better! Is the envir parameter required?

It isn't but just in case there is a different within.qenv defined somewhere...

Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer if there was within.default

The within generic is a base function so defining a default that suits our particular classes would cause a mess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The within generic is a base function so defining a default that suits our particular classes would cause a mess.

Yup, so a .generic_within function would be best if we wanted to go this route.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the .default was a suggestion if it didn't interfere with base::within (by not exporting it, but even then it might be "dangerous")

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, but S3 methods must be exported 😉

Copy link
Contributor

Choose a reason for hiding this comment

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

@gogonzo would you like to pitch in?

Copy link
Contributor

@pawelru pawelru Dec 4, 2023

Choose a reason for hiding this comment

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

One interesting pattern that I have found when working on the verdepcheck is to make a simple (i.e. non S3 / non S4 etc.) (implementation) function for each method and these methods are essentially a one-liner calling those functions. Please find an example in R6 context: https://github.com/r-lib/pkgdepends/blob/main/R/pkg-plan.R - all public methods are calling private (implementation) functions.

Then you can re-use those implementation functions from multiple classes but there are some obvious drawbacks though such as number of objects in a package, documentation, maintenance, debugging etc.

This however is not addressing an exporting / importing issue mentioned here 😈

2 changes: 1 addition & 1 deletion R/teal_data_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' dataset2 <- mtcars
#' }
#' )
#' datanames(data) <- c("iris", "mtcars")
#' datanames(data) <- c("dataset1", "dataset2")
#'
#' data
#' })
Expand Down
4 changes: 4 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ setdiff_teal_slices <- getFromNamespace("setdiff_teal_slices", "teal.slice")
coalesce_r <- getFromNamespace("coalesce_r", "teal.slice")
# all *Block objects are private in teal.reporter
RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") # nolint

# Use non-exported function(s) from teal.code
# This one is here because lang2calls should not be exported from teal.code
lang2calls <- getFromNamespace("lang2calls", "teal.code")
chlebowa marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ reference:
- get_code_tdata
- get_metadata
- tdata2env
- eval_code
- within.teal_data_module
- show_rcode_modal
- join_keys.tdata
# - title: Functions Moved to Other Packages
Expand Down
37 changes: 37 additions & 0 deletions man/eval_code.Rd

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

2 changes: 1 addition & 1 deletion man/teal_data_module.Rd

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

43 changes: 43 additions & 0 deletions man/within.teal_data_module.Rd

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

1 change: 0 additions & 1 deletion tests/testthat/test-module_nested_tabs.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
teal_data <- teal.data::teal_data()
teal_data <- within(teal_data, iris <- head(iris))
teal_data <- teal.data::teal_data() |> within(iris <- head(iris))
datanames(teal_data) <- "iris"
filtered_data <- teal_data_to_filtered_data(teal_data)

Expand Down
97 changes: 96 additions & 1 deletion tests/testthat/test-module_teal_with_splash.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ testthat::test_that("srv_teal_with_splash teal_data_rv_validate throws then qenv
id = "test",
data = teal_data_module(
ui = function(id) div(),
server = function(id) reactive(teal_data() %>% within(stop("not good")))
server = function(id) reactive(within(teal_data(), stop("not good")))
),
modules = modules(example_module())
),
Expand Down Expand Up @@ -210,3 +210,98 @@ testthat::test_that("srv_teal_with_splash gets observe event from srv_teal", {
}
)
})

testthat::test_that("srv_teal_with_splash accepts data after within.teal_data_module", {
tdm <- teal_data_module(ui = function(id) div(), server = function(id) reactive(teal_data(IRIS = iris)))
tdm2 <- within(tdm, IRIS$id <- seq_len(NROW(IRIS$Species))) # nolint: object_name_linter.

testthat::expect_no_error(
shiny::testServer(
app = srv_teal_with_splash,
args = list(
id = "id",
data = tdm2,
modules = modules(example_module())
),
expr = {
testthat::expect_s3_class(teal_data_rv, "reactive")
testthat::expect_s3_class(teal_data_rv_validate, "reactive")
testthat::expect_s4_class(teal_data_rv_validate(), "teal_data")
testthat::expect_identical(
teal_data_rv_validate()[["IRIS"]],
within(iris, id <- seq_len(NROW(Species)))
)
}
)
)
})

testthat::test_that("srv_teal_with_splash throws error when within.teal_data_module returns qenv.error", {
tdm <- teal_data_module(ui = function(id) div(), server = function(id) reactive(teal_data(IRIS = iris)))
tdm2 <- within(tdm, non_existing_var + 1)

testthat::expect_no_error(
shiny::testServer(
app = srv_teal_with_splash,
args = list(
id = "id",
data = tdm2,
modules = modules(example_module())
),
expr = {
testthat::expect_s3_class(teal_data_rv, "reactive")
testthat::expect_s3_class(teal_data_rv(), "qenv.error")
testthat::expect_s3_class(teal_data_rv_validate, "reactive")
testthat::expect_error(teal_data_rv_validate(), "when evaluating qenv code")
}
)
)
})

testthat::test_that("srv_teal_with_splash throws error when within.teal_data_module returns NULL", {
tdm <- teal_data_module(ui = function(id) div(), server = function(id) reactive(NULL))
tdm2 <- within(tdm, within(1 + 1))
testthat::expect_no_error(
shiny::testServer(
app = srv_teal_with_splash,
args = list(
id = "id",
data = tdm2,
modules = modules(example_module())
),
expr = {
testthat::expect_s3_class(teal_data_rv, "reactive")
testthat::expect_null(teal_data_rv())
testthat::expect_s3_class(teal_data_rv_validate, "reactive")
testthat::expect_error(teal_data_rv_validate(), "`teal_data_module` did not return `teal_data` object ")
}
)
)
})

testthat::test_that(
paste(
"srv_teal_with_splash throws error when within.teal_data_module returns arbitrary object",
"(other than `teal_data` or `qenv.error`)"
),
{
tdm <- teal_data_module(ui = function(id) div(), server = function(id) reactive(NULL))
tdm2 <- within(tdm, 1 + 1)
testthat::expect_no_error(
shiny::testServer(
app = srv_teal_with_splash,
args = list(
id = "id",
data = tdm2,
modules = modules(example_module())
),
expr = {
testthat::expect_s3_class(teal_data_rv, "reactive")
testthat::expect_null(teal_data_rv())
testthat::expect_s3_class(teal_data_rv_validate, "reactive")
testthat::expect_error(teal_data_rv_validate(), "`teal_data_module` did not return `teal_data` object ")
}
)
)
}
)
Loading