在系统中使用多个命令从R调用外部程序 [英] Calling external program from R with multiple commands in system

查看:178
本文介绍了在系统中使用多个命令从R调用外部程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新编程,主要是我能够在R中做一些脚本,但对于我的工作,我需要调用一个外部程序。对于这个程序在ubuntu的终端上工作,我必须先使用setenv然后执行程序。 Googling我发现了system()和Sys.setenv()函数,但不幸的是我可以使它的函数。



这是在ubuntu工作的代码终端:

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


b $ b

其中前两个参数是输入文件,-o参数是输出目录,-comp是程序运行的另一个参数。



我需要在R中做它的原因,尽管它已经在终端中工作是因为我需要运行程序1000次与1000个不同的文件,所以我想做一个for循环,输入名称在每个循环中更改,然后分析R中的每个输出。



我已经尝试使用:

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


 home / meme / bin)&& mast/home/meme/meme.txt/home/meme/seqs.txt-o/ home / meme / output-comp)

但总是收到:

 错误:system(mast/home/meme/meme.txt



 错误:system(Sys.setenv(PATH =/ home / meme / bin)中的意外符号&&&&&&&&&&&&&&  /home/meme/meme.txt

如果这已经被回答了,那么我的Google搜索刚刚很差,我很感激任何链接到它的响应。



谢谢



Carlos



其他详情:



我使用Ubuntu 12.04 64位版本,RStudio版本0.97.551,R版本3.0.2(2013-09-25) - Frisbee Sailing平台:x86_64-pc-linux-gnu位)。
我使用的程序(MAST)在字母列表中查找序列模式,并且是 http://meme.nbcr.net/meme/doc/meme-install.html ,并通过命令行运行。 mast的命令行用法是:

  mast< motif文件> < sequence file> [options] 


解决方案

构造要执行的字符串粘贴并将其添加到系统

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

注意使用 sep =''停止粘贴在字符串中加入空格,并反引号引用标记以保留空格。

使用 print(cmd)而不是系统(cmd)运行之前测试以确保您获得正确的命令。可能会:

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

并设置 TESTING = TRUE FALSE 在R中运行。



如果你要对系统运行多个shell命令 call,最好将它们全部放在一个shell脚本文件中,并从R传递参数。

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

然后 dojob.sh 是一个解析args的shell脚本。你需要学习一些shell脚本。


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]

解决方案

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.

这篇关于在系统中使用多个命令从R调用外部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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