2011-04-21 47 views
13

我已经在过去使用过R来对命令行进行非常基本的调用。该示例可以在here找到。R和系统调用

这一次,我希望模仿这个代码在Windows的命令行运行成功:

> cd C:\Documents and Settings\BTIBERT\My Documents\My Dropbox\Eclipse\Projects\R\MLB\retrosheet\rawdata 
> bgame -y 2010 2010bos.eva >2010bos.txt 

这是我试图运行里面R.我已经设置了代码工作目录里面的R.

dir <- paste("cd", getwd(), sep=" ") 
system(dir) 
system("bgame -y 2010 2010bos.eva >2010bos.txt") 

我相信这是用户错误,但我做错了什么?它似乎最初工作,但返回以下错误。我很可能做错了什么,但我相信我正在使用相同的命令。

Expanded game descriptor, version 109(185) of 05/08/2008. 
    Type 'bgame -h' for help. 
Copyright (c) 2001 by DiamondWare. 
[Processing file 2010bos.eva.] 
>2010bos.txt: can't open. 
Warning message: 
running command 'bgame -y 2010 2010bos.eva >2010bos.txt' had status 2 

任何帮助,您可以提供将不胜感激。

回答

19

您需要发出一个system()调用的所有命令:

system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" ")) 

你应该已经在你的工作目录,所以我不知道该cd getwd()是必要的。你可能需要引用你的路径,因为它包含空格。错误可能通过在>附近放置空格来解决。

如果我是你的话,我会尝试这样的:

system("bgame -y 2010 2010bos.eva > 2010bos.txt") 

UPDATE:

而且你应该在节中的“Unix和Windows之间的区别”的?system,说听从这一建议你应该使用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. 
+0

感谢您的帮助。我遵循你的建议并忽略了目录,但发现我必须在shell调用中包含shQuote才能表现不同。也就是说,它似乎有效,但我现在得到一个错误代码1,这是奇怪的,因为该文件看起来不错,并且命令是相同的,我会在R以外的命令行上键入。 – Btibert3 2011-04-21 16:51:33

+0

你会知道吗如何在Linux上完成相同的任务?看到我的[问题](http://stackoverflow.com/questions/36431465/warning-running-command-had-status-127-when-trying-to-run-exe-from-r) – Antoine 2016-04-06 08:38:17

0

是否破坏你的代码,当你错误1或不executi继续?

每当通过另一种语言执行系统命令时,在调用系统调用以查看到底发生了什么之前打印系统调用会很有用,请拔出您打算使用的shell并检查相同的错误。当命令正确执行时,这可能是bgame或R中的一个hickup。

如果你看一下http://astrostatistics.psu.edu/datasets/R/html/base/html/shell.html,你可以看到传递给系统调用的变量标志。“标志开关在shell下运行命令。 shell是bash或tcsh默认更改为“-c”。“

Also "the shell to be used can be changed by setting the configure variable R_SHELL to a suitable value (a full path to a shell, e.g. /usr/local/bin/bash)."

8

已经没有其他人发现,system("dir", intern = T)例如不工作,但你需要system("cmd.exe /c dir", intern = T)?只有后者对我有用。我在讨论网站here(William Dunlap的帖子,大约下降了三分之一)发现了这一点。

此外,它不能与“cd”命令一起使用,但可以在R中使用setwd()函数,然后该命令将在该目录内执行。

我创建了以下功能为方便起见,用于执行程序和运行命令:

#the subject is an input file that a programme might require 
execute <- function(programme, subject.spec = "", intern = FALSE, wait = FALSE){ 
    if(!identical(subject.spec, "")){subject.spec <- paste0(" ", subject.spec)} #put space before the subject if it exists 
    system(paste0("cmd.exe /c ", programme, subject.spec), intern = intern, wait = wait) 
} 


command <- function(command, intern = TRUE, wait = FALSE){ 
    system(paste("cmd.exe /c", command), intern = T, wait = wait) 
} 
+0

什么是等效的在Linux上? – Antoine 2016-04-06 08:26:07

+0

不能告诉你,对不起。 – Bazz 2016-04-06 08:30:11

+0

不用担心谢谢 – Antoine 2016-04-06 08:33:17