1

我正在尝试让 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。如果我执行代码,我会收到错误:“Value out of range” at Dim group1 As NumericVector = engine.CreateNumericVector(New Double() {30.02, 29.99, 30.11, 29.97, 30.01, 29.99})

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

仅供参考:我检查了路径并设置了所有需要的参考。

4

1 回答 1

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")
于 2013-11-09T10:00:09.860 回答