3

I'm Creating a Console app with C# in wich i have to make some data analysis using R.NET. I already created multiple R fuctions that i'll be using and i tested them in RStudio and everything works fine. Suppose that my source file has a function called "delete" that take a vector as parameter.

1- Now i want to import the R source file that contains my functions to my Console app to be able to call these functions directly from there. I found this code:

using RDotNet;
{ ....
REngine engine = REngine.GetInstance();
engine.Evaluate("source('PATH/file.R");
... }

It doesn't get any error but i'm not sure if it's working. Now if i want to execute my function "delete", what is the command for that??

2- The source file is now situated on my desktop, but i want my app to run on any computer, so how can i add my R source file to the VS project to make it portable?

Motoko
  • 87
  • 11
  • I believe you have to install the following package in your .NET console app - https://www.nuget.org/packages/R.NET.Community/ – Alex Apr 21 '16 at 11:18
  • yes i already did. i'm wondering about the command to execute a function in a source file – Motoko Apr 21 '16 at 13:30
  • I found an answer in this [stackoverflow](http://stackoverflow.com/questions/4485943/executing-r-script-programmatically) – Motoko Apr 28 '16 at 09:09

1 Answers1

0

I ran into this issue and after hours of searching I realized that C# does not like the path given for the R file because it used the slashes as escape keys.

The following path worked for me once I added 4 backslashes in front of each directory:

using RDotNet;
using System;

namespace SimpleScriptingTest
{
    class Program
    {
        static void Main(string[] args)
        {            
            REngine.SetEnvironmentVariables();

            REngine engine = REngine.GetInstance();

            engine.Initialize();
            var path = "C:\\\\Program Files\\\\R\\\\RWDir\\\\HelloWorldTest.R";
            engine.Evaluate("source('" + path + "')");
            Console.ReadLine();
        }

    }
}
Jwoody30
  • 11
  • 1
  • You can also use the "verbatim" string literal syntax to avoid having C# treat backslashes as escape characters: `engine.Evaluate(@"source('C:\\Program Files\\...')");`. – StriplingWarrior Feb 26 '20 at 00:26