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

1352 functions in data raise warnings #1353

Closed
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
10 changes: 8 additions & 2 deletions R/module_init_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,23 @@ srv_init_data <- function(id, data) {

#' Get code that tests the integrity of the reproducible data
#'
#' @section Functions:
#' Data objects that are functions are converted to strings representing their body
#' in order to strip their enclosing environments.
#'
#' @param data (`teal_data`) object holding the data
#' @param datanames (`character`) names of `datasets`
#'
#' @return A character vector with the code lines.
#' @keywords internal
#'
.get_hashes_code <- function(data, datanames = ls(teal.code::get_env(data))) {
# Functions cause problems when calculating hashes
# because they have environments that have parents on the search list.
vapply(
datanames,
function(dataname, datasets) {
hash <- rlang::hash(data[[dataname]])
function(dataname) {
hash <- rlang::hash(defunction(data[[dataname]]))
sprintf(
"stopifnot(%s == %s) # @linksto %s",
deparse1(bquote(rlang::hash(.(as.name(dataname))))),
Expand Down
7 changes: 4 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ build_app_title <- function(
#' App ID is a hash of the app's data and modules.
#' See "transferring snapshots" section in ?snapshot.
#'
#' @inheritSection .get_hashes_code Functions
#'
#' @param data (`teal_data` or `teal_data_module`) as accepted by `init`
#' @param modules (`teal_modules`) object as accepted by `init`
#'
Expand All @@ -296,16 +298,15 @@ create_app_id <- function(data, modules) {
} else if (inherits(data, "teal_data_module")) {
deparse1(body(data$server))
}
modules <- lapply(modules, defunction)

rlang::hash(list(data = data, modules = modules))
rlang::hash(defunction(list(data = data, modules = modules)))
}

#' Go through list and extract bodies of encountered functions as string, recursively.
#' @keywords internal
#' @noRd
defunction <- function(x) {
if (is.list(x)) {
if (checkmate::test_list(x)) {
lapply(x, defunction)
} else if (is.function(x)) {
deparse1(body(x))
Expand Down
6 changes: 6 additions & 0 deletions man/create_app_id.Rd

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

6 changes: 6 additions & 0 deletions man/dot-get_hashes_code.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,16 @@ testthat::test_that("defunction recursively goes down a list", {
y
)
})

testthat::test_that("defunction leaves lists with other classes intact", {
# styler: off
x <- list(
"character" = "character",
"data.frame" = head(mtcars)
)

testthat::expect_identical(
defunction(x),
x
)
})
Loading