Create a Nix expression running an R function
Usage
rxp_r(
name,
expr,
additional_files = "",
nix_env = "default.nix",
serialize_function = NULL,
unserialize_function = NULL,
env_var = NULL
)
Arguments
- name
Symbol, name of the derivation.
- expr
R code to generate the expression.
- additional_files
Character vector, additional files to include. Custom functions must go into a script called "functions.R", and additional files that need to be accessible during the build process can be named anything.
- nix_env
Character, path to the Nix environment file, default is "default.nix".
- serialize_function
Function, defaults to NULL. A function used to serialize objects for transfer between derivations. It must accept two arguments: the object to serialize (first), and the target file path (second). If your function has a different signature, wrap it to match this interface. By default,
saveRDS()
is used, but this may yield unexpected results, especially for complex objects like machine learning models. For instance, for{keras}
models, usekeras::save_model_hdf5()
to capture the full model (architecture, weights, training config, optimizer state, etc.).- unserialize_function
Function, defaults to NULL. A function used to unserialize objects transferred between derivations. By default,
readRDS()
is used, but this may produce unexpected results with complex objects like machine learning models. For example, if the parent derivation usedkeras::save_model_hdf5()
to serialize a model, this derivation should usekeras::load_model_hdf5()
to load it correctly.- env_var
Character vector, defaults to NULL. A named vector of environment variables to set before running the R script, e.g.,
c("CMDSTAN" = "${defaultPkgs.cmdstan}/opt/cmdstan)"
. Each entry will be added as an export statement in the build phase.
Details
At a basic level, rxp_r(mtcars_am, filter(mtcars, am == 1))
is
equivalent to mtcars_am <- filter(mtcars, am == 1)
. rxp_r()
generates the
required Nix boilerplate to output a so-called "derivation" in Nix jargon.
A Nix derivation is a recipe that defines how to create an output (in this
case mtcars_am
) including its dependencies, build steps, and output
paths.
See also
Other derivations:
rxp_jl()
,
rxp_py()
,
rxp_py_file()
,
rxp_qmd()
,
rxp_r_file()
,
rxp_rmd()
Examples
if (FALSE) { # \dontrun{
# Basic usage
rxp_r(name = filtered_mtcars, expr = filter(mtcars, am == 1))
# Serialize object using qs
rxp_r(
name = filtered_mtcars,
expr = filter(mtcars, am == 1),
serialize_function = qs::qsave
)
# Unerialize using qs::qread in the next derivation
rxp_r(
name = mtcars_mpg,
expr = select(filtered_mtcars, mpg),
unserialize_function = qs::qread
)
} # }