R 和系统调用
我过去曾使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在一次
system()
调用中发出所有命令:您应该已经在工作目录中,因此我不确定
cd getwd()
是否必要。并且您的路径可能需要引号,因为它包含空格。该错误可以通过在>
周围添加空格来解决。如果我处于你的立场,我会尝试这个:
更新:
你可能应该注意
?system
的“Unix 和 Windows 之间的差异”部分中的建议,它说你应该使用shell :
You need to issue all commands in one
system()
call: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:
UPDATE:
And you should probably heed this advice in the "Differences between Unix and Windows" section of
?system
that says you should useshell
:没有其他人发现
system("dir", intern = T)
不起作用,但您需要system("cmd.exe /c dir", intern = T)?只有后者对我有用。我在讨论网站此处(William Dunlap 的帖子,大约下降了三分之一)。
此外,它不适用于“cd”命令,但您可以使用 R 中的 setwd() 函数,然后该命令将在该目录中执行。
为了方便起见,我创建了以下函数来执行程序和运行命令:
Has no-one else found that
system("dir", intern = T)
for example doesn't work, but that you needsystem("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:
当您收到错误 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)."