简体   繁体   中英

unable to connect to R from c#

I am trying to connect to R from c# using the following code. It looks like C# is not reading the R dll files. My R installation directory is this:

C:\Users\R-2-13\R-2.13.0\bin\i386 

and I also downloaded and put the R.NET.dll in the same directory. In Visual Studio, I set the reference to R.NET.dll file. When I run the following code, the code goes the the catch section "unable to find the R Installation". Any ideas? Has anybody got this working?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;

namespace RNet_Calculator
{
    public partial class Form1 : Form
    {

        // set up basics and create RDotNet instance 
        // if anticipated install of R is not found, ask the user to find it. 

        public Form1()
        {
            InitializeComponent();


            bool r_located = false;
            while (r_located == false)
            {
                try
                {
                    REngine.SetDllDirectory(@"C:\Users\R-2-13\R-2.13.0\bin\i386");
                    REngine.CreateInstance("RDotNet"); 
                    r_located = true;
                }

                catch { MessageBox.Show(@"Unable to find R installation's \bin\i386 folder. Press OK to attempt to locate it."); 

                        MessageBox.Show("error");

                } 
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

This is http://rdotnet.codeplex.com/ (RDotNet) to develop Winform applications. While I know Shiny and all the other Web-like R-tools quite well, the combination of c# and R still is my preferred end-user combinations. Try simple things like disabling buttons with Shiny...

Too bad rdotnet is quite buggy; in the current version, it crashes on R exeptions, even in try-catched ones.

This said: please make absolutely sure that you use version 1.5, not the stupidly called "stable" (=early beta) version on the page. Best download it via NuGet. Also check if you did not mix 32bit R with 64 bit c#.

Using the Helper-functions of 1.5, initialization is:

  Helper.SetEnvironmentVariables();
  engine = REngine.CreateInstance(EngineName);
  engine.Initialize();
  # Assuming you want to catch the graphic window, use my RGraphAppHook
  # on the rdotnet site http://rdotnet.codeplex.com/workitem/7
  cbt = new RGraphAppHook { GraphControl = GraphPanelControl };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM