3

I'm creating a console app with C# in which I have to perform some data analysis using R.NET. I already created multiple R functions 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?

  1. 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?
3
  • I believe you have to install the following package in your .NET console app - nuget.org/packages/R.NET.Community Commented Apr 21, 2016 at 11:18
  • yes i already did. i'm wondering about the command to execute a function in a source file Commented Apr 21, 2016 at 13:30
  • I found an answer in this stackoverflow Commented Apr 28, 2016 at 9:09

1 Answer 1

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();
        }

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use the "verbatim" string literal syntax to avoid having C# treat backslashes as escape characters: engine.Evaluate(@"source('C:\\Program Files\\...')");.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.