[vb.net] Sample Code

Nov 7, 2013 at 10:39 AM
Hello!

I am desperately trying to run a sample code with vb.net. Running the following code the debugger doesnt give an exception but stops at engine.Initialize(). Since my path is the same as in the example I didnt change it.

Here is the code I translated from the C# example. It would be so great if someone could help me get my first example getting work:

Imports System.IO

Namespace RDotNetSetup
Public Class SetupHelper
    Public Shared Sub SetupPath()
        Dim oldPath = System.Environment.GetEnvironmentVariable("PATH")

        Dim rPath = If(System.Environment.Is64BitProcess, "C:\Program Files\R\R-3.0.2\bin\x64", "C:\Program Files\R\R-3.0.2\bin\i386")

        If Directory.Exists(rPath) = False Then
            Throw New DirectoryNotFoundException(String.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath))
        End If

        Dim newPath = String.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath)
        System.Environment.SetEnvironmentVariable("PATH", newPath)
    End Sub
End Class
End Namespace

Imports RDotNet
Imports ConsoleApplication36.RDotNetSetup
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks

Module Module1
Sub Main()
    SetupHelper.SetupPath()
    Using engine As REngine = REngine.CreateInstance("RDotNet")
        ' From v1.5, REngine requires explicit initialization.
        ' You can set some parameters.
        engine.Initialize()

        Dim charVec As CharacterVector = engine.CreateCharacterVector({"Hello, R world!, .NET speaking"})

        engine.SetSymbol("greetings", charVec)
        engine.Evaluate("str(greetings)")
        ' print out in the console
        Dim a As String() = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray()
        Console.WriteLine("R answered: '{0}'", a(0))
        Console.WriteLine("Press any key to exit the program")
        Console.ReadKey()
    End Using
End Sub
End Module
Nov 8, 2013 at 8:30 PM
nobody who can give me an vb.net samplecode?
Nov 8, 2013 at 8:37 PM
Edited Nov 8, 2013 at 8:39 PM
.
Nov 8, 2013 at 8:56 PM
sorry but the link u posted is c# code.
Nov 8, 2013 at 9:11 PM
You need to set the R_HOME environment.

System.Environment.SetEnvironmentVariable("R_HOME", "C:\Program Files\R\R-3.0.2")
Nov 8, 2013 at 9:58 PM
Hello. I changed my code to
Module Module1
    Sub Main()
        System.Environment.SetEnvironmentVariable("R_HOME", "C:\Program Files\R\R-3.0.2\")
        Using engine As REngine = REngine.CreateInstance("RDotNet")
            ' From v1.5, REngine requires explicit initialization.
            ' You can set some parameters.
            engine.Initialize()
            Dim charVec As CharacterVector = engine.CreateCharacterVector({"Hello, R world!, .NET speaking"})
            engine.SetSymbol("greetings", charVec)
            engine.Evaluate("str(greetings)")
            ' print out in the console
            Dim a As String() = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray()
            Console.WriteLine("R answered: '{0}'", a(0))
            Console.WriteLine("Press any key to exit the program")
            Console.ReadKey()
        End Using
    End Sub
End Module
I use .net 4.0. Now I get the error: system.DllNotFoundException" occurd in RDotNet.NativeLibrary.dll. Any ideas to solve that?
Nov 8, 2013 at 10:25 PM
I meant you should add the R_HOST setting to the SetupHelper.
You need the PATH set too.
Your original code worked after adding that one line of code.
Nov 9, 2013 at 8:54 AM
yes, it works!!! what i had to change was:
SolutionManager -> Compile -> from "AnyCPU" to "x68" and now it works!
scubaskier: Thanks for your help!