Run R Script from asp.net application and get output from r

I need to run an r script stored in a folder from my asp.net application and show the output of that r script on a 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 following simplest encoding

C<-sum(1,2,3,4)

      

The operation is such that I have a button and when I click the button the r script in the folder should be executed and the result value should be displayed in the shortcut that I have in my web page, I have an rscript.r file in the folder web application and getting it right.

When I click the button, the code runs and my web app throws an error

An attempt was made to read or write protected memory. It is often that other memory is corrupted.

enter image description here

I tried google but it didn't help. Is there a way to execute r file directly from asp.net web app and get the result of the executed file.

Can anyone help me?

+3


source to share


2 answers


I used RScriptRunner and my solution has been fixed. I created a class file for RScriptRunner and called this function on the click event.

RScriptRunner can execute r script using rscript.exe in command line mode.



However, I cannot get the output from the r-code.

+2


source


ASP.NET does not work well with R.NET, not excluding some IIS specifics. to use environment variables. However, excluding an access violation is not the symptom sign I would expect.

I would advise you to prototype R.NET code without ASP.NET or even a desktop UI if you haven't started this way yet. This can help diagnose if the problem is IIS specific or not.

Second, take a look at the existing samples at: https://github.com/jmp75/rdotnet-onboarding/tree/master/



Particularly in the "solutions / WebApp" section, there is a solution that the contributor and myself (mainly the contributor) come together as a sample. Please note that you may need to change the PATH env var before starting from Visual Studio.

IIS will prevent you from constantly changing your environment variable. I think you get around this, but by changing the path, every button click. You will find background information around IIS in the discussion at https://rdotnet.codeplex.com/discussions/462947 , specifically towards the end of the thread.

0


source







All Articles