我过去使用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
Run Code Online (Sandbox Code Playgroud)
这是我试图在R里面运行的代码.我已经在R里面设置了工作目录.
dir <- paste("cd", getwd(), sep=" ")
system(dir)
system("bgame -y 2010 2010bos.eva >2010bos.txt")
Run Code Online (Sandbox Code Playgroud)
我确定这是用户错误,但我做错了什么?它似乎最初工作,但返回以下错误.我很可能做错了什么,但我相信我使用相同的命令.
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
Run Code Online (Sandbox Code Playgroud)
您将提供任何帮助,我们将不胜感激.
Jos*_*ich 27
您需要在一次system()调用中发出所有命令:
system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" "))
Run Code Online (Sandbox Code Playgroud)
你应该已经在你的工作目录中了,所以我不确定cd getwd()是否有必要.并且您可能需要在路径周围添加引号,因为它包含空格.可以通过在周围放置空格来解决错误>.
如果我在你的鞋子里,我会试试这个:
system("bgame -y 2010 2010bos.eva > 2010bos.txt")
Run Code Online (Sandbox Code Playgroud)
更新:
你可能应该在"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.
Run Code Online (Sandbox Code Playgroud)
CJB*_*CJB 11
没有人发现system("dir", intern = T)例如不起作用,但你需要system("cmd.exe /c dir", intern = T)吗?只有后者适合我.我在这里的讨论网站上找到了这个(威廉邓拉普的帖子,约三分之一).
此外,它不适用于"cd"命令,但您可以使用setwd()R中的函数,然后命令将在该目录中执行.
为方便起见,我创建了以下函数,用于执行程序和运行命令:
#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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38955 次 |
| 最近记录: |