0

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

Jia Ru
  • 93
  • 6
  • Probably you want to look at either `.onLoad` or `.onAttach` (and the corresponding cleanup versions so you can reset things if your package is detached/unloaded). – joran Jun 12 '17 at 03:16

1 Answers1

0

just as @joran commented, .onLoad function is what I need.

Jia Ru
  • 93
  • 6