Skip to content

Commit

Permalink
tar_assign() informative error
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Aug 16, 2024
1 parent 9526f54 commit c95761e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 9 additions & 1 deletion R/tar_assign.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ tar_assign <- function(targets) {
as.list(expr[-1L]),
list(expr)
)
check_assign <- function(x){
check_assign <- function(x) {
identical(x[[1L]], quote(`<-`)) || identical(x[[1L]], quote(`=`))

This comment has been minimized.

Copy link
@hadley

hadley Aug 19, 2024

Member

FWIW since you're using rlang, you could simplify this to is_call(x, c("<-", "="))

}
targets::tar_assert_true(
Expand All @@ -76,6 +76,14 @@ tar_assign <- function(targets) {
}

tar_assign_parse <- function(statement, envir) {
targets::tar_assert_true(
length(statement) > 2L && is.call(statement[[3L]]),
msg = paste0(
"tar_assign() failed to parse statement `",
tar_deparse_language(statement),
"` because the right-hand side is not a function call."
)
)
call <- statement[[3L]]
call$name <- statement[[2L]]
eval(call, envir = envir)
Expand Down
16 changes: 13 additions & 3 deletions tests/testthat/test-tar_assign.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ targets::tar_test("tar_assign() single statement", {
expect_equal(out$command, "c(1L, 2L)")
targets::tar_make(callr_function = NULL)
expect_equal(targets::tar_read(x), c(1L, 2L))

targets::tar_script(tar_assign({x = tar_target(c(1L, 2L))}))
targets::tar_script(tar_assign({x = tar_target(c(1L, 2L))})) # nolint
out <- targets::tar_manifest(callr_function = NULL)
expect_equal(out$name, "x")
expect_equal(out$command, "c(1L, 2L)")
Expand All @@ -17,7 +16,7 @@ targets::tar_test("tar_assign() single statement", {
targets::tar_test("tar_assign()", {
targets::tar_script({
tar_assign({
x = tar_target(c(1L, 2L))
x = tar_target(c(1L, 2L)) # nolint
y <- tar_target(x + 1L, pattern = map(x))
z <- tar_rep(TRUE, batches = 2L)
})
Expand All @@ -44,3 +43,14 @@ targets::tar_test("tar_assign()", {
expect_equal(targets::tar_read(z_batch), c(1L, 2L))
expect_equal(unname(targets::tar_read(z)), rep(TRUE, 2L))
})

targets::tar_test("tar_assign() non-calls", {
expect_error(
tar_assign({
x <- targets::tar_target(1)
y <- 3
z <- tar_target(x + y)
}),
class = "tar_condition_validate"
)
})

0 comments on commit c95761e

Please sign in to comment.