Problem description
Sys.setenv
does not have an easy interface to supply LHS (the env var name) as a parameter. If one wants to dynamically define what env var should be set, then metaprogramming approach is required.
Base R way
This small helper function works as expected.
setenv = function(var, value, quiet=TRUE) {
stopifnot(is.character(var), !is.na(var), length(value)==1L, is.atomic(value))
qc = as.call(c(list(quote(Sys.setenv)), setNames(list(value), var)))
if (!quiet) print(qc)
eval(qc)
}
var_name = "RISCOOL"
Sys.getenv(var_name)
#[1] ""
setenv(var_name, value=150, quiet=FALSE)
#Sys.setenv(RISCOOL = 150)
Sys.getenv(var_name)
#[1] "150"
Question
The question is about how the problem can be solved using packages like pryr
or rlang
(tidyeval
)? or eventually another popular one.
I don't know these packages at all and would like to get better understanding how they could simplify my metaprogramming code.
Note that question is about metaprogramming, setting env var is just an example.