Skip to content

Commit

Permalink
feat: adds documentation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Nov 28, 2023
1 parent 03d5b7e commit dc6739f
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 114 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Imports:
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 +57,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 +98,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
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(c,teal_slices)
S3method(get_code,tdata)
S3method(get_metadata,default)
S3method(get_metadata,tdata)
S3method(join_keys,tdata)
Expand All @@ -19,7 +20,6 @@ export("%>%")
export(TealReportCard)
export(as.teal_slices)
export(example_module)
export(get_code.tdata)
export(get_code_tdata)
export(get_metadata)
export(init)
Expand Down
90 changes: 90 additions & 0 deletions R/teal_data_module-eval_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
setOldClass("teal_data_module")

#' @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`)
#' @inherit teal.code::eval_code
#' @importMethodsFrom teal.code 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))
#' })
#' }
#' )
#' \dontrun{
#' eval_code(tdm, "IRIS <- subset(IRIS, Species == 'virginica')")
#' }
setMethod("eval_code", signature = c("teal_data_module", "character"), function(object, code) {
tdm <- teal_data_module(
ui = function(id) {
ns <- NS(id)
object$ui(ns("mutate_inner"))
},
server = function(id) {
moduleServer(id, function(input, output, session) {
data <- object$server("mutate_inner")
reactive(eval_code(data(), code))
})
}
)

attr(tdm, "code") <- paste(c(attr(object, "code"), code), collapse = "\n")
tdm
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) {
eval_code(object, code = format_expression(code))
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) {
eval_code(object, code = format_expression(code))
})

#' @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))
#' })
#' }
#' )
#' \dontrun{
#' 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))
}
54 changes: 0 additions & 54 deletions R/teal_data_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,57 +50,3 @@ teal_data_module <- function(ui, server) {
class = "teal_data_module"
)
}

setOldClass("teal_data_module")

#' @name eval_code
#' @inherit teal.code::eval_code
#' @importMethodsFrom teal.code eval_code
#' @export
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) {
data <- object$server("mutate_inner")
eventReactive(data(), {
eval_code(data(), code)
})
})
}
)
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("teal_data_module", "language"), function(object, code) {
eval_code(object, code = teal.code:::format_expression(code))
})

#' @rdname eval_code
#' @export
setMethod("eval_code", signature = c("teal_data_module", "expression"), function(object, code) {
eval_code(object, code = teal.code:::format_expression(code))
})

#' @inherit teal.code::within.qenv
#' @export
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))
}
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 format_expression should not be exported from teal.code
format_expression <- getFromNamespace("format_expression", "teal.code")
20 changes: 12 additions & 8 deletions man/eval_code.Rd

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

4 changes: 2 additions & 2 deletions man/init.Rd

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

4 changes: 2 additions & 2 deletions man/srv_teal_with_splash.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/tdata.Rd

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

4 changes: 2 additions & 2 deletions man/ui_teal_with_splash.Rd

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

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

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

Loading

0 comments on commit dc6739f

Please sign in to comment.