Home

Awesome

<!-- README.md is generated from README.Rmd. Please edit that file -->

cryogenic <img src="man/figures/logo.png" align="right" height=230/>

<!-- badges: start -->

Lifecycle:
experimental R build
status

<!-- badges: end -->

cryogenic is a package for freezing a function call, modifying its arguments and then evaluating later.

This package is written for a particular use case I have in mind, and is not trying to be the comprehensive answer to call manipulation in R.

My Use Case

What’s in the box

Installation

You can install from GitHub with:

# install.package('remotes')
remotes::install_github('coolbutuseless/cryogenic')

Simple example 1

<!-- end list -->
library(cryogenic)

cc <- capture_call(runif(n = 10, min=-3))
cc
#> runif(n = 10, min = -3)
cc <- modify_call(cc, defaults = list(min=0, max=5), update = list(n = 5))
cc
#> runif(n = 5, min = -3, max = 5)
evaluate_call(cc)
#> [1] -0.8759307 -0.0230088  1.5828269  4.2656623 -1.3865446

Simple example 2

<!-- end list -->
library(cryogenic)

cc <- capture_call(runif(n = 10, min=-3))
cc
#> runif(n = 10, min = -3)
evaluate_call(cc, defaults = list(min=0, max=5), update = list(n = 15))
#>  [1]  4.18711748  4.55740215  2.28638234  2.03291235 -2.50570984 -1.35220340
#>  [7] -1.58754598  2.49618277  0.07282975  3.15873136  0.98159394  2.74094807
#> [13]  4.93524876  0.04028144  3.21956177

Simple example 3

library(cryogenic)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Capture a call as-is.
#
# * the function does not have to exist in the capturing environment
# * By default, all arguments are eagerly evaluated in the environment in which
#   the call was captured
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc <- capture_call(farnarkle(6, x = 2, y = 3, z = 2+9, error = TRUE))
cc
#> farnarkle(6, x = 2, y = 3, z = 11, error = TRUE)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Update the arguments to the call
#
# * 'defaults' are only added to the arguments if the named argument does 
#    not already exist there.
# * 'update' values are added to the arguments unconditionally
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc <- modify_call(
  cc, 
  defaults = list(x = NULL, y = 7), 
  update   = list(z = NULL, na.rm = TRUE),
  delete   = c('error')
)

cc
#> farnarkle(6, x = 2, y = 3, z = NULL, na.rm = TRUE)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 'farnarkle' is not yet defined, so evaluation should fail
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
evaluate_call(cc)
#> Error in farnarkle(6, x = 2, y = 3, z = NULL, na.rm = TRUE): could not find function "farnarkle"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# define farnarkle and it should work
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
farnarkle <- function(...) {print("Hello #Rstats")}
evaluate_call(cc)
#> [1] "Hello #Rstats"

Related Software

Acknowledgements