如何让R.Net与VB.Net一起工作

时间:2013-11-07 13:26:35

标签: vb.net r r.net

我试图让R.Net使用VB.NET。我将一个正式的例子从c#翻译成了vb.net,但它没有用。我尝试过不同的东西。首先,我使用了官方页面上解释的SetupHelper。

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")
    engine.Initialize()
    Dim charVec As CharacterVector = engine.CreateCharacterVector({"Hello, R world!, .NET speaking"})
    engine.SetSymbol("greetings", charVec)
    engine.Evaluate("str(greetings)")
    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

我没有收到错误,在引擎上使用调试器。初始化我的测试停止运行(重新出现绿色的开始箭头)。

所以我找到了另一个应该(显然)可以在VB.Net上运行的例子

Imports RDotNet
Imports System.IO
Imports System.Linq

Module Module1

Sub Main()
    Dim envPath = System.Environment.GetEnvironmentVariable("PATH")
    Dim rBinPath = "C:\Program Files\R\R-3.0.2\bin\i386"
    System.Environment.SetEnvironmentVariable("PATH", envPath & Path.PathSeparator & rBinPath)
    Dim engine As REngine = REngine.CreateInstance("RDotNet")
    Dim group1 As NumericVector = engine.CreateNumericVector(New Double() {30.02, 29.99, 30.11, 29.97, 30.01, 29.99})
    engine.SetSymbol("group1", group1)
    ' Direct parsing from R script.
    Dim s = "group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)"
    Dim group2 = engine.Evaluate(s).AsNumeric()
    Dim testResult As GenericVector = engine.Evaluate("t.test(group1, group2)").AsList()
    Dim p As Double = testResult("p.value").AsNumeric().First()
    Console.WriteLine("Group1: [{0}]", String.Join(", ", group1))
    Console.WriteLine("Group2: [{0}]", String.Join(", ", group2))
    Console.WriteLine("P-value = {0:0.000}", p)
End Sub
End Module

看起来这位作家有同样的问题,只是离开了engine.initialize。如果我执行代码,我会在Dim group1 As NumericVector = engine.CreateNumericVector(New Double() {30.02, 29.99, 30.11, 29.97, 30.01, 29.99})处收到错误:“值超出范围”。

任何可以帮助我获取VB.NET示例代码的人都可以使用吗?并解释我为什么不能初始化。

fyi:我检查了路径并设置了所有需要的引用。

1 个答案:

答案 0 :(得分:0)

好的,稍后尝试和讨论的时间会有效。我做了什么:

  1. 我将.net 4.5更改为.net 4.0
  2. 我将编译器设置从“AnyCPU”更改为“x86”
  3. SetupPath()”中的第一行是:System.Environment.SetEnvironmentVariable("R_HOME", "C:\Program Files\R\R-3.0.2")