我过去曾使用 R 对命令行进行非常基本的调用。例子可以找到here https://stackoverflow.com/questions/3284301/command-line-in-r-code.
这一次,我希望模仿这段从 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
您能提供的任何帮助将不胜感激。
您需要一次发出所有命令system()
call:
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)