如何正确地从 R 内部转义系统调用 [英] How to correctly escape system calls from inside R

查看:32
本文介绍了如何正确地从 R 内部转义系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中运行几个 shell 命令.

我已经尝试过 system() 但我还没有找到如何正确转义即使使用 shQuote.

# 工作正常system('ls -a -l')

但是我如何执行像 perl -e 'print "test\n"'curl --data-urlencode query@biomart.xml http://biomart.org 这样的命令/biomart/martservice/results 在 R 中?

更新:

对于像 perl 示例这样的命令,我不知道如何转义引号,因为它需要作为字符串引用但已经使用了两种类型的引号.

在 curl 的情况下,问题似乎出在 RESTful 调用中,以使用 @ 在 shell 中工作但不在 system() 调用中传递 xml

dat <-system('curl --data-urlencode query@biomart.xml http://biomart.org/biomart/martservice/results', intern=F)

<块引用>

警告:无法从文件query@biomart.xml"读取数据,这会导致一个空的警告:发布.

该文件是 biomart.xml 而不是 query@biomart.xml

** 更新 2**

我用于测试的 xml 文件是:

解决方案

R 中的字符串可以用单引号 (') 或双引号 (") 括起来.

如果要同时执行单引号和双引号的命令,例如:

perl -e 'print "test\n"'

那么您为 R 字符串选择的结果无关紧要 - 因为无论哪种方式都需要转义一对.

假设您选择单引号:

system('')

然后我们需要以与换行符相同的方式对单引号进行转义,使用转义符\:

command <- 'perl -e \'print "test\n"\''系统(命令)

也可以使用 \Unnnnnnnn\unnnn 以这种方式对 Unicode 字符进行编码.或者使用八进制 (\nnn) 或十六进制 (\xnnn).

因此:

atSymbol <- '\u0040' # '\x040' '\100'

如果 curl 命令中的 @ 导致了问题,像这样编码应该可以解决问题.

I have several shell commands that I want to run in in R.

I have tried system() but I have not find out how to do the right escaping even using shQuote.

# works OK
system('ls -a -l')

but how I execute a command like perl -e 'print "test\n"' or curl --data-urlencode query@biomart.xml http://biomart.org/biomart/martservice/results inside R?

update:

In the case of commands like the perl example I do not know how to do the escaping of quotes as it needs to be quoted as string but already use both types of quotes.

In the case of curl, the problem seems to be in the RESTful call to pass the xml with the @ that works in the shell but not in the system() call

dat <-system('curl  --data-urlencode query@biomart.xml http://biomart.org/biomart/martservice/results', intern=F)

Warning: Couldn't read data from file "query@biomart.xml", this makes an empty Warning: POST.

The file is biomart.xml not query@biomart.xml

** Update2**

The xml file I am using for test is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Query>
<Query virtualSchemaName = "default" formatter = "TSV" header = "0" uniqueRows = "0" count = "" datasetConfigVersion = "0.6" >

<Dataset name = "hsapiens_gene_ensembl" interface = "default" >
  <Filter name = "hgnc_symbol" value = "LDLR"/>
  <Attribute name = "external_gene_id" />

</Dataset>
</Query>

解决方案

Strings in R may be enclosed in either single (') or double (") quotes.

If you want to execute a command with both single and double quotes, such as:

perl -e 'print "test\n"'

then it is of little consequence which you choose for your R string - since one pair needs to be escaped either way.

Let's say you choose single quotes:

system('')

Then we need to escape the single quotes in the same way as for the newline character, with the escape character, \:

command <- 'perl -e \'print "test\n"\''
system(command)

It is also possible to encode Unicode characters in this way with \Unnnnnnnn or \unnnn. Alternatively with octal (\nnn), or hex (\xnnn).

Thus:

atSymbol <- '\u0040' # '\x040' '\100'

If the @ in your curl command is causing the problem, encoding it like this should fix it.

这篇关于如何正确地从 R 内部转义系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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