我想知道是否可以从Windows中的命令提示符向RGui传递参数.我想做点什么
RGui myScript.r param1 param2
Run Code Online (Sandbox Code Playgroud)
就像我对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();
Run Code Online (Sandbox Code Playgroud)
如上所述,你通常不能这样做.如果你攻击你的Rprofile或Rprofile.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) )
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许您:
Rgui --args myscript.r arg1 arg2
Rscript myscript.r arg1 arg2
R --args myscript.r arg1 arg2
R -f myscript.r --args arg1 arg2
Run Code Online (Sandbox Code Playgroud)
--args参数将处理@iterator警告的弹出窗口.代码将生成一个Args包含在基本环境中的变量(不是 .GlobalEnv!).此变量包含除文件名之外的所有参数.您可以随后从脚本中访问该脚本,例如:
#dumb script
print(Args)
Run Code Online (Sandbox Code Playgroud)
如果使用Rgui或调用R,则还会有一个变量File,其中包含已获取的文件的名称.
请注意,更改您的rProfile无法移植到其他计算机.所以这仅供个人使用.你也不能在-args之后给-f作为参数,否则你会得到错误.
编辑:我们最好搜索"-f"而不是"-f",因为这可能发生在"path/to/new-files /"中.
| 归档时间: |
|
| 查看次数: |
2459 次 |
| 最近记录: |