| run_py_step | R Documentation |
Runs, inside a fresh R environment, an optional R pre-script, a Python script (via reticulate), and an optional R post-script, then returns either a converted R object or a character vector of files. This is the function the target built by tar_target_py() calls; it is exported so that call resolves at pipeline run time but this function is not destined to be called directly by the package users.
run_py_step(
script,
pre_script = NULL,
post_script = NULL,
inputs = list(),
output = "object",
retrieve = NULL,
files = NULL,
python_version = NULL,
env = NULL,
env_manager = "system",
python = NULL,
name = NULL
)
script |
Path to the Python script to run (required). Accepts a file path or an inline |
pre_script |
Optional path to an R script run before the Python script. It is evaluated in the step environment, which already holds the named |
post_script |
Optional path to an R script run after the Python script. It is evaluated in the same environment, which now also holds |
inputs |
Named list of upstream target values, bound by name into the step environment. Supplied automatically by the constructor. |
output |
Output mode: |
retrieve |
Optional character vector of foreign-session variable names to return when no |
files |
Optional character vector of file paths to return when no |
python_version |
Optional Python version to select (e.g. |
env, env_manager, python |
Python environment selection: the reproducible alternatives to |
name |
Character string, the step's target name. Supplied automatically by the constructor; used only to name this step's log files when |
The converted R object (object mode) or a character vector of normalised file paths (file mode).
A worker receives whatever the constructor already resolved, which is one of two things: a path to a file on disk, or an inline carrier built by tar_code() that holds the code in memory. Both are accepted, so a direct call may pass either.
tar_target_path() is deliberately not a third form at this level. It is a constructor-level convenience: tar_target_py() and the other constructors rewrite it while the pipeline's DAG is built, so that by the time a worker runs it has already become the ordinary file path held by the upstream target. Handing the result of tar_target_path() straight to a worker therefore does not resolve to a file. The three forms as written in _targets.R, and which of them tracks your edits, are covered in vignette("scripts").
run_jl_step(), tar_target_py()
# This worker binds a live Python interpreter, so it only runs when
# TARPOLYGLOT_EXAMPLES=true says a Python toolchain is available.
# tar_dir() runs the code in a temporary directory, so nothing is written
# to the working directory.
if (identical(Sys.getenv("TARPOLYGLOT_EXAMPLES"), "true")) {
targets::tar_dir({
# Normally invoked by tar_target_py(); shown here as a direct call.
writeLines("to_py <- list(x = x)", "pre.R")
writeLines("result = float(sum(x))", "sum.py")
run_py_step(
script = "sum.py",
pre_script = "pre.R",
inputs = list(x = c(1, 2, 3)),
retrieve = "result"
)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.