This project is read-only.

C# and Library usage (specifically RODBC)

Oct 15, 2013 at 10:18 PM
Admitted a much stronger R user than C# user. We have pre built some models in R but are having some issues with the R.NET components. I am sure it is in something I am doing but not sure how to resolve.

I am trying to load some libraries and failing. Currently Code is:
engine.Evaluate("library(RODBC)");

previous to this is I am setting the dll directory, loading the RDotNet reference and all of that seems to be working fine.

I also added the library path to the environment variable for my windows 7 machine. I looked at the other RODBC discussion thread and that is where I figured it could be a path issue but I no even less about VB.NET.

Any thoughts would be appreciated. Thanks.
Oct 16, 2013 at 11:03 PM
Can you report:
  • a concise program (C# or VB by the sound of it and the R statements evaluates) reproducing the issue
  • The error message reported if any
  • the stack trace reported if available
Oct 17, 2013 at 1:28 AM
Yes

Code is Below. Error message is self generated in the try/catch, fails at: engine.Evaluate("library (RODBC)");

No Call Stack, so I am assuming it is failing on the R side? However, the r code works in R.


Public Form1()
    {
        InitializeComponent();

        //var rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-2.15.3\bin\x64" : @"C:\Program Files\R\R-2.15.3\bin\i386";

        string dlldir = @"C:\Program Files\R\R-3.0.2\bin\i386";
        bool r_located = false;

        while (r_located == false)
        {
            try
            {
                REngine.SetDllDirectory(dlldir);
                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.");

                /*if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    dlldir = @folderBrowserDialog1.SelectedPath;
                }*/




            }


        }
    }

    private string s = @"C:\Program Files\R\R-3.0.2\bin;C:\Program Files\R\R-3.0.2\bin\x64;";

    private void radButton1_Click(object sender, EventArgs e)
    {
        REngine engine = REngine.GetInstanceFromID("RDotNet");
        var path = System.Environment.GetEnvironmentVariable("Path");
        path = this.s + path;

  System.Environment.SetEnvironmentVariable("Path", path);

        string whichError = "Library Add";

        try
        {
            engine.Evaluate(@".libPaths(\' & 'C:\Program Files\R\R-3.0.2\library");
            engine.Evaluate("library (RODBC)");
            whichError = "ODBC connect issue";

            // Connect to ODBC
            //engine.EagerEvaluate("dataset<-read.table(file.choose(), header=TRUE, sep = ',')");
            engine.Evaluate("ch <- odbcConnect(************************************************);

            whichError = "Meter Call Error";
            //Prepare the dataframe


            whichError = "Conversion Error";

            engine.Evaluate("Meters.DF$ConvertDate <- as.Date(Meters.DF$ReadingDate, '%Y/%m/%d')");


            // retrieve the data frame
            DataFrame dataset = engine.Evaluate("Meters.DF").AsDataFrame();

            for (int i = 0; i < dataset.ColumnCount; ++i)
            {
                dataGridView1.ColumnCount++;
                dataGridView1.Columns[i].Name = dataset.ColumnNames[i];
            }

            for (int i = 0; i < dataset.RowCount; ++i)
            {
                dataGridView1.RowCount++;
                dataGridView1.Rows[i].HeaderCell.Value = dataset.RowNames[i];

                for (int k = 0; k < dataset.ColumnCount; ++k)
                {
                    dataGridView1[k, i].Value = dataset[i, k];

                }

            }

        }

        catch
        {
            MessageBox.Show(@"Equation error." + whichError);
        }
    }
Oct 17, 2013 at 5:13 AM
I think what is missing is the call to REngine.Initialize(). A colleague overlooked the same thing a couple days ago, so I'll chenge things to make the error message clearer.
The path setup process you use looks fine.

I wrote a working hello world sample in the recent past, that should clarify the essential steps for the current version. I just reran it as it happens, albeit on Linux.
https://github.com/jmp75/rdotnet-onboarding/blob/master/HelloWorld/Program.cs

The project creator, Kosei, and I have been working to further automate the setup steps, but this is under development.

I am growing a set of use cases and sample codes at the following location, that you may want to check out if this helps. Feedback welcome.
https://github.com/jmp75/rdotnet-onboarding

Regards
Oct 17, 2013 at 6:52 AM
Appreciate the help with this. Your set of sample files is a great addition to the R.NET needs. I attempted to implement those components into my code and downloaded the entire package and ran the sample solution and am getting the same error:

Error in .Call("R_isMethodsDispatchOn", onOff, PACKAGE = "base") :
"R_isMethodsDispatchOn" not available for .Call() for package "base"
Error in list.files(lib, pattern = paste0("^", pkg, "$"), full.names = TRUE) :
7 arguments passed to .Internal(list.files) which requires 8
Error: could not find function "str"

I am wondering if there is a setup or something other I need to do in the R environment. Will continue to troubleshoot but if you have seen this error, any thoughts?
Oct 17, 2013 at 7:15 AM
Sound like R cannot find its home, namely via its R_HOME environment variable, and from this on fails to load the standard packages that come with R.

Normally R on Windows should be able to find this by itself, even when set up from R. Never sought exactly what it is doing.

For some reasons on some Windows boxes this seems to fail. I'd really like to finally exactly understand why.

Meanwhile here are a few posts that sound related, including with some happy endings and workarounds that may work for you:
https://rdotnet.codeplex.com/discussions/269996
https://rdotnet.codeplex.com/discussions/444463

searching the discussions for the strings (single quote included) 'base' or 'stats' brings possibly related posts: e.g. https://rdotnet.codeplex.com/discussions?searchText=%27base%27

I note that https://rdotnet.codeplex.com/discussions/431376 went unanswered at the time; unfortunately... Day Work workload in the way.
Oct 18, 2013 at 5:12 PM
Good news. The setup helper code works great, loading libraries and running statements and can finally test the R.NET functionality. Thank you so much for your help!