gpt4 book ai didi

c# - R 引擎未初始化

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:20 25 4
gpt4 key购买 nike

static void Main(string[] args)
{
// Set the folder in which R.dll locates.
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = @"C:\R-3.0.2\bin\i386\";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
using (REngine engine = REngine.CreateInstance("RDotNet"))
{
// Initializes settings.
engine.Initialize(); // After Executing this line its crashing.

NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
engine.SetSymbol("group1", group1);
NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();

// Test difference of mean and get the P-value.
GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
double p = 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);
Console.ReadLine();
}

嗨当我执行上面的代码时,它在初始化时崩溃了。操作系统是 Windows XP sp3(32 位)R 版本- R-3.0.2使用 R.Net(1.5 版本)

请帮助我从 C# 连接到 R

最佳答案

我认为引擎崩溃是因为你有一些 R 路径错误。最好从 Windows 注册表中读取路径。

尝试这样的事情:

using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\R-core\R")){
var envPath = Environment.GetEnvironmentVariable("PATH");
string rBinPath = (string)registryKey.GetValue("InstallPath");
string rVersion = (string)registryKey.GetValue("Current Version");
rBinPath = System.Environment.Is64BitProcess ? rBinPath + "\\bin\\x64" :
rBinPath + "\\bin\\i386";
Environment.SetEnvironmentVariable("PATH",
envPath + Path.PathSeparator + rBinPath);
}
using (REngine engine = REngine.CreateInstance("RDotNet")){
// same code here
}

当然你应该添加正确的引用:

using Microsoft.Win32;
using RDotNet;
using System.IO;

关于c# - R 引擎未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20142528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com