diff --git a/R/tar_knit.R b/R/tar_knit.R index ef75e10..5463a59 100644 --- a/R/tar_knit.R +++ b/R/tar_knit.R @@ -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"), @@ -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) } @@ -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", @@ -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") diff --git a/R/tar_knit_raw.R b/R/tar_knit_raw.R index 0573590..e433db8 100644 --- a/R/tar_knit_raw.R +++ b/R/tar_knit_raw.R @@ -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"), @@ -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) } @@ -84,6 +87,7 @@ tar_knit_raw <- function( name = name, command = tar_knit_command( path, + output, working_directory, knit_arguments, quiet diff --git a/man/tar_knit.Rd b/man/tar_knit.Rd index cf0e5ae..a2f24ba 100644 --- a/man/tar_knit.Rd +++ b/man/tar_knit.Rd @@ -7,6 +7,7 @@ tar_knit( name, path, + output = NULL, working_directory = NULL, tidy_eval = targets::tar_option_get("tidy_eval"), packages = targets::tar_option_get("packages"), @@ -44,6 +45,10 @@ on the result to locally recreate the target's initial RNG state.} \item{path}{Character string, file path to the \code{knitr} source file. Must have length 1.} +\item{output}{Path to the output file for \code{knit()}. If \code{NULL}, this +function will try to guess a default, which will be under the current +working directory.} + \item{working_directory}{Optional character string, path to the working directory to temporarily set when running the report. diff --git a/man/tar_knit_raw.Rd b/man/tar_knit_raw.Rd index 68d3b78..0f1b51c 100644 --- a/man/tar_knit_raw.Rd +++ b/man/tar_knit_raw.Rd @@ -7,6 +7,7 @@ tar_knit_raw( name, path, + output = NULL, working_directory = NULL, packages = targets::tar_option_get("packages"), library = targets::tar_option_get("library"), @@ -29,6 +30,10 @@ tar_knit_raw( \item{path}{Character string, file path to the \code{knitr} source file. Must have length 1.} +\item{output}{Path to the output file for \code{knit()}. If \code{NULL}, this +function will try to guess a default, which will be under the current +working directory.} + \item{working_directory}{Optional character string, path to the working directory to temporarily set when running the report. diff --git a/tests/testthat/test-tar_knit.R b/tests/testthat/test-tar_knit.R index b18d59f..2e04dd9 100644 --- a/tests/testthat/test-tar_knit.R +++ b/tests/testthat/test-tar_knit.R @@ -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))) +}) diff --git a/tests/testthat/test-tar_knit_raw.R b/tests/testthat/test-tar_knit_raw.R index 3e423cd..75b3735 100644 --- a/tests/testthat/test-tar_knit_raw.R +++ b/tests/testthat/test-tar_knit_raw.R @@ -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))) +})