I need to run a r script stored in a folder from my asp.net application and show the output of that r script in the web page.
below is the code of my Default.aspx.cs file
using RDotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private static double EvaluateExpression(REngine engine, string expression)
{
var expressionVector = engine.CreateCharacterVector(new[] { expression });
engine.SetSymbol("expr", expressionVector);
// WRONG -- Need to parse to expression before evaluation
//var result = engine.Evaluate("eval(expr)");
// RIGHT way to do this!!!
var result = engine.Evaluate("eval(parse(text=expr))");
var ret = result.AsNumeric().First();
return ret;
}
public static void SetupPath(string Rversion = "R-3.2.1")
{
var oldPath = System.Environment.GetEnvironmentVariable("PATH");
var rPath = System.Environment.Is64BitProcess ?
string.Format(@"C:\Program Files\R\{0}\bin\x64", Rversion) :
string.Format(@"C:\Program Files\R\{0}\bin\i386", Rversion);
if (!Directory.Exists(rPath))
throw new DirectoryNotFoundException(
string.Format(" R.dll not found in : {0}", rPath));
var newPath = string.Format("{0}{1}{2}", rPath,
System.IO.Path.PathSeparator, oldPath);
System.Environment.SetEnvironmentVariable("PATH", newPath);
}
protected void Button1_Click(object sender, EventArgs e)
{
SetupPath();
REngine.SetEnvironmentVariables();
REngine engine = REngine.GetInstance();
// REngine requires explicit initialization.
// You can set some parameters.
engine.Initialize();
string path = Server.MapPath("~/R Script/rscript.r");
engine.Evaluate("source('" + path + "')");
Label1.Text = EvaluateExpression(engine, "C").ToString();
}
}
The rscript.R Script file has the below simple coding
C<-sum(1,2,3,4)
The operation is such a way that I have a button and when i click the button, the r script in the folder needs to be executed and the result value needs to get displayed in the Label I have in my web page. I have the file rscript.r in the web applications folder and its getting pulled correctly.
When I click on the button the code gets executed and my web application is throwing an error
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I tried to google out but that was not helpful. Is there a way to execute the r file directly from asp.net web application and get the output of the executed file.
Can anyone help me out?