Skip to content

Commit

Permalink
Sketch #169
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Mar 18, 2024
1 parent 8507cfd commit 939b342
Show file tree
Hide file tree
Showing 23 changed files with 203 additions and 48 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ importFrom(targets,tar_assert_df)
importFrom(targets,tar_assert_envir)
importFrom(targets,tar_assert_equal_lengths)
importFrom(targets,tar_assert_expr)
importFrom(targets,tar_assert_file)
importFrom(targets,tar_assert_flag)
importFrom(targets,tar_assert_function)
importFrom(targets,tar_assert_ge)
Expand Down Expand Up @@ -226,6 +227,7 @@ importFrom(utils,globalVariables)
importFrom(utils,head)
importFrom(vctrs,vec_c)
importFrom(vctrs,vec_rbind)
importFrom(withr,local_dir)
importFrom(withr,local_envvar)
importFrom(withr,local_options)
importFrom(withr,with_options)
27 changes: 18 additions & 9 deletions R/tar_knit.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#' @inheritSection tar_map Target objects
#' @inheritParams targets::tar_target
#' @inheritParams knitr::knit
#' @inheritParams tar_render
#' @param path Character string, file path to the `knitr` source file.
#' Must have length 1.
#' @param ... Named arguments to `knitr::knit()`.
Expand Down Expand Up @@ -70,6 +71,7 @@
tar_knit <- function(
name,
path,
working_directory = NULL,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
Expand All @@ -86,9 +88,10 @@ tar_knit <- function(
...
) {
targets::tar_assert_package("knitr")
targets::tar_assert_scalar(path)
targets::tar_assert_chr(path)
targets::tar_assert_path(path)
targets::tar_assert_file(path)
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
envir <- tar_option_get("envir")
args <- targets::tar_tidy_eval(
substitute(list(...)),
Expand All @@ -97,7 +100,7 @@ tar_knit <- function(
)
targets::tar_target_raw(
name = targets::tar_deparse_language(substitute(name)),
command = tar_knit_command(path, args, quiet),
command = tar_knit_command(path, working_directory, args, quiet),
packages = packages,
library = library,
format = "file",
Expand All @@ -114,12 +117,18 @@ tar_knit <- function(
)
}

tar_knit_command <- function(path, args, quiet) {
tar_knit_command <- function(path, working_directory, args, quiet) {
args$input <- path
args$quiet <- quiet
deps <- call_list(as_symbols(knitr_deps(path)))
fun <- call_ns("tarchetypes", "tar_knit_run")
exprs <- list(fun, path = path, args = args, deps = deps)
exprs <- list(
fun,
path = path,
working_directory = working_directory,
args = args,
deps = deps
)
as.expression(as.call(exprs))
}

Expand All @@ -132,15 +141,15 @@ tar_knit_command <- function(path, args, quiet) {
#' and the relative path to the output `knitr` report.
#' The output path depends on the input path argument,
#' which has no default.
#' @param path Path to the `knitr` source file.
#' @inheritParams tar_knit
#' @param args A named list of arguments to `knitr::knit()`.
#' @param deps An unnamed list of target dependencies of the `knitr`
#' report, automatically created by `tar_knit()`.
tar_knit_run <- function(path, args, deps) {
tar_knit_run <- function(path, working_directory, args, deps) {
targets::tar_assert_package("knitr")
withr::local_options(list(crayon.enabled = NULL))
opt <- knitr::opts_knit$get("root.dir")
knitr::opts_knit$set(root.dir = getwd())
knitr::opts_knit$set(root.dir = working_directory %|||% getwd())
on.exit(knitr::opts_knit$set(root.dir = opt))
envir <- parent.frame()
args$envir <- args$envir %|||% targets::tar_envir(default = envir)
Expand Down
15 changes: 11 additions & 4 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,
working_directory = NULL,
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
error = targets::tar_option_get("error"),
Expand All @@ -72,15 +73,21 @@ tar_knit_raw <- function(
knit_arguments = quote(list())
) {
targets::tar_assert_package("knitr")
targets::tar_assert_scalar(path)
targets::tar_assert_chr(path)
targets::tar_assert_path(path)
targets::tar_assert_file(path)
targets::tar_assert_not_dirs(path)
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
targets::tar_assert_lang(knit_arguments)
targets::tar_assert_not_expr(knit_arguments)
targets::tar_target_raw(
name = name,
command = tar_knit_command(path, knit_arguments, quiet),
command = tar_knit_command(
path,
working_directory,
knit_arguments,
quiet
),
packages = packages,
library = library,
format = "file",
Expand Down
4 changes: 2 additions & 2 deletions R/tar_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' inform is_missing quo_squash
#' @importFrom targets tar_assert_chr tar_assert_dbl tar_assert_df
#' tar_assert_envir tar_assert_equal_lengths
#' tar_assert_expr tar_assert_flag
#' tar_assert_expr tar_assert_file tar_assert_flag
#' tar_assert_function tar_assert_ge tar_assert_identical
#' tar_assert_in tar_assert_not_dirs tar_assert_not_dir
#' tar_assert_not_in tar_assert_inherits tar_assert_int
Expand All @@ -37,7 +37,7 @@
#' last_col matches num_range one_of starts_with
#' @importFrom utils download.file globalVariables head
#' @importFrom vctrs vec_c vec_rbind
#' @importFrom withr local_envvar local_options with_options
#' @importFrom withr local_dir local_envvar local_options with_options
NULL

utils::globalVariables(".x")
2 changes: 2 additions & 0 deletions R/tar_quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
tar_quarto <- function(
name,
path = ".",
working_directory = NULL,
extra_files = character(0),
execute = TRUE,
execute_params = list(),
Expand Down Expand Up @@ -148,6 +149,7 @@ tar_quarto <- function(
tar_quarto_raw(
name = name,
path = path,
working_directory = working_directory,
extra_files = extra_files,
execute = execute,
execute_params = execute_params,
Expand Down
15 changes: 10 additions & 5 deletions R/tar_quarto_raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' @inheritSection tar_quarto Quarto troubleshooting
#' @inheritParams targets::tar_target_raw
#' @inheritParams quarto::quarto_render
#' @inheritParams tar_render
#' @param path Character of length 1,
#' either the single `*.qmd` source file to be rendered
#' or a directory containing a Quarto project.
Expand Down Expand Up @@ -108,6 +109,7 @@
tar_quarto_raw <- function(
name,
path = ".",
working_directory = NULL,
extra_files = character(0),
execute = TRUE,
execute_params = NULL,
Expand Down Expand Up @@ -135,10 +137,10 @@ tar_quarto_raw <- function(
targets::tar_assert_nzchar(name)
targets::tar_assert_chr(extra_files)
targets::tar_assert_nzchar(extra_files)
targets::tar_assert_scalar(path)
targets::tar_assert_chr(path)
targets::tar_assert_nzchar(path)
targets::tar_assert_path(path)
targets::tar_assert_file(path)
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
targets::tar_assert_scalar(execute)
targets::tar_assert_lgl(execute)
targets::tar_assert_lang(execute_params %|||% quote(x))
Expand Down Expand Up @@ -168,6 +170,7 @@ tar_quarto_raw <- function(
}
command <- tar_quarto_command(
path = path,
working_directory = working_directory,
sources = sources,
output = output,
input = input,
Expand Down Expand Up @@ -199,6 +202,7 @@ tar_quarto_raw <- function(

tar_quarto_command <- function(
path,
working_directory,
sources,
output,
input,
Expand All @@ -216,7 +220,7 @@ tar_quarto_command <- function(
input = path,
execute = execute,
execute_params = execute_params,
execute_dir = quote(getwd()),
execute_dir = execute_dir,
execute_daemon = 0,
execute_daemon_restart = FALSE,
execute_debug = FALSE,
Expand All @@ -230,6 +234,7 @@ tar_quarto_command <- function(
env = list(
path = path,
execute = execute,
execute_dir = working_directory %|||% quote(getwd()),
execute_params = execute_params,
cache = cache,
cache_refresh = cache_refresh,
Expand Down
2 changes: 2 additions & 0 deletions R/tar_quarto_rep.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
tar_quarto_rep <- function(
name,
path,
working_directory = NULL,
execute_params = data.frame(),
batches = NULL,
extra_files = character(0),
Expand Down Expand Up @@ -128,6 +129,7 @@ tar_quarto_rep <- function(
tar_quarto_rep_raw(
name = targets::tar_deparse_language(substitute(name)),
path = path,
working_directory = working_directory,
execute_params = execute_params,
batches = batches,
extra_files = extra_files,
Expand Down
14 changes: 9 additions & 5 deletions R/tar_quarto_rep_raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
tar_quarto_rep_raw <- function(
name,
path,
working_directory = NULL,
execute_params = expression(NULL),
batches = NULL,
extra_files = character(0),
Expand Down Expand Up @@ -137,11 +138,11 @@ tar_quarto_rep_raw <- function(
targets::tar_assert_scalar(name)
targets::tar_assert_chr(name)
targets::tar_assert_nzchar(name)
targets::tar_assert_scalar(path)
targets::tar_assert_chr(path)
targets::tar_assert_nzchar(path)
targets::tar_assert_path(path)
targets::tar_assert_not_dirs(path)
targets::tar_assert_file(path)
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
targets::tar_assert_lang(execute_params %|||% quote(x))
targets::tar_assert_dbl(batches %|||% 0L, "batches must be numeric.")
targets::tar_assert_scalar(batches %|||% 0L, "batches must have length 1.")
Expand Down Expand Up @@ -191,6 +192,7 @@ tar_quarto_rep_raw <- function(
command = tar_quarto_rep_command(
name = name,
path = path,
working_directory = working_directory,
extra_files = extra_files,
execute = execute,
cache = cache,
Expand Down Expand Up @@ -300,6 +302,7 @@ tar_quarto_rep_run_params <- function(
tar_quarto_rep_command <- function(
name,
path,
working_directory,
extra_files,
execute,
cache,
Expand All @@ -314,7 +317,7 @@ tar_quarto_rep_command <- function(
list(
input = path,
execute = execute,
execute_dir = quote(getwd()),
execute_dir = execute_dir,
execute_daemon = 0,
execute_daemon_restart = FALSE,
execute_debug = FALSE,
Expand All @@ -328,6 +331,7 @@ tar_quarto_rep_command <- function(
env = list(
path = path,
execute = execute,
execute_dir = working_directory %|||% quote(getwd()),
cache = cache,
cache_refresh = cache_refresh,
debug = debug,
Expand Down
20 changes: 16 additions & 4 deletions R/tar_render.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
#' @inheritParams rmarkdown::render
#' @param path Character string, file path to the R Markdown source file.
#' Must have length 1.
#' @param working_directory Optional character string,
#' path to the working directory
#' to temporarily set when running the report.
#' The default is `NULL`, which runs the report from the
#' current working directory at the time the pipeline is run.
#' This default is recommended in the vast majority of cases.
#' To use anything other than `NULL`, you must manually set a value
#' for the `store` argument in all calls to
#' `tar_read()` and `tar_load()` in the report. Otherwise,
#' these functions will not know where to find the data.
#' @param ... Named arguments to `rmarkdown::render()`.
#' These arguments are evaluated when the target actually runs in
#' `tar_make()`, not when the target is defined. That means, for
Expand Down Expand Up @@ -112,6 +122,7 @@
tar_render <- function(
name,
path,
working_directory = NULL,
tidy_eval = targets::tar_option_get("tidy_eval"),
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
Expand All @@ -128,9 +139,10 @@ tar_render <- function(
...
) {
targets::tar_assert_package("rmarkdown")
targets::tar_assert_scalar(path)
targets::tar_assert_chr(path)
targets::tar_assert_path(path)
targets::tar_assert_file(path)
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
envir <- tar_option_get("envir")
args <- targets::tar_tidy_eval(
substitute(list(...)),
Expand All @@ -139,7 +151,7 @@ tar_render <- function(
)
targets::tar_target_raw(
name = targets::tar_deparse_language(substitute(name)),
command = tar_render_command(path, args, quiet),
command = tar_render_command(path, working_directory, args, quiet),
packages = packages,
library = library,
format = "file",
Expand Down
20 changes: 13 additions & 7 deletions R/tar_render_raw.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
tar_render_raw <- function(
name,
path,
working_directory = NULL,
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
error = targets::tar_option_get("error"),
Expand All @@ -97,15 +98,20 @@ tar_render_raw <- function(
render_arguments = quote(list())
) {
targets::tar_assert_package("rmarkdown")
targets::tar_assert_scalar(path)
targets::tar_assert_chr(path)
targets::tar_assert_path(path)
targets::tar_assert_not_dirs(path)
targets::tar_assert_file(path)
if (!is.null(working_directory)) {
targets::tar_assert_file(working_directory)
}
targets::tar_assert_lang(render_arguments)
targets::tar_assert_not_expr(render_arguments)
targets::tar_target_raw(
name = name,
command = tar_render_command(path, render_arguments, quiet),
command = tar_render_command(
path,
working_directory,
render_arguments,
quiet
),
packages = packages,
library = library,
format = "file",
Expand All @@ -121,9 +127,9 @@ tar_render_raw <- function(
}


tar_render_command <- function(path, args, quiet) {
tar_render_command <- function(path, working_directory, args, quiet) {
args$input <- path
args$knit_root_dir <- quote(getwd())
args$knit_root_dir <- working_directory %|||% quote(getwd())
args$quiet <- quiet
deps <- call_list(as_symbols(knitr_deps(path)))
fun <- call_ns("tarchetypes", "tar_render_run")
Expand Down
Loading

0 comments on commit 939b342

Please sign in to comment.