| evalwith | R Documentation |
Evaluates an expression with a predefined global state, including the:
working directory (set via setwd())
global options (set via options())
graphical parameters (set via par())
In addition to that, evalwith allows to:
Redirect or capture the output and/or message stream via sink()
Measure the runtime of the evaluated expression via system.time()
Creating a temporary test directory (inside tmpdir()) and populating it
with input files according to inputs
Predefine answers for calls to readline() happening during evaluation of
expr
Caching the result of the expression
All changes to the global state are reverted after the expression has been evaluated.
evalwith(
expr,
testdir = NULL,
answers = NULL,
output = NULL,
message = NULL,
plot = NULL,
datadir_temp = c("default", "missing", "empty", "filled")[1],
datadir_persistent = c("default", "missing", "empty", "filled")[1],
inputs = character(),
opts = NULL,
pars = NULL,
cache = FALSE,
overwrite = FALSE
)
expr |
Expression to be evaluated. |
testdir |
ID of the test directory. E.g. |
answers |
Answers to be returned by readline(). |
output |
Path to the file where output stream should be redirected to.
Use |
message |
Path to the file where message stream be redirected to. Use
|
plot |
An expression opening a device, the string "captured" or a path
ending in ".pdf", ".svg", or ".png". Examples: |
datadir_temp |
State of the mocked temporary data directory. See details section. |
datadir_persistent |
State of the mocked persistent data directory. See details section. |
inputs |
Paths to be copied to the test directory before evaluating
|
opts |
Named list of options to be set. See |
pars |
Named list of parameters to be set. See |
cache |
Logical indicating whether to cache the result of the expression. |
overwrite |
Logical indicating whether to overwrite the cache file if it already exists. |
The datadir_temp and datadir_persistent arguments accept values
"missing", "filled" and "empty". Setting a value unequal NULL causes the
functions datadir_temp() and/or datadir_persistent() to be replaced with
mock functions pointing to fake directories. Functions depending on these
functions will then use the fake directories instead of the real ones. When
set to "missing" the returned mock directory does not exist. When set to
"empty" it exists and is guaranteed to be empty. When set to "filled", it is
populated with example datasets.
Attention: the mocked functions, i.e. datadir_temp() and
datadir_persistent() cannot be used directly inside expr when called via
devtools::test(). I'm not sure why, but it seems as if devtools and/or
testthat have their own copies of the functions which are used when the
expression is evaluated.
A list containing with following elements:
rv: The return value of the expression.
runtime: The "elapsed" runtime of the expression in seconds. Measured
with system.time().
output: The captured output.
message: The captured messages.
plot: The path to the saved plot.
testdir: The path to the test directory.
inputs: The paths to the copied input files.
2024-2025 Tobias Schmidt: initial version.
x1 <- evalwith(output = "captured", cat("Helloworld\n"))
str(x1)
x2 <- evalwith(datadir_persistent = "missing", message = "captured", datadir())
str(x2)
x3 <- evalwith(testdir = "dummy", inputs = "bruker/urine/urine_1", dir())
str(x3)
x4 <- evalwith(Sys.sleep(0.02))
str(x4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.