【问题标题】:R and System callsR 和系统调用
【发布时间】:2011-04-21 14:59:57
【问题描述】:

我过去曾使用 R 对命令行进行非常基本的调用。例子可以在here找到。

这一次,我希望模仿这段在 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 

您能提供的任何帮助将不胜感激。

【问题讨论】:

    标签: r command-line


    【解决方案1】:

    您需要在一次system() 调用中发出所有命令:

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

    您应该已经在您的工作目录中,所以我不确定cd getwd() 是否必要。而且您可能需要在路径周围加上引号,因为它包含空格。可以通过在&gt; 周围放置空格来解决该错误。

    如果我在你的鞋子里,我会试试这个:

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

    更新:

    您可能应该注意?system 的“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.
    

    【讨论】:

    • 感谢您的帮助。我听从了您的建议并忽略了该目录,但发现我必须在 shell 调用中包含 shQuote 才能使其行为不同。也就是说,它似乎有效,但我现在收到错误代码 1,这很奇怪,因为文件看起来不错,并且命令与我在 R 之外的命令行上输入的相同。
    • 你们知道如何在 Linux 上完成同样的任务吗?见我的question
    • 我想像这样运行多个系统命令。在命令中,可能有一个命令在执行时会失败。 R如何忽略“执行停止”并继续执行下一个命令?
    【解决方案2】:

    没有其他人发现例如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)
    }
    

    【讨论】:

    • Linux 上的等价物是什么?
    • 无法告诉你,抱歉。
    • 这是真的,但是(如在接受的答案中引用的那样)更好的方法是在 Windows 中使用 shell 而不是 systemshell("dir", intern=T 对我来说很好用。
    【解决方案3】:

    当您遇到错误 1 ​​时它会破坏您的代码还是继续执行?

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

    如果您查看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)."

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2015-01-02
      • 2014-12-25
      • 1970-01-01
      • 2010-12-18
      • 2016-09-07
      相关资源
      最近更新 更多