将脚本作为参数传递给 RGUI

发布于 2024-11-28 05:04:06 字数 769 浏览 1 评论 0 原文

我想知道是否可以从 Windows 中的命令提示符将参数传递给 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();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2024-12-05 05:04:06

如前所述,您通常不能这样做。如果您侵入您的 RprofileRprofile.site(请参阅?Startup 了解更多信息,或 此站点),您可以绕过它,但代码无法移植到其他计算机。所以,如果你觉得自己真的很幸运,也很勇敢,你可以尝试做以下事情。

您将此代码添加到您的 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 <- 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 script
print(Args)

如果使用 RguiR 调用,还会有一个变量 File,其中包含已被调用的文件的名称。来源。

请注意,更改您的 rProfile 无法移植到其他计算机。所以这仅供个人使用。您也不能在 --args 之后给出 -f 作为参数,否则会出现错误。

编辑:我们最好搜索“-f”而不是“-f”,因为这可能出现在“path/to/new-files/”中。

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/".

守望孤独 2024-12-05 05:04:06

(更新)警告:这会“起作用”,但这是非常不明智的。据我所知,Rgui 并不打算采用此类脚本参数。 @Joris 向我指出可接受的参数列表列于 Rgui --help 中。

如果你遵循下面的方法,有些人会认为你正在走向疯狂。另一方面,疯狂的人可能会认为你是天才。所有人都会同意你不应该在他们使用的东西上这样做。

警告结束。

如果脚本名为.Rprofile,它将被获取。如果您创建一个读取commandArgs().Rprofile文件(或环境变量),那么您可以将其设置为解析命令行。

你会收到来自 R 的错误/忽略弹出窗口。这是为了识别已经做了坏事。

就其价值而言,这可能在 Rstudio 的待办事项列表中:http://support.rstudio.org/help/discussions/problems/823-pass-command-line-parameters-to-r

(UPDATED) WARNING: This will "work" but it is extremely ill-advised. As far as I can tell, Rgui is not meant to take such script parameters. @Joris pointed out to me the list of acceptable parameters is listed in Rgui --help.

If you follow the method below, some people will think you are on your way to madness. On the other hand, mad people may think you're a genius. All will agree that you should not do this in stuff that they use.

End of warnings.

If the script is named .Rprofile it will be sourced. If you create a .Rprofile file (or environment variable) that reads commandArgs() then you can set it up to parse the command line.

You will get errors/ignore popups from R. That's to identify that a bad thing has been done.

For what it's worth, this may be on the to-do list for Rstudio: http://support.rstudio.org/help/discussions/problems/823-pass-command-line-parameters-to-r

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文