In R, I can set environment variables "manually", for example:
Sys.setenv(TODAY = "Friday")
But what if the environment variable name and value are stored in R objects?
var.name <- "TODAY"
var.value <- "Friday"
I wrote this:
expr <- paste("Sys.setenv(", var.name, " = '", var.value, "')", sep = "")
expr
# [1] "Sys.setenv(TODAY = 'Friday')"
eval(parse(text = expr))
which does work:
Sys.getenv("TODAY")
# 1] "Friday"
but I find it quite ugly. Is there a better way? Thank you.
Log in to the Process Admin Console, and then click Installed Apps to show the list of current snapshots on the server. Click the snapshot that you want to work with. From the menu bar, click Environment Vars. For the variables listed, provide a value or ensure that the value shown is accurate for the current server.
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment".
You can use do.call
to call the function with that named argument:
args = list(var.value)
names(args) = var.name
do.call(Sys.setenv, args)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With