Skip to content

Improvements (i hope so) #254

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

Merged
merged 5 commits into from
Jun 4, 2025
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
29 changes: 17 additions & 12 deletions R/qenv-eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@
#' q <- eval_code(q, expression(assert_number(a)))
#'
#' @aliases eval_code,qenv-method
#' @aliases eval_code,qenv.error-method
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is needed - all methods have to have own docs position

#'
#' @export
setGeneric("eval_code", function(object, code, cache = FALSE, ...) standardGeneric("eval_code"))

setMethod("eval_code", signature = c(object = "qenv"), function(object, code, cache = FALSE, ...) {
code <- .preprocess_code(code) # preprocess code to ensure it is a character vector
if (!is.language(code) && !is.character(code)) {
stop("eval_code accepts code being language or character")
}
code <- .preprocess_code(code)
# preprocess code to ensure it is a character vector
.eval_code(object = object, code = code, cache = cache, ...)
})

setMethod("eval_code", signature = c(object = "qenv.error"), function(object, code, cache = FALSE, ...) object)

#' @keywords internal
.eval_code <- function(object, code, cache = FALSE, ...) {
if (identical(code, "")) {
return(object)
}
parsed_code <- parse(text = code, keep.source = TRUE)
object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(.GlobalEnv))
if (length(parsed_code) == 0) {
Expand Down Expand Up @@ -100,17 +108,14 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co
}

setGeneric(".preprocess_code", function(code) standardGeneric(".preprocess_code"))
setMethod(".preprocess_code", signature = c("ANY"), function(code) paste(code, collapse = "\n"))
setMethod(".preprocess_code", signature = c("language"), function(code) {
paste(
vapply(lang2calls(code), deparse1, collapse = "\n", character(1L)),
collapse = "\n"
)
})
setMethod(".preprocess_code", signature = c("expression"), function(code) {
if (length(attr(code, "wholeSrcref")) == 0L) {
paste(lang2calls(code), collapse = "\n")
} else {
setMethod(".preprocess_code", signature = c("character"), function(code) paste(code, collapse = "\n"))
setMethod(".preprocess_code", signature = c("ANY"), function(code) {
if (is.expression(code) && length(attr(code, "wholeSrcref"))) {
paste(attr(code, "wholeSrcref"), collapse = "\n")
} else {
paste(
vapply(lang2calls(code), deparse1, collapse = "\n", character(1L)),
collapse = "\n"
)
}
})
17 changes: 6 additions & 11 deletions R/qenv-within.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,15 @@
#' @export
#'
within.qenv <- function(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.

Should we check for named dots?

pkgload::load_all("teal.code")
#> ℹ Loading teal.code
qenv() |> within(aa <- 1+1, TRUE, FALSE)
#> <environment: 0x5b1c29313ae0> 🔒 
#> Parent: <environment: devtools_shims> 
#> Bindings:
#> - aa: [numeric]

Created on 2025-06-03 with reprex v2.1.1

expr <- substitute(expr)
expr <- as.expression(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))
calls <- lapply(expr, function(x) do.call(substitute, list(x, env = extras)))
do.call(
eval_code,
utils::modifyList(extras, list(object = data, code = as.expression(calls)))
)
}


Expand Down
1 change: 1 addition & 0 deletions man/eval_code.Rd

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

21 changes: 10 additions & 11 deletions tests/testthat/test-qenv_eval_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ testthat::test_that("eval_code works with expression", {
testthat::expect_equal(q1, list2env(list(a = 1, b = 2)))
})

testthat::test_that("eval_code ignores empty code", {
q <- qenv()
testthat::expect_identical(q, eval_code(q, ""))
})

testthat::test_that("eval_code preserves original formatting when `srcref` is present in the expression", {
code <- "# comment
a <- 1L"
Expand Down Expand Up @@ -77,12 +82,11 @@ testthat::test_that("eval_code works with quoted code block", {
testthat::expect_equal(q1, list2env(list(a = 1, b = 2)))
})

testthat::test_that("eval_code fails with unquoted expression", {
b <- 3
testthat::expect_error(
eval_code(qenv(), a <- b),
"unable to find an inherited method for function .eval_code. for signature"
)
testthat::test_that("eval_code fails with code not being language nor character", {
msg <- "eval_code accepts code being language or character"
testthat::expect_error(eval_code(qenv(), NULL), msg)
testthat::expect_error(eval_code(qenv(), 1), msg)
testthat::expect_error(eval_code(qenv(), list()), msg)
})

testthat::test_that("an error when calling eval_code returns a qenv.error object which has message and trace", {
Expand Down Expand Up @@ -182,8 +186,3 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
"x"
)
})

testthat::test_that("Code executed with integer shorthand (1L) is the same as original", {
q <- within(qenv(), a <- 1L)
testthat::expect_identical(get_code(q), "a <- 1L")
})
5 changes: 5 additions & 0 deletions tests/testthat/test-qenv_within.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ testthat::describe("within run with `=`", {
testthat::expect_equal(q$i, 1)
})
})

testthat::test_that("Code executed with integer shorthand (1L) is the same as original", {
q <- within(qenv(), a <- 1L)
testthat::expect_identical(get_code(q), "a <- 1L")
})