Quantcast
Channel: R.NET
Viewing all articles
Browse latest Browse all 1634

New Post: REngine only working once

$
0
0
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 libR.so 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";
                break;
            case PlatformID.Unix:
                rHome = "/usr/lib/R";
                break;
            default:
                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!

Jason

Viewing all articles
Browse latest Browse all 1634

Trending Articles