[英]Using RScript.exe to run a rscript with parameter
我有一个简单的 R 脚本,它接受一个输入值并根据输入值进行一些计算并将其写入一个 csv 文件。 下面是我的 r 脚本的代码。
testfun=function(x){
x<-args[1]
x=sqrt(x)+(x*x)
a=x+x+x
b=data.frame(x,a)
setwd("D:\\R Script")
write.csv(b,"testfun.csv",row.names=F)
}
我正在使用 Jake Drew 提供的 Rscriptrunner 从我的 asp.net web 应用程序调用这个 rscript
/// <summary>
/// This class runs R code from a file using the console.
/// </summary>
public class RScriptRunner
{
/// <summary>
/// Runs an R script from a file using Rscript.exe.
/// Example:
/// RScriptRunner.RunFromCmd(curDirectory + @"\ImageClustering.r", "rscript.exe", curDirectory.Replace('\\','/'));
/// Getting args passed from C# using R:
/// args = commandArgs(trailingOnly = TRUE)
/// print(args[1]);
/// </summary>
/// <param name="rCodeFilePath">File where your R code is located.</param>
/// <param name="rScriptExecutablePath">Usually only requires "rscript.exe"</param>
/// <param name="args">Multiple R args can be seperated by spaces.</param>
/// <returns>Returns a string with the R responses.</returns>
public static string RunFromCmd(string rCodeFilePath, string rScriptExecutablePath, string args, int iInput)
{
string file = rCodeFilePath;
string result = string.Empty;
try
{
var info = new ProcessStartInfo();
info.FileName = rScriptExecutablePath;
info.WorkingDirectory = Path.GetDirectoryName(rScriptExecutablePath);
info.Arguments = rCodeFilePath + " " + args;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
using (var proc = new Process())
{
proc.StartInfo = info;
proc.Start();
result = proc.StandardOutput.ReadToEnd();
proc.Close();
}
return result;
}
catch (Exception ex)
{
throw new Exception("R Script failed: " + result, ex);
}
}
}
我将脚本调用到 rscriptrunner 的方式如下
int x = 64;
string scriptPath = string.Format(@"C:\Program Files\R\{0}\bin\Rscript.exe", Rversion);
string path = string.Format(@"D:\testfun.r");
string result = RScriptRunner.RunFromCmd(path, scriptPath, path.Replace('\\', '/'), x);
基本上,我想将我的 Web 应用程序中的 x 值提供到我的 r 代码中,并在 r 脚本中指定的 csv 文件中获取输出。
我尝试了以下线程中提供的几种方法
在 .net 中使用 start.process 运行 R 脚本
但是我无法得到我想要的。 脚本没有被执行。 我还尝试在我的 rscript 中添加 args<-commandArgs(TRUE) 但它不起作用。
基本上,我是一名 .net 开发人员,由于某些客户端需要,我希望从我的 Web 应用程序运行 r 脚本。 我对 R 不太了解,如果有人帮助我了解如何将参数值传递给我的 r 代码,这对我会有帮助。
我找到了一种通过 ASP.NET C# 为我的函数提供参数的方法。
基本上我为我的 r 代码创建了一个包装器,这样我就可以从另一个 wrapper.r 代码中调用我真正的 r 代码,我在其中传递我的参数。
包装器代码如下所示
args <- commandArgs(TRUE)
r <- as.double(args[1])
x <- as.double(args[2])
source("logistic.R")
logistic(r, x)
在这里,我使用源函数调用我的真实 r 代码并将参数传递给我的logistics.R 代码。
创建包装器后,我将使用以下代码执行我的 r 代码
Rscript logistic-wrapper.R 3.2 0.5
执行logistic-wrapper.R 以及参数3.2 和0.5 将执行我的logistic.r 代码提供的参数。
最快的方法是使用 args 运行 R CMD 批处理并将结果写入文件,让我们从命令行说 .json:R CMD BATCH --slave '--args ab c'
在 R:
args <- commandArgs(trailingOnly = TRUE)
a <- args[1]
b <- args[2]
c <- args[3]
并将您的结果写入文件
您可以使用包 funr。
假设你的脚本看起来像这样
# define the function
testfun <- function(x = 2, outfile = "testfun.csv"){
x=sqrt(x)+(x*x)
a=x+x+x
b=data.frame(x, a)
#setwd("D:\\R Script")
write.csv(b,outfile,row.names=F)
}
help_text = "
This script creates a data.frame and writes it out as a CSV file.
Usage: testfunr.R testfun x=<a number> outfile=<myfile.csv>
"
library(funr)
out = funr(commandArgs(trailingOnly = TRUE), help_text = help_text)
然后简单地用参数调用你的函数。
Rscript testfun.r testfun x=2
# one may explicitly provide the output filename as well
Rscript testfun.r testfun x=2 outfile=myfile.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.