What is the correct way to use R.Net in an Intranet application

Aug 30, 2012 at 9:26 PM
Edited Aug 30, 2012 at 9:45 PM

I am trying to create an Intranet site that collects data, stores it in a database, runs a forecast in R and saves the forecast to the database.  Everything runs perfectly the first time I test it.  Then the second time it gets hung up.

 

I'm using version 1.4.  Any advice?

 

Has anyone been successful using R.Net in a web application?  Should I give this up and have the R scripts that I'm using re-written in C#?

Sep 7, 2012 at 8:19 PM

Unfortunately R engine does not support reinitialization. You need to recycle the same instance through calls. But then you cannot have concurrent calls.

Sep 10, 2012 at 10:17 PM

I think I can work with that.  Do I initialize at the start of each session and than close the r engine at the end of each session.  Or do I initialize the r engine at the at the start of the application.

 

Here is how I am doing it now.  It seems to be working in my local environment.  But I'm not sure if this will work for the Intranet site.

 

Any ideas?

 

namespace OIApp
{
    public class Global : System.Web.HttpApplication
    {  // Create a global singleton instance of the R engine (used for statistical programming)
        public static REngine engine;

        protected void Application_Start(object sender, EventArgs e)
        {
            // Set the folder in which R.dll locates.
            var envPath = System.Environment.GetEnvironmentVariable("PATH");
            var rBinPath = @"C:\Program Files\R\R-2.15.1\bin\i386";
            System.Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);

            engine = REngine.GetInstanceFromID("RDotNet");
            if (engine == null)
            {
                engine = REngine.CreateInstance("RDotNet");
            }
            // Initializes settings.
            engine.Initialize();

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            //OIApp.RSetup.StopR();
        }
    }
}