Skip to content

Commit

Permalink
Merge pull request #189 from rmflight/allow-equals-188
Browse files Browse the repository at this point in the history
allows use of = for assignment in tar_assign
  • Loading branch information
wlandau authored Aug 16, 2024
2 parents ce61fbc + c3c14b2 commit 9526f54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
14 changes: 9 additions & 5 deletions R/tar_assign.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#' are as follows:
#' * The code supplied to [tar_assign()] must be enclosed in curly braces
#' beginning with `{` and `}` unless it only contains a
#' one-line statement.
#' one-line statement or uses `=` as the assignment.
#' * Each statement in the code block must be of the form
#' `x <- f()`, where `x` is the name of a target and
#' `x <- f()`, or `x = f()` where `x` is the name of a target and
#' `f()` is a function like `tar_target()` or [tar_quarto()]
#' which accepts a `name` argument.
#' * The native pipe operator `|>` is allowed because it lazily
Expand All @@ -36,7 +36,7 @@
#' filter(!is.na(Ozone)) |>
#' tar_target()
#'
#' model <- lm(Ozone ~ Temp, data) |>
#' model = lm(Ozone ~ Temp, data) |>
#' coefficients() |>
#' tar_target()
#'
Expand All @@ -59,12 +59,16 @@ tar_assign <- function(targets) {
as.list(expr[-1L]),
list(expr)
)
check_assign <- function(x){

Check warning on line 62 in R/tar_assign.R

View workflow job for this annotation

GitHub Actions / lint

file=R/tar_assign.R,line=62,col=30,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 62 in R/tar_assign.R

View workflow job for this annotation

GitHub Actions / lint

file=R/tar_assign.R,line=62,col=30,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
identical(x[[1L]], quote(`<-`)) || identical(x[[1L]], quote(`=`))
}
targets::tar_assert_true(
all(map_lgl(statements, ~identical(.x[[1L]], quote(`<-`)))),
all(map_lgl(statements, check_assign)),
msg = paste(
"tar_assign() code must be enclosed in curly braces if",
"it has multiple statements, and each statement",
"must be an assignment statement using the left arrow <-"
"must be an assignment statement using the left arrow <-",
"or equal sign ="
)
)
envir <- targets::tar_option_get("envir")
Expand Down
6 changes: 3 additions & 3 deletions man/tar_assign.Rd

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

9 changes: 8 additions & 1 deletion tests/testthat/test-tar_assign.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ 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))

Check warning on line 8 in tests/testthat/test-tar_assign.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-tar_assign.R,line=8,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
targets::tar_script(tar_assign({x = tar_target(c(1L, 2L))}))

Check warning on line 9 in tests/testthat/test-tar_assign.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-tar_assign.R,line=9,col=34,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 9 in tests/testthat/test-tar_assign.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-tar_assign.R,line=9,col=37,[assignment_linter] Use <-, not =, for assignment.
out <- targets::tar_manifest(callr_function = NULL)
expect_equal(out$name, "x")
expect_equal(out$command, "c(1L, 2L)")
targets::tar_make(callr_function = NULL)
expect_equal(targets::tar_read(x), c(1L, 2L))
})

targets::tar_test("tar_assign()", {
targets::tar_script({
tar_assign({
x <- tar_target(c(1L, 2L))
x = tar_target(c(1L, 2L))

Check warning on line 20 in tests/testthat/test-tar_assign.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-tar_assign.R,line=20,col=9,[assignment_linter] Use <-, not =, for assignment.
y <- tar_target(x + 1L, pattern = map(x))
z <- tar_rep(TRUE, batches = 2L)
})
Expand Down

0 comments on commit 9526f54

Please sign in to comment.