1. https://mirrors.tuna.tsinghua.edu.cn/CRAN/ 安装R3.4.4所有安装最好不要改路径
2.https://mirrors.tuna.tsinghua.edu.cn/CRAN/ 下载DCOM安装
3.https://www.nuget.org/packages/R.NET.Community/1.6.5 VS安装R.NET
程序包管理控制台:Install-Package R.NET.Community -Version 1.6.5
C#程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDotNet;
using System.IO;
namespace CSharpCallR
{
class Program
{
private REngine engine = REngine.GetInstance();
public Program()
{
initREngine();
}
public void initREngine()
{
// RDoNet环境初始化
var oldPath = System.Environment.GetEnvironmentVariable("PATH");
// var rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.4.4\bin\x64" : @"C:\Program Files\R\R-3.4.4\bin\i386";
var rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.4.4\bin\x64" : @"C:\Program Files\R\R-3.4.4\bin\i386";
if (Directory.Exists(rPath) == false)
throw new DirectoryNotFoundException(string.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath));
var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);
System.Environment.SetEnvironmentVariable("PATH", newPath);
REngine.SetEnvironmentVariables();// There are several options to initialize the engine, but by default the following suffice:
LoadRenviron();
}
/// <summary>
/// 加载R包,设定函数
/// </summary>
public void LoadRenviron()
{
string code = @"
.libPaths('C:\\Users\\lenovo\\Documents\\Visual Studio 2013\\Projects\\CSharpCallR\\packages')#设置安装包路径,之后可以把离线下载的包放进来
library(entropy)
cal=function(x)
{
n=c(1:100)
for(i in 1:100)
{
n[i]=length(which(x>(0.03*i)&x<(0.03*(i+1))))
}
entropy(n)
}
";
engine.Evaluate(code);//执行
}
public double calEntroy(IEnumerable<double> values)
{
REngine engine = REngine.GetInstance();
//传参
NumericVector loadData = engine.CreateNumericVector(values);
engine.SetSymbol("loadData", loadData);
string code = @"
entro=cal(loadData)
";
engine.Evaluate(code);
//提取结果
NumericVector res = engine.GetSymbol("entro").AsNumeric();
double entro = Convert.ToDouble(res[0].ToString());
return entro;
}
static void Main(string[] args)
{
double[] num = { 1.2, 2.3, 3.4, 5.5, 4, 4, 4.4, 3, 2 };
Program mypro=new Program();
mypro.initREngine();
double x=mypro.calEntroy(num);
Console.WriteLine(x);
Console.ReadKey();
}
}
}
问题解析:
1.错误 2 “RDotNet.REngine”并不包含“GetInstance”的定义 c:\users\边远\documents\visual studio 2013\Projects\CsharpCallR\CsharpCallR\Program.cs 16 38 CsharpCallR
错误 1 “RDotNet.REngine”并不包含“SetEnvironmentVariables”的定义 c:\users\边远\documents\visual studio 2013\Projects\CsharpCallR\CsharpCallR\Program.cs 14 21 CsharpCallR
解决方案:换1.6.5的包,之前1.5.5的不行
2. Windows Registry key 'SOFTWARE\R-core' not found in HKEY_LOCAL_MACHINE nor HKEY_CURRENT_USER
解决方案:重新安装R和DCOM不要该路径
3. Warning in install.packages("TSA") :
'lib="C:/Program Files/R/R-3.4.4/library"'不可写
解决方案:得用管理员方式打开
4.package ‘wavelets’ is not available (for R version 3.4.4)
解决方案:开翻墙软件。。
5.怎么使用离线的R安装包
设置R环境的安装包路径,将下语句加入到code,执行就行.
libPaths('C:\\Users\\lenovo\\Documents\\Visual Studio 2013\\Projects\\CSharpCallR\\packages')