Skip to content

Commit

Permalink
Custom tar_knit()/tar_knit_raw() output
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Mar 18, 2024
1 parent 939b342 commit 8417f5e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/tar_knit.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
tar_knit <- function(
name,
path,
output = NULL,
working_directory = NULL,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
Expand All @@ -89,6 +90,8 @@ tar_knit <- function(
) {
targets::tar_assert_package("knitr")
targets::tar_assert_file(path)
targets::tar_assert_chr(output %|||% "x")
targets::tar_assert_scalar(output %|||% "x")
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
Expand All @@ -100,7 +103,7 @@ tar_knit <- function(
)
targets::tar_target_raw(
name = targets::tar_deparse_language(substitute(name)),
command = tar_knit_command(path, working_directory, args, quiet),
command = tar_knit_command(path, output, working_directory, args, quiet),
packages = packages,
library = library,
format = "file",
Expand All @@ -117,8 +120,9 @@ tar_knit <- function(
)
}

tar_knit_command <- function(path, working_directory, args, quiet) {
tar_knit_command <- function(path, output, working_directory, args, quiet) {
args$input <- path
args$output <- output
args$quiet <- quiet
deps <- call_list(as_symbols(knitr_deps(path)))
fun <- call_ns("tarchetypes", "tar_knit_run")
Expand Down
4 changes: 4 additions & 0 deletions R/tar_knit_raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
tar_knit_raw <- function(
name,
path,
output = NULL,
working_directory = NULL,
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
Expand All @@ -75,6 +76,8 @@ tar_knit_raw <- function(
targets::tar_assert_package("knitr")
targets::tar_assert_file(path)
targets::tar_assert_not_dirs(path)
targets::tar_assert_chr(output %|||% "x")
targets::tar_assert_scalar(output %|||% "x")
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
Expand All @@ -84,6 +87,7 @@ tar_knit_raw <- function(
name = name,
command = tar_knit_command(
path,
output,
working_directory,
knit_arguments,
quiet
Expand Down
5 changes: 5 additions & 0 deletions man/tar_knit.Rd

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

5 changes: 5 additions & 0 deletions man/tar_knit_raw.Rd

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

38 changes: 38 additions & 0 deletions tests/testthat/test-tar_knit.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,41 @@ targets::tar_test("tar_knit(nested) runs from project root", {
expect_true(file.exists("here"))
expect_false(file.exists(file.path("out", "here")))
})

targets::tar_test("tar_knit() custom output & working directory", {
skip_on_cran()
skip_rmarkdown()
lines <- c(
"---",
"title: report",
"output_format: html_document",
"---",
"",
"```{r}",
"tar_read(upstream, store = '../_targets')",
"file.create(\"here\")",
"```"
)
dir.create("out")
on.exit(unlink("out", recursive = TRUE))
writeLines(lines, file.path("out", "report.Rmd"))
targets::tar_script({
library(tarchetypes)
list(
tar_target(upstream, "UPSTREAM_SUCCEEDED"),
tar_knit(
name = report,
path = file.path("out", "report.Rmd"),
output = file.path("out", "report.md"),
working_directory = "out"
)
)
})
expect_false(file.exists("here"))
expect_false(file.exists(file.path("out", "here")))
suppressMessages(targets::tar_make(callr_function = NULL))
expect_false(file.exists("here"))
expect_true(file.exists(file.path("out", "here")))
lines <- readLines(file.path("out", "report.md"))
expect_true(any(grepl("UPSTREAM_SUCCEEDED", lines)))
})
38 changes: 38 additions & 0 deletions tests/testthat/test-tar_knit_raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,41 @@ targets::tar_test("tar_knit_raw(nested) runs from the project root", {
expect_true(file.exists("here"))
expect_false(file.exists(file.path("out", "here")))
})

targets::tar_test("tar_knit_raw() custom output & working directory", {
skip_on_cran()
skip_rmarkdown()
lines <- c(
"---",
"title: report",
"output_format: html_document",
"---",
"",
"```{r}",
"tar_read(upstream, store = '../_targets')",
"file.create(\"here\")",
"```"
)
dir.create("out")
on.exit(unlink("out", recursive = TRUE))
writeLines(lines, file.path("report.Rmd"))
targets::tar_script({
library(tarchetypes)
list(
tar_target(upstream, "UPSTREAM_SUCCEEDED"),
tar_knit_raw(
name = "report",
path = "report.Rmd",
working_directory = "out",
output = file.path("out", "report.md")
)
)
})
expect_false(file.exists("here"))
expect_false(file.exists(file.path("out", "here")))
suppressMessages(targets::tar_make(callr_function = NULL))
expect_false(file.exists("here"))
expect_true(file.exists(file.path("out", "here")))
lines <- readLines(file.path("out", "report.md"))
expect_true(any(grepl("UPSTREAM_SUCCEEDED", lines)))
})

0 comments on commit 8417f5e

Please sign in to comment.