3

I am trying to use R script in a asp.net c# web application. I am trying to use a simple r script which will concatenate two strings to a single one.

Below is my cs code

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();
        //engine.Evaluate("G<-5+3+2");
        engine.Evaluate("G<-paste('hello from r', 'Test')");
        Label1.Text = EvaluateExpression(engine, "G").ToString();
    }
}

I have a click button and when i click it i want the output to be showed in the web page.

When i run the code and click the button i am getting the text

NaN

in the label control page. However when i run

engine.Evaluate("G<-5+3+2"); 

instead of

engine.Evaluate("G<-paste('hello from r', 'Test')");

i am getting the correct value as 10 in the label of my page. I am not aware of this error and dont know how to get the output in my web page. I tried help in some websites however all the code has been written for console applications and nothing for asp.net web application.

Can anyone help me to get my problem solved by displaying the r output in my web page?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Karthik Venkatraman
  • 1,619
  • 4
  • 25
  • 55
  • i also tried to create instance using the below one using (REngine engine = REngine.CreateInstance("RDotNet")) { engine.Initialize(); however i'm getting an error at REngine.CreateInstance that create instance is unknown – Karthik Venkatraman Jul 08 '15 at 08:33

1 Answers1

3

You don't need EvaluateExpression at all. Use engine.GetSymbol("G") instead.

The NaN output you get happens because of AsNumeric() conversion inside EvaluateExpression. It will only work properly for numeric output, not character.

Here's a minimal example for numeric and character variables:

    static void Main(string[] args)
    {
        REngine.SetEnvironmentVariables();
        REngine engine = REngine.GetInstance();
        engine.Initialize();
        engine.Evaluate("x <- 40 + 2");
        engine.Evaluate("s <- paste('hello', letters[5])");
        var x = engine.GetSymbol("x").AsNumeric().First();
        var s = engine.GetSymbol("s").AsCharacter().First();
        Console.WriteLine(x);
        Console.WriteLine(s);
    }
tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Hi, I tried with the below code as you suggested engine.Evaluate("C<-paste('hello from r', 'Test')").AsSymbol(); Label1.Text = engine.GetSymbol("C").ToString(); however i still get the error as RDotNet.SymbolicExpression – Karthik Venkatraman Jul 08 '15 at 11:03