R and System calls
You need to issue all commands in one system()
call:
system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" "))
You should already be in your working directory, so I'm not sure the cd getwd()
is necessary. And you may need quotes around your path because it contains spaces. The error may be resolved by putting spaces around >
.
If I were in your shoes, I would try this:
system("bgame -y 2010 2010bos.eva > 2010bos.txt")
UPDATE:
And you should probably heed this advice in the "Differences between Unix and Windows" section of ?system
that says you should use shell
:
• The most important difference is that on a Unix-alike
‘system’ launches a shell which then runs ‘command’. On
Windows the command is run directly - use ‘shell’ for an
interface which runs ‘command’ _via_ a shell (by default the
Windows shell ‘cmd.exe’, which has many differences from the
POSIX shell).
This means that it cannot be assumed that redirection or
piping will work in ‘system’ (redirection sometimes does, but
we have seen cases where it stopped working after a Windows
security patch), and ‘system2’ (or ‘shell’) must be used on
Windows.
R parallel system call on files
It doesnt' matter if you use variables or system
. Assuming you're not on Windows (which doesn't support parallel), on any decent system you can run
parallel::mclapply(Sys.glob("*.NEF"),
function(fn) system(paste("dcraw.exe -4 -T", shQuote(fn))),
mc.cores=8, mc.preschedule=F)
It will run 8 jobs in parallel. But then you may as well not use R and use instead
ls *.NEF | parallel -u -j8 'dcraw.exe -4 -T {}'
instead (using GNU parallel).
Listing files quickly using system calls
You should really put a quote around the entire command, also in case you want to save the output into an R object/vector, you may use an option called intern with "TRUE" value like below:
system('find "/Users/pradeepkumar" -name "*.R"',intern=T)
Output received:
> system('find "/Users/pradeepkumar" -name "*.R"',intern=T)
[1] "/Users/pradeepkumar/Desktop/company doc/g/code/SIPC-10.R"
[2] "/Users/pradeepkumar/Desktop/company doc/g/code/SIPC-2.R"
[3] "/Users/pradeepkumar/Desktop/company doc/g/code/SIPC-3.R"
Capture both exit status and output from a system call in R
As of R 2.15, system2
will give the return value as an attribute when stdout
and/or stderr
are TRUE. This makes it easy to get the text output and return value.
In this example, ret
ends up being a string with an attribute "status"
:
> ret <- system2("ls","xx", stdout=TRUE, stderr=TRUE)
Warning message:
running command ''ls' xx 2>&1' had status 1
> ret
[1] "ls: xx: No such file or directory"
attr(,"status")
[1] 1
> attr(ret, "status")
[1] 1
How to correctly escape system calls from inside R
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.
having R print a system call that contains , '', and escape character \
Hey I haven't tested your particular perl
call (since it involves particular file/directory etc) but tried something trivial by escaping the quotes and it seems to work. You might want to refer this question for more as well.
My approach,
# shouldnt have any text expect for an empty string
my_text <- try(system(" perl -e 'print \"\n\"' ", intern = TRUE))
my_text
[1] ""
# should contain the string - Hello perl from R!
my_text2 <- try(system(" perl -e 'print \"Hello perl from R!\"' ", intern = TRUE))
my_text2
[1] "Hello perl from R!"
So based on the above trials I think this should work for you -
try(system(command = "perl -pe '/^>/ ? print \"\n\" : chomp' in.fasta | tail -n +2 > out.fasta", intern = TRUE))
Note - intern = TRUE
just captures the output as a character vector in R.
Related Topics
Cluster One-Dimensional Data Optimally
Replacing Occurrences of a Number in Multiple Columns of Data Frame with Another Value in R
R Draws Plots with Rectangles Instead of Text
Installing R Gsl Package on Ubuntu
Convert Character to Date *Quickly* in R
R: Ggplot2, How to Set the Plot Title to Wrap Around and Shrink the Text to Fit the Plot
Error in Model.Frame.Default: Variable Lengths Differ
Changing the Line Type in the Ggplot Legend
Mutate Multiple Columns in a Dataframe
R Strsplit with Multiple Unordered Split Arguments
How to Format Axis Labels with Exponents with Ggplot2 and Scales
Saving a Graph with Ggsave After Using Ggplot_Build and Ggplot_Gtable
Importing Two Functions with Same Name Using Roxygen2
Understanding Dates and Plotting a Histogram with Ggplot2 in R