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
15 changes: 10 additions & 5 deletions R/call_chunk_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
#' @keywords internal
#' @rdname call_chunk_checks
is_reactive_call <- function(x, env = rlang::caller_env()) {
rlang::is_call(x) &&
call_name <- if (rlang::is_call(x)) rlang::call_name(x)

!is.null(call_name) &&
length(rlang::call_args(x)) == 0 &&
rlang::call_name(x) %in% names(env)
call_name %in% names(env) &&
inherits(env[[call_name]], "reactive")
}

#' @description
Expand All @@ -39,10 +42,12 @@ is_reactive_val_call <- function(x, env = rlang::caller_env()) {
#'
#' @rdname call_chunk_checks
is_reactive_val_setter_call <- function(x, env = rlang::caller_env()) {
rlang::is_call(x) &&
call_name <- if (rlang::is_call(x)) rlang::call_name(x)

!is.null(call_name) &&
length(rlang::call_args(x)) == 1L &&
rlang::call_name(x) %in% names(env) &&
inherits(env[[rlang::call_name(x)]], "reactiveVal")
call_name %in% names(env) &&
inherits(env[[call_name]], "reactiveVal")
}

#' @description
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-call_chunk_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,35 @@ test_that("Able to extract package name from unnamespaced function call", {
test_that("When extract package from function call, base R packages are ignored", {
expect_null(get_pkg_name(str2lang("nzchar('foo')")))
})

test_that("Call checks return FALSE, not NA, when the call has no function name", {
anon_call <- str2lang('get("mean")(1:10)')

expect_false(is_reactive_call(anon_call))
expect_false(is_reactive_val_call(anon_call))
expect_false(is_reactive_val_setter_call(anon_call))
})

test_that("A reactive calling an unnamed function is reproduced rather than erroring", {
testthat::skip_if_not_installed("shiny")

shiny::reactiveConsole(TRUE)
on.exit(shiny::reactiveConsole(FALSE), add = TRUE)

named_fn <- shiny::reactive(get("mean")(1:10))
expect_identical(reprex_reactive(named_fn), 'get("mean")(1:10)')

anon_fn <- shiny::reactive((function(x) x + 1)(1))
expect_identical(reprex_reactive(anon_fn), "(function(x) x + 1)(1)")
})

# is_reactive_call ----
test_that("A function call with no arguments is not classed as a reactive", {
testthat::skip_if_not_installed("shiny")

my_fn <- function() "hello"
my_reactive <- shiny::reactive("")

expect_false(is_reactive_call(quote(my_fn())))
expect_true(is_reactive_call(quote(my_reactive())))
})
Loading