启动R并从批处理文件中调用脚本 [英] Starting R and calling a script from a batch file

查看:108
本文介绍了启动R并从批处理文件中调用脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于R的GUI,它允许一些非技术用户访问统计模型.就目前而言,用户必须先加载R,然后在命令行中键入loadGui().

I have an R-based GUI that allows some non-technical users access to a stats model. As it stands, the users have to first load R and then type loadGui() at the command line.

虽然这不是一个过分的挑战,但我不希望非技术人员在命令行中键入任何内容.我的想法是编写一个.bat文件(用户都在运行Windows,尽管也欢迎使用多平台解决方案),该文件可以启动R GUI,然后自动运行该命令.

While this isn't overly challenging, I don't like having to make non-technical people type anything at a command line. I had the idea of writing a .bat file (users are all running Windows, though multi-platform solutions also appreciated) that starts R GUI, then autoruns that command.

我的第一个问题是从命令行打开RGui.虽然我可以提供一个明确的路径,例如

My first problem is opening RGui from the command line. While I can provide an explicit path, such as

"%ProgramW6432%\R\R-2.15.1\bin\i386\Rgui.exe"

每次R升级时都需要更新.从%path%环境变量中检索RGui的位置会更好,但是我不知道一种简单的方法来解析它.

it will need updating each time R is upgraded. It would be better to retrieve the location of RGui from the %path% environment variable, but I don't know an easy way to parse that.

第二个更大的问题是如何在启动时从命令行调用R的命令.我首先想到的是,我可以复制~/.Rprofile,附加额外的命令,然后在加载R后替换文件的原始副本.不过,这太凌乱了,所以我想要一个替代方法.

The second, larger problem is how to call commands for R on startup from the command line. My first thought is that I could take a copy of ~/.Rprofile, append the extra command, and then replace the original copy of the file once R is loaded. This is awfully messy though, so I'd like an alternative.

不能以批处理模式运行R,首先是因为我不能说服GUI进行显示,其次是因为我希望R控制台可用,即使用户不需要 来使用它.

Running R in batch mode isn't an option, firstly since I can't persuade GUIs to display themselves, and secondly because I would like the R console available, even if the users shouldn't need to use it.

如果您希望玩具GUI测试您的想法,请尝试以下操作:

If you want a toy GUI to test your ideas, try this:

loadGui <- function()
{
  library(gWidgetstclck)
  win <- gwindow("test")
  rad <- gradio(letters[1:3], cont = win)
}

推荐答案

我已经做过两次类似的事情.在我的情况下,客户端使用Windows,因此我只是在他们的计算机上安装了R,并在其桌面上创建了运行R的快捷方式.然后,我右键单击该快捷方式,然后选择属性以获取propertiest对话框.然后,我将开始于"文件夹更改为我要从中运行的文件夹(该文件夹具有包含正确数据的.Rdata文件,并且.Rdata文件中具有.First函数,或者该文件夹中具有.Rprofile).还有一个运行:"选项,其中有一个最小化"选项以最小化运行主R窗口.

I have done similar a couple of times. In my cases the client was using windows so I just installed R on their computer and created a shortcut on their desktop to run R. Then I right click on the shortcut and choose properties to get the propertiest dialog. I then changed the "Start in" folder to the one where I wanted it to run from (which had the .Rdata file with the correct data and either a .First function in the .Rdata file or .Rprofile in the folder). There is also a "Run:" option that has a "Minimized" option to run the main R window minimized.

我已经创建了要运行的功能(通常是使用tcltk的专用gui),并将所有需要的数据保存在.Rdata文件中,还创建了.First或.Rprofile来运行显示gui的命令.用户双击桌面上的图标,然后弹出我可以使用的GUI,而忽略其他部分.

I had created the functions that I wanted to run (usually a specialized gui using tcltk) and any needed data and saved them in the .Rdata file and also either created .First or .Rprofile to run the comnand that showed the gui. The user double clicks on the icon on the desktop and up pops my GUI that they can work with while ignoring the other parts.

这篇关于启动R并从批处理文件中调用脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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