R语言 env参数在system2中不起作用?

vkc1a9a2  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(169)

我尝试使用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
2w2cym1i

2w2cym1i1#

由于进入shell的命令只是paste(c(env, shQuote(command), args), collapse = " "),因此您可以执行以下操作:

system2("echo", args = "$X", env = c("X=foobar;"))

相关问题