knitr
language engine that runs targets
code chunks in Target Markdown.
Value
Character, output generated from knitr::engine_output()
.
Target Markdown interactive mode
Target Markdown has two modes:
Non-interactive mode. This is the default when you run
knitr::knit()
orrmarkdown::render()
. Here, the code intargets
code chunks gets written to special script files in order to set up atargets
pipeline to run later.Interactive mode: here, no scripts are written to set up a pipeline. Rather, the globals or targets in question are run in the current environment and the values are assigned to that environment.
The mode is interactive if !isTRUE(getOption("knitr.in.progress"))
,
is TRUE
. The knitr.in.progress
option is TRUE
when you run knitr::knit()
or rmarkdown::render()
and NULL
if you are running one chunk at a time interactively
in an integrated development environment, e.g. the
notebook interface in RStudio:
https://bookdown.org/yihui/rmarkdown/notebook.html.
You can choose the mode with the tar_interactive
chunk option.
(In targets
0.6.0, tar_interactive
defaults to interactive()
instead of !isTRUE(getOption("knitr.in.progress"))
.)
Target Markdown chunk options
Target Markdown introduces the following knitr
code chunk options.
Most other standard knitr
code chunk options should just work
in non-interactive mode. In interactive mode, not all
tar_globals
: Logical of length 1, whether to define globals or targets. IfTRUE
, the chunk code defines functions, objects, and options common to all the targets. IfFALSE
orNULL
(default), then the chunk returns formal targets for the pipeline.tar_interactive
: Logical of length 1, whether to run in interactive mode or non-interactive mode. See the "Target Markdown interactive mode" section of this help file for details.tar_name
: name to use for writing helper script files (e.g._targets_r/targets/target_script.R
) and specifying target names if thetar_simple
chunk option isTRUE
. All helper scripts and target names must have unique names, so please do not set this option globally withknitr::opts_chunk$set()
.tar_script
: Character of length 1, where to write the target script file in non-interactive mode. Most users can skip this option and stick with the default_targets.R
script path. Helper script files are always written next to the target script in a folder with an"_r"
suffix. Thetar_script
path must either be absolute or be relative to the project root (where you calltar_make()
or similar). If not specified, the target script path defaults totar_config_get("script")
(default:_targets.R
; helpers default:_targets_r/
). When you runtar_make()
etc. with a non-default target script, you must select the correct target script file either with thescript
argument or withtar_config_set(script = ...)
. The function willsource()
the script file from the current working directory (i.e. withchdir = FALSE
insource()
).tar_simple
: Logical of length 1. Set toTRUE
to define a single target with a simplified interface. In code chunks withtar_simple
equal toTRUE
, the chunk label (or thetar_name
chunk option if you set it) becomes the name, and the chunk code becomes the command. In other words, a code chunk with labeltargetname
and commandmycommand()
automatically gets converted totar_target(name = targetname, command = mycommand())
. All other arguments oftar_target()
remain at their default values (configurable withtar_option_set()
in atar_globals = TRUE
chunk).
See also
https://books.ropensci.org/targets/literate-programming.html
Other Target Markdown:
tar_interactive()
,
tar_noninteractive()
,
tar_toggle()
Examples
if (identical(Sys.getenv("TAR_EXAMPLES"), "true")) { # for CRAN
# Register the engine.
if (requireNamespace("knitr", quietly = TRUE)) {
knitr::knit_engines$set(targets = targets::tar_engine_knitr)
}
# Then, `targets` code chunks in a knitr report will run
# as described at
# <https://books.ropensci.org/targets/literate-programming.html>.
}