调用系统命令并将变量作为参数传递给管道 [英] Invoke a system command and pipe a variable as an argument
问题描述
我正在尝试使用 system2
函数传递系统命令,但我想将变量作为参数传递给该函数.
I'm trying to pass a system command, using the system2
function, but I want to pass a variable as an argument to the function.
更具体地说,我有一个bash命令 xml-to-json
,我想从R上运行一个包含XML的对象.
More specifically I have a bash command, xml-to-json
, that I want to run from R on an object containing XML.
我可以将此数据写入文件,然后第二次运行 system2
,但是我想尽可能地简化.
I could write this data to file and then run system2
a second time, but I'd like to keep this as parsimonious as possible.
推荐答案
system2
的 input
自变量可以满足您的要求:
The input
argument to system2
does what you want:
输入:如果提供了字符向量,则将其复制一个字符串每行到一个临时文件,以及标准输入命令"被重定向到文件.
input: if a character vector is supplied, this is copied one string per line to a temporary file, and the standard input of ‘command’ is redirected to the file.
示例:
foo = c("foo", "bar")
result = system2("cat", input = foo, stdout = TRUE)
这篇关于调用系统命令并将变量作为参数传递给管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!