Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling external program from R with multiple commands in system

I am new to programming and mainly I am able to do some scripts within R, but for my work I need to call an external program. For this program to work on the ubuntu's terminal I have to first use setenv and then execute the program. Googling I've found the system () and Sys.setenv() functions, but unfortunately I can make it function.

This is the code that does work in the ubuntu terminal:

$ export PATH=/home/meme/bin:$PATH
$ mast "/home/meme/meme.txt" "/home/meme/seqs.txt" -o "/home/meme/output" -comp

Where the first two arguments are input files, the -o argument is the output directory and the -comp is another parameter for the program to run.

The reason that I need to do it in R despite it already works in the terminal is because I need to run the program 1000 times with 1000 different files so I want to make a for loop where the input name changes in every loop and then analyze every output in R.

I have already tried to use:

Sys.setenv(PATH="/home/meme/bin"); system(mast "/home/meme/meme.txt" "/home/meme/seqs.txt" -o "/home/meme/output" -comp )

and

system(Sys.setenv(PATH="/home/meme/bin") && mast "/home/meme/meme.txt" "/home/meme/seqs.txt" -o "/home/meme/output" -comp )

but always received:

Error: unexpected constant string in "system(mast "/home/meme/meme.txt""

or

Error: unexpected symbol in "system(Sys.setenv(PATH="/home/meme/bin") && mast "/home/meme/meme.txt""

At this point I have run out of ideas to make this work. If this has already been answered, then my googling have just been poor and I would appreciate any links to its response.

Thank you very much for your time.

Carlos

Additional details:

I use Ubuntu 12.04 64-bits version, RStudio version 0.97.551, R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Platform: x86_64-pc-linux-gnu (64-bit). The program I use (MAST) finds a sequence pattern in a list of letters and is part of the MEME SUIT version 4.9.1 found in http://meme.nbcr.net/meme/doc/meme-install.html and run through command line. The command-line usage for mast is:

mast    <motif file> <sequence file> [options]
like image 958
user3342784 Avatar asked Feb 23 '14 09:02

user3342784


1 Answers

Construct the string you want to execute with paste and feed that to system:

for(i in 1:10){
cmd=paste("export FOO=",i," ; echo \"$FOO\" ",sep='')
system(cmd)
}

Note the use of sep='' to stop paste putting spaces in, and back-quoting quote marks in the string to preserve them.

Test before running by using print(cmd) instead of system(cmd) to make sure you are getting the right command built. Maybe do:

if(TESTING){print(cmd)}else{system(cmd)}

and set TESTING=TRUE or FALSE in R before running.

If you are going to be running more than one shell command per system call, it might be better to put them all in one shell script file and call that instead, passing parameters from R. Something like:

cmd = paste("/home/me/bin/dojob.sh ",i,i+1)
system(cmd)

and then dojob.sh is a shell script that parses the args. You'll need to learn a bit more shell scripting.

like image 136
Spacedman Avatar answered Sep 29 '22 19:09

Spacedman