R 和系统调用

发布于 2024-11-02 20:59:23 字数 909 浏览 1 评论 0原文

我过去曾使用 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 

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

I have used R in the past to do very basic calls to the commmand line. The example can be found here.

This time around, I am looking to mimic this code which runs successfully from the command line in Windows:

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

This is the code I am trying to run inside of R. I have already set the working directory inside of R.

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

I am sure this is user error, but what am I doing wrong? It appears to work initially, but returns the following error. I very well could be doing something wrong, but I believe I am using the same commands.

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 

Any help you can provide will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩心态 2024-11-09 20:59:23

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

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

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

如果我处于你的立场,我会尝试这个:

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.

You need to issue all commands in one system() call:

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

You should already be in your working directory, so I'm not sure the cd getwd() is necessary. And you may need quotes around your path because it contains spaces. The error may be resolved by putting spaces around >.

If I were in your shoes, I would try this:

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

UPDATE:

And you should probably heed this advice in the "Differences between Unix and Windows" section of ?system that says you should use 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.
无畏 2024-11-09 20:59:23

没有其他人发现 system("dir", intern = T) 不起作用,但您需要 system("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)
}

Has no-one else found that system("dir", intern = T) for example doesn't work, but that you need system("cmd.exe /c dir", intern = T)? Only the latter works for me. I found this at the discussion site here (William Dunlap's post, about a third of the way down).

Also, it doesn't work with the "cd" command, but you can use the setwd() function within R and then the command will be executed within that directory.

I created the following functions for convenience, for executing programmes and running commands:

#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)
}
塔塔猫 2024-11-09 20:59:23

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

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

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

还有“要使用的 shell可以通过将配置变量 R_SHELL 设置为合适的值(shell 的完整路径,例如 /usr/local/bin/bash)来更改。”

Does it break your code when you get error 1 or does execution continue?

Whenever executing system commands through another language it is useful to print the system call before you call it to see exactly what is happening, pull up the shell you are intending to use and check for the same error. As the command is executed correctly this could be a hickup in bgame or R.

If you look at http://astrostatistics.psu.edu/datasets/R/html/base/html/shell.html you can see the variable flag passed to the system call."flag the switch to run a command under the shell. If the shell is bash or tcsh the default is changed to "-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)."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文