This project is read-only.

REngine only working once

Mar 4, 2014 at 3:13 PM
Hello everyone, i'm setting up a simple web application and when i run the sample code it works on my first run, but then stops working if the page is reloaded.

Here's the code i'm using:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using RDotNet;
using RDotNet.NativeLibrary;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void RunR_Click(object sender, EventArgs e)
        SetupPath(); // current process, soon to be deprecated
        using (REngine engine = REngine.CreateInstance("RDotNet"))
            engine.Initialize(); // required since v1.5
            CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
            engine.SetSymbol("greetings", charVec);
            engine.Evaluate("str(greetings)"); // print out in the console
            string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
            Rout.InnerHtml = string.Format("R answered: '{0}'", a[0]);

    public static void SetupPath()
        var oldPath = System.Environment.GetEnvironmentVariable("PATH");
        var rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.0.2\bin\x64" : @"C:\Program Files\R\R-3.0.2\bin\i386";
        // Mac OS X
        //var rPath = "/Library/Frameworks/R.framework/Libraries";
        // Linux (in case of exists in the directory)
        //var rPath = "/usr/lib";
        if (Directory.Exists(rPath) == false)
            throw new DirectoryNotFoundException(string.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath));
        var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);
        System.Environment.SetEnvironmentVariable("PATH", newPath);
        // NOTE: you may need to set up R_HOME manually also on some machines
        string rHome = "";
        var platform = Environment.OSVersion.Platform;
        switch (platform)
            case PlatformID.Win32NT:
                break; // R on Windows seems to have a way to deduce its R_HOME if its R.dll is in the PATH
            case PlatformID.MacOSX:
                rHome = "/Library/Frameworks/R.framework/Resources";
            case PlatformID.Unix:
                rHome = "/usr/lib/R";
                throw new NotSupportedException(platform.ToString());
        if (!string.IsNullOrEmpty(rHome))
            Environment.SetEnvironmentVariable("R_HOME", rHome);
am i missing something in the setup process?

Thank you for you input!

Mar 4, 2014 at 7:59 PM

REngine.CreateInstance should be called once only during the process. You'll find references to how to do this in a few posts such as and . The future API of R.NET will likely change to promote a use that takes into account this limitation of the native R engine.

Note that running R.NET from ASP.NET has known issues and I have not read on the discussions of a satisfactory way to overcome them.