Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting environment variables programmatically

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.

like image 532
flodel Avatar asked Sep 21 '12 15:09

flodel


People also ask

How do I set environment variables in runtime?

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.

How do I set environment variables in registry?

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".


1 Answers

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)
like image 186
David Robinson Avatar answered Sep 18 '22 08:09

David Robinson