From 7a041cbfd81043986495a6328c2c836ca000345a Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Sat, 24 Aug 2024 10:36:53 -0400 Subject: [PATCH] Better fix for issue #30. --- R/tabular.R | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/R/tabular.R b/R/tabular.R index 16aedd9..d8293a7 100644 --- a/R/tabular.R +++ b/R/tabular.R @@ -629,15 +629,21 @@ tabular.formula <- function(table, data=NULL, n, suppressLabels=0, ...) { if (missing(n) && inherits(data, "data.frame")) n <- nrow(data) - # We need access to labelSubset() (and perhaps other functions) - # when evaluating a table expression. (Issue #30) + # We need access to labelSubset() (and perhaps other functions in future) + # when evaluating a table expression (issue #30), but we don't want + # to mask the user's copy. + + parent <- if (is.environment(data)) data else environment(table) + if (!exists("labelSubset", envir = parent)) { + withTableFns <- new.env(parent = parent) + withTableFns$labelSubset <- labelSubset + } else + withTableFns <- parent - withTableFns <- new.env(parent = if (is.environment(data)) data else environment(table)) - withTableFns$labelSubset <- labelSubset if (is.null(data)) data <- withTableFns else if (is.list(data)) - data <- list2env(data, parent=withTableFns) + data <- list2env(data, parent = withTableFns) else if (!is.environment(data)) stop("'data' must be a dataframe, list or environment")