4

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.

enter image description here

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?

4
  • try this jakemdrew.com/blog/RScriptRunner.htm Commented Jul 8, 2015 at 11:32
  • Also you can call R functions from .NET code with use of F# RTypeProvider. Doesn't answer your question, but may be helpfull. Commented Jul 8, 2015 at 12:39
  • @Thorarins I tried with the RScriptRunner. I created a class file with the code provided in the RScriptRunner. I had breakpoints and the code is getting run however, no result is produced. The result value is always showing as " " Commented Jul 8, 2015 at 16:08
  • @Thorarins Thanks for your help... rscriptrunner worked by doing some tweaks to the code... Commented Jul 14, 2015 at 7:20

2 Answers 2

2

I used the RScriptRunner and my solution got fixed. I created a class file for RScriptRunner and called that function in my button click event.

RScriptRunner can execute the r script using the rscript.exe in the commandline mode.

However I am not able to get the output result from the r code.

Sign up to request clarification or add additional context in comments.

Comments

0

ASP.NET does not play well with R.NET, not without ironing out some IIS quirks. to do with environment variables. That said, Access Violation Exception is not the tell-tale symptom I'd expect.

I'd advise you prototype R.NET code without ASP.NET or even desktop user interface, if you have not started this way. This can help diagnose if an issue is specific to IIS or not.

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

In particular, there is a solution under "solutions/WebApp" that a contributor and I (mostly the contributor) put together as a sample. Do note that you may need to change your PATH env var before running from Visual Studio.

IIS will not let you change permanently an environmental variable. I think you work around this however, by changing the path every "button click". You'll find background information around IIS in the discussion https://rdotnet.codeplex.com/discussions/462947, in particular towards the end of the thread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.