title: Using R in asp.net web application
body: <p>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.</p>
  
  <p>Below is my cs code</p>
  
  <pre><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&lt;-5+3+2");
          engine.Evaluate("G&lt;-paste('hello from r', 'Test')");
          Label1.Text = EvaluateExpression(engine, "G").ToString();
      }
  }
  </code></pre>
  
  <p>I have a click button and when i click it i want the output to be showed in the web page.</p>
  
  <p>When i run the code and click the button i am getting the text </p>
  
  <blockquote>
    <p>NaN</p>
  </blockquote>
  
  <p>in the label control page. However when i run </p>
  
  <pre><code>engine.Evaluate("G&lt;-5+3+2"); 
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>engine.Evaluate("G&lt;-paste('hello from r', 'Test')");
  </code></pre>
  
  <p>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.</p>
  
  <p>Can anyone help me to get my problem solved by displaying the r output in my web page?</p>