调用系统命令并将变量作为参数传递给管道 [英] Invoke a system command and pipe a variable as an argument

查看:62
本文介绍了调用系统命令并将变量作为参数传递给管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆