Pablo Marin-Garcia Pablo Marin-Garcia - 1 year ago 63
R Question

How to correctly escape system calls from inside R

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"'
curl --data-urlencode query@biomart.xml
inside R?


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', 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" />


Answer Source

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:


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"\''

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


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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download