[Rd] Possibly broken system2 env-option

Gábor Csárdi c@@rd|@g@bor @end|ng |rom gm@||@com
Tue Mar 19 10:48:16 CET 2019


Probably not broken, just hard to get the quoting right? Or it does
not work with echo?

It is surely hard to get it right, I have been trying for 15 minutes.I
am pretty sure that the correct form is env = "VAR='Hello-World'" but
spaces and other special characters might cause trouble. E.g. this
works, but only without spaces (px is an internal tool for testing,
that can get an env var, and it is portable):

❯ px <- processx:::get_tool("px")
❯ system2(px, c("getenv", "VAR"), env = "VAR=foo")
foo

❯ system2(px, c("getenv", "VAR"), env = c(VAR="Hello World"))
sh: Hello: command not found
Warning message:
In system2(px, c("getenv", "VAR"), env = c(VAR = "Hello World")) :
  error in running command

Somebody with more system2() experience can probably fix this, but
here is an alternative, using the processx package:

❯ processx::run(px, c("getenv", "VAR"), env = c(Sys.getenv(), VAR =
"Hello World"))
$status
[1] 0

$stdout
[1] "Hello World\n"

$stderr
[1] ""

$timeout
[1] FALSE

For processx, you don't need to quote anything (i.e. command,
arguments or env vars), because it does not
use an intermediate shell.

Disclaimer: I am an author of processx.

Gabor

On Tue, Mar 19, 2019 at 9:28 AM Henning Bredel <h.bredel using gmx.de> wrote:
>
> Hey all,
>
> what is wrong with this command:
>
>   system2("echo", env = c(VAR = "Hello World"), args = c("$VAR"))
>
> I am a bit confused, as help("system2") writes about the env option:
>
> > character vector of name=value strings to set environment variables.
>
> Is this option buggy, or am I using it just wrong?
>
> Thanks for your help
>
>   Henning
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list