R和系统调用 [英] R and System calls

查看:279
本文介绍了R和系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R在过去对commmand行做非常基本的调用。该示例可以在此处找到。



这一次,我想模仿这个代码从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中设置了工作目录。

  dir<  -  paste(cd,getwd (bgame -y 2010 2010bos.eva> 2010bos.txt)
系统(dir)
pre>

我相信这是用户错误,但我做错了什么?它似乎最初工作,但返回以下错误。

 展开的游戏描述符,版本109 185)of 2008/08/2008。 
输入bgame -h获取帮助。
DiamondWare的版权所有(c)2001。
[正在处理文件2010bos.eva。]
> 2010bos.txt:无法打开。
警告消息:
运行命令'bgame -y 2010 2010bos.eva> 2010bos.txt'状态2

$ b

解决方案

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

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

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



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

 系统(bgame -y 2010 2010bos.eva> 2010bos.txt)

更新:



你应该在在?system 之间的Unix和Windows之间的部分中说你应该使用 shell

 •最重要的区别是在Unix上相似的
'system'启动一个shell,然后运行'command'。在
Windows命令直接运行 - 使用'shell'为
接口运行'命令'_via_一个shell(默认
Windows shell'cmd.exe',它有很多与
POSIX shell的区别)。

这意味着它不能假定重定向或
管道将工作在'系统'(重定向有时会,但
我们已经看到它停止工作后的Windows
安全补丁),并且必须在
Windows上使用'system2'(或'shell')。


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.

解决方案

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.

这篇关于R和系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆