Incomplete sheet
This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.
Dynamic evaluation
Variables
| Action |
Code |
Details |
|
Generate valid variable names from string(s)
|
|
|
|
Generate unique variable names from string(s)
|
|
|
|
Check if variable exists
|
|
|
|
Get variable name from expression
|
deparse(substitute(expr))
|
|
|
Fill in variable values (substitute) in expression
|
|
Useful when expression is called in another function |
|
Force evaluation of variable
|
|
|
Environments
| Action |
Code |
Details |
|
Create new environment, inherit from global
|
|
|
|
Create new environment without parent
|
new.env(parent = emptyenv())
|
Technically has a parent, but inherits no variables |
|
Check if variable exists in environment
|
|
|
|
Get current environment
|
|
|
|
Get parent environment
|
|
|
|
Get package environment
|
|
|
|
Find environment of variable
|
|
|
|
Get value of variable in environment
|
|
|
|
Try get value of variable, with default return
|
get0('var', envir=env, ifnotfound='default value')
|
|
Dynamic evaluation
| Action |
Code |
Details |
|
Create call
|
|
|
|
Create expression
|
|
|
|
Create expression from string
|
expr = parse(text='A = 1')
|
|
|
Evaluate call
|
|
|
|
Substitute a call
|
do.call(substitute, list(CALL, env=ENV))
|
where CALL is stored in a variable |
|
Evaluate expression
|
|
|
|
Evaluate expression with specified values for (undefined) variables
|
a = 1
q = quote(a + b)
eval(q, list(b=3))
|
|
|
Get expression as string
|
deparse(expr, width.cutoff=500)
|
|
|
Force evaluation of variable
|
|
|
|
Lazy-evaluate a variable assignment through expression
|
delayedAssign('x', 2 + 2)
|
|
|
Call inline function
|
|
|
|
Call function with some arguments forced
|
|
|
|
Call function with arguments in list
|
|
|
|
Call function with arguments in list, ignoring unused
|
R.utils::doCall(fun, n=100, args=env)
|
|
Chained function evaluation
Also referred to as piping. Snippets use the magrittr package
| Action |
Code |
Details |
|
Chain functions
|
y = x %>% table %>% prop.table
|
|
|
Chain function calls, using the former input as a later argument
|
z = data.table(…) %>% .[, mean(x)]
|
call must be placed in brackets for embedded function calls |
|
Call consecutive function, but return the former value
|
y = x %>% table %T>% print
|
|
|
Call functions and update the LHS
|
|
|
Functions
| Action |
Code |
Details |
|
Get all arguments, including defaults
|
mget(names(formals()),sys.frame(sys.nframe()))
|
|
|
Get all specified arguments, including ellipsis
|
as.list(match.call()[-1])
|
|
|
Pass all specified arguments to a nested function
|
|
|
|
Pass all specified arguments to a nested function
|
call = match.call()
call$extra = 'test'
eval(call)
|
|
|
Pass all specified arguments to a nested function
|
call = match.call()
call[[1]] = as.symbol('newFunction')
eval(call)
|
|
|
Get name of the parent calling function
|
parentCall = sys.calls()[[sys.nframe()-2]]
as.character(parentCall[[1]])
|
|