将脚本作为参数传递给RGui [英] Passing script as parameter to RGui

查看:130
本文介绍了将脚本作为参数传递给RGui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可能从窗口中的命令提示符传递参数到RGui。
我想做像

  RGui myScript.r param1 param2 



就像我用RScript做的那样,但是我需要显示一个GUI。



这里有一些关于我的需要的信息。
我想在我的C#表单应用程序中嵌入一个用R写成的gui。会发生什么是我按下一个按钮在窗体中,应用程序启动一个过程,用我的脚本和一些参数调用RGui。这到目前为止与RScript工作正常,但现在我显示图形我需要R在交互式模式。
下面是我使用的代码:

  myProcess.StartInfo.FileName = Pathing.GetUNCPath(r_path) \\Rscript; 
string script_path = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName.ToString();
myProcess.StartInfo.Arguments = Pathing.GetUNCPath(script_path)+\\display.r+ data_path;
myProcess.StartInfo.UseShellExecute = true;
myProcess.Start();
myProcess.WaitForExit();


解决方案

如上所述,你通常不能这样做。如果你进入你的 Rprofile Rprofile.site (有关更多信息,请参阅启动;或这个网站),你可以回避,但是代码是不能移植到其他计算机。



您可以将此代码添加到 Rprofile 文件或 Rprofile.site (可以在R安装的/ etc文件夹中找到):

  Args < -  commandArgs(trailingOnly = TRUE)
if(length(Args)> 0& sum(grepl(-f,commandArgs()))= = 0){
if(grepl((?i).r $,Args [1])){
File< - Args [1]
Args < [-1]
tryCatch(source(File),error = function(e)print(e))
}
}

这将允许您执行:

  Rgui --args myscript.r arg1 arg2 
Rscript myscript.r arg1 arg2
R --args myscript.r arg1 arg2
R -f myscript.r --args arg1 arg2

--args参数将处理@iterator警告的弹出窗口。代码将产生一个变量 Args ,它包含在基本环境中(不是 .GlobalEnv!)。此变量包含除文件名之外的所有参数。您可以随后从脚本中访问该脚本,例如:

  #dumb脚本
print(Args)

如果使用 Rgui



请注意,更改您的rProfile是一个文件,不能移植到其他计算机。所以这是仅供个人使用。



编辑:我们最好搜索-f而不是 - f,因为这可能发生在path / to / new-files /\".


I was wondering if it possible to pass parameters to RGui from command prompt in windows. I would like to do something like

RGui myScript.r param1 param2

just like I would do with RScript but I need to display a GUI.

Here is some more info regarding my needs. I want to embedd a gui written in R in my C# forms application. What would happen is I press a button in the form and the application launches a process that calls RGui with my script and some parameters. This has worked fine so far with RScript but now that I am displaying graphics I need R to be in interactive mode. Here is the code I am using:

        myProcess.StartInfo.FileName =Pathing.GetUNCPath( r_path) + "\\Rscript";
        string script_path=Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName.ToString();
        myProcess.StartInfo.Arguments = Pathing.GetUNCPath(script_path) + "\\display.r " + data_path;
        myProcess.StartInfo.UseShellExecute = true;           
        myProcess.Start();
        myProcess.WaitForExit();

解决方案

As said, you normally cannot do that. If you hack into your Rprofile or Rprofile.site ( see ?Startup for more information, or this site), you can go around that, but the code is not portable to other computers. So if you feel really lucky and daring, you can try to do the following.

You add this code to your Rprofile file or Rprofile.site (which can be found in the /etc folder of your R install):

Args <- commandArgs(trailingOnly=TRUE)
if(length(Args)>0 & sum(grepl(" -f ",commandArgs()))==0 ){          
    if(grepl("(?i).r$",Args[1])){
        File <- Args[1]
        Args <- Args[-1]
        tryCatch(source(File) , error=function(e) print(e) )
    }
}

This will allow you to do :

Rgui --args myscript.r arg1 arg2
Rscript myscript.r arg1 arg2
R --args myscript.r arg1 arg2
R -f myscript.r --args arg1 arg2

The --args argument will take care of the popups that @iterator warns for. The code will result in a variable Args which is contained in the base environment (which is not .GlobalEnv!). This variable contains all arguments apart from the filename. You can subsequently access that one from your script, eg:

#dumb script
print(Args)

If called with Rgui or R, there will also be a variable File that contains the name of the file that has been sourced.

Be reminded that changing your rProfile is not portable to other computers. So this is for personal use only. You can also not give -f as a parameter after --args, or you'll get errors.

Edit: We better search for " -f " than "-f" as this can occur in "path/to/new-files/".

这篇关于将脚本作为参数传递给RGui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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