Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions R/qenv-within.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#' `within()` is a convenience method that wraps `eval_code` to provide a simplified way of passing expression.
#' `within` accepts only inline expressions (both simple and compound) and allows to substitute `expr`
#' with `...` named argument values.
#' Functions that trigger side effects like `options` or `set.seed` cannot be linked to specific objects with
#' tag "`# @linksto <object name>`" as expressions ignore comments. This is relevant when we want
#' to reproduce the code with
#' function `get_code`. For such scenarios, we recommend to use `eval_code` instead.
Comment thread
osenan marked this conversation as resolved.
Outdated
#'
Comment thread
osenan marked this conversation as resolved.
Outdated
#' @section Using language objects with `within`:
#' Passing language objects to `expr` is generally not intended but can be achieved with `do.call`.
Expand Down
4 changes: 4 additions & 0 deletions man/within.qenv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vignettes/qenv.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ if (interactive()) {
}
```

### Reproducibility

Code inside a `qenv` can be reproduced by calling function `get_code`
Comment thread
osenan marked this conversation as resolved.
Outdated
```{r, get_code}
q_reproducible <- qenv()
q_reproducible <- within(q_reproducible, {
a <- 2
b <- 5
c <- a + b
})
cat(get_code(q_reproducible))
cat(get_code(q_reproducible, names = "a"))
cat(get_code(q_reproducible, names = "c"))
```

As shown, we can get the code for specific objects by referencing the name of the object with the `names` argument
in `get_code`.
Comment thread
osenan marked this conversation as resolved.
Outdated
In the scenarios where we have specific objects that are affected by side effects of previous calls, it can be
passed to the qenv by adding # @linksto + the name of the object that is linked. This is important for setting options
or controlling random number generation.
Comment thread
osenan marked this conversation as resolved.
Outdated

```{r, get_code_linked}
q_linked <- qenv()
q_linked <- eval_code(q_reproducible, "
set.seed(2) # @linksto a
a <- runif(1)
")
cat(get_code(q_linked))
cat(get_code(q_linked, names = "a"))
```
Linking of objects for reproducibility is currently only supported by `eval_code`. As `within` uses expression as input
and ignores comments, it is not recommended in the cases where side effects functions are required for reproducibility.
Comment thread
osenan marked this conversation as resolved.
Outdated

## `qenv` and `teal` applications

Expand Down
Loading