我尝试使用system2
运行一个命令,并使用env
参数设置一个环境变量,但它似乎没有设置。我在Linux上,所以这应该工作[在帮助文件中提到env
有时在Windows中不工作]。
有人知道如何在system32
中设置环境变量吗?
例如
system2('echo', args='foobar')
# foobar # as expected
system2('echo', args='$X', env="X=foobar")
# # <-- nothing is printed. as if I had just done `echo`
# I expect to see 'foobar' echoed.
# pretty sure the above environment specification is right, but just in case
system2('echo', args='$X', env=c(X='foobar'))
# sh: 1: foobar: not found
# just to show it can work
system('X=foobar; echo $X')
# foobar
现在我将使用system
作为一种解决方法,但是system
帮助文件一直在讨论我应该如何使用system2
(如果这是一个bug,我应该将它归档到哪里?)
> sessionInfo()
R version 3.2.5 (2016-04-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)
locale:
[1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8
[5] LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8
[7] LC_PAPER=en_AU.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
1条答案
按热度按时间2w2cym1i1#
由于进入shell的命令只是
paste(c(env, shQuote(command), args), collapse = " ")
,因此您可以执行以下操作: