R和系统调用

R和系统调用,r,command-line,R,Command Line,在过去,我使用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

在过去,我使用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")

非常感谢您提供的任何帮助。

您需要在一个
系统()中发出所有命令。
调用:

system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" "))
您应该已经在工作目录中,所以我不确定是否需要
cd getwd()
。您可能需要在路径周围加引号,因为它包含空格。可以通过在
>
周围放置空格来解决此错误

如果我处在你的位置,我会试试这个:

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

您可能应该注意
系统
中“Unix和Windows之间的差异”一节中的建议,其中说您应该使用
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.

当您收到错误1时,它是否会中断您的代码,或者执行是否继续?

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

如果查看,可以看到传递给系统调用的变量标志。“标记开关以在shell下运行命令。如果shell为bash或tcsh,则默认值更改为“-c”。”


没有其他人发现例如
系统(“dir”,intern=T)
不起作用,但是您需要
系统(“cmd.exe/c dir”,intern=T)
?只有后者对我有效。我在讨论网站(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)
}
#主题是程序可能需要的输入文件

谢谢你的帮助。我听从了你的建议,忽略了目录,但发现我必须在shell调用中包含shQuote,这样它的行为才会有所不同。这就是说,它似乎已经工作了,但我现在得到了一个错误代码1,这很奇怪,因为文件看起来正常,命令与我在R之外的命令行中键入的相同。你们知道如何在Linux上完成相同的任务吗?请参阅我的“我想运行多个系统命令”,如下图所示。在命令中,可能有一个命令在执行时会失败。R如何可以忽略“执行暂停”并继续执行下一个命令?Linux上的等效命令是什么?抱歉,我无法告诉您。这是真的,但是(正如公认答案中所引用的)更好的方法是在windows中使用
shell
而不是
system
<代码>shell(“dir”,intern=T
对我来说很好用。
#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)
}