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

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

问题描述

我过去曾使用 R 对命令行进行非常基本的调用.可以在此处找到该示例.

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

这一次,我想模仿这段从 Windows 命令行成功运行的代码:

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

> cd C:Documents and SettingsBTIBERTMy DocumentsMy DropboxEclipseProjectsRMLB
etrosheet
awdata
> bgame -y 2010 2010bos.eva >2010bos.txt

这是我试图在 R 中运行的代码.我已经在 R 中设置了工作目录.

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.

推荐答案

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

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

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

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

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")

更新:

并且您可能应该注意 ?system 的Unix 和 Windows 之间的差异"部分中的这条建议,其中说您应该使用 shell:

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天全站免登陆