我有一个简单的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提供的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文件中获取输出。
我尝试了以下线程中提供的几种方法
How can I read command line parameters from an R script?
Run R script with start.process in .net
然而,我无法得到我想要的。脚本不会被执行。我还尝试在rscript中添加args<-commandArgs(TRUE),但它不起作用。
基本上,我是一个.net开发人员,由于客户端的需要,我想从我的web应用程序中运行r脚本。我不太了解R,如果有人帮助我如何将参数值传递给我的r代码,这将对我很有帮助。
发布于 2015-11-23 04:18:38
我找到了一种通过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代码,并将参数传递给我的logical.R代码。
创建包装器后,我将使用以下代码执行r代码
Rscript logistic-wrapper.R 3.2 0.5执行logistic-wrapper.R以及参数3.2和0.5将执行与参数一起提供的逻辑.r代码。
发布于 2015-10-19 12:15:05
最快的方法是使用args运行run批处理,并将结果写入一个文件,比如命令行中的.json :run批处理--从-args a b c‘
在R:
args <- commandArgs(trailingOnly = TRUE)
a <- args[1]
b <- args[2]
c <- args[3]只需将结果写入文件
发布于 2015-10-19 12:33:38
你可以用那个包裹漏斗。
假设您的脚本如下所示
# 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.csvhttps://stackoverflow.com/questions/33213663
复制相似问题