I'm developing an R package, I'd like the behavior of one of my function depends on an Environment Variable ENV_VAR
, whose default value is foo
, while users can also change this ENV_VAR
at runtime using Sys.setenv(ENV_VAR = 'bar')
. I tried this
Sys.setenv(ENV_VAR = 'foo')
#' @export
my_funciton <- function(){
v <- Sys.getenv(ENV_VAR)
if (v == 'foo') ... else if (v == 'bar') ...
}
but when i build and reload the package in RStudio, I run Sys.getenv(ENV_VAR)
gives ""
, i.e, when loading the package, it did not set environment ENV_VAR
as foo
. Predictably my_function
also raise error: Error in Sys.getenv(ENV_VAR) : object 'ENV_VAR' not found