VS、C#配置R语言开发环境

时间:2023-03-10 01:40:28
VS、C#配置R语言开发环境

R语言学习笔记(一)——在Vs、C#中配置R语言开发环境。

最近在学习小众的R语言,所以将遇到的问题记录下来供大家参考,不足之处欢迎大家交流指正。

至于R语言的介绍就不多说了,它集成了复杂的数学算法,将之封装成简单函数,开发者可以直接调用,使用得当绝对是一把利器。

配置前准备:

1.R语言安装包,因为是开源的所以大家可以直接去官网下载。https://cran.r-project.org/src/base/R-3/

官网最新版是3.6.1,我这是使用的是3.4.1。

安装包P地址

链接:https://pan.baidu.com/s/16Z4gD9uhIdDoqttkrJ7KBw
提取码:xbj2

注意:R语言的安装包版本要与下面的引用类库版本兼容,不然就会出现

engine = REngine.GetInstance(); engine = null的情况。

我这里使用的3.4.1与类库版本亲测兼容,大家嫌麻烦可以直接使用我的。但是版本最高支持.netFrameWork 4.5。

2.R环境的引用类库。

直接网盘奉献:

链接:https://pan.baidu.com/s/1wYSLbXDs3CD6hFp5tcPKHg
提取码:w2lo

正式开始:
一.打开下载好的安装包,注意:要用管理员权限打开。

然后一步一步下一步。

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

下一步安装即可。

二.打开VS,我这里是2012。

1.新建控制台

2.添加引用

VS、C#配置R语言开发环境

VS、C#配置R语言开发环境

3.可以将下面这段代码拷走测试

先设置R语言路径、环境,后进行函数调用。

这里给了两种测试代码。

         static void Main(string[] args)
{
Program mypro = new Program();
mypro.ExcuteCode();
}
private REngine engine; #region 测试代码
public void ExcuteCode()
{
InitREngine();
#region Test1 using (engine = REngine.GetInstance(null, true, null, null))
{
engine.Initialize(); // required since v1.5
CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
engine.SetSymbol("greetings", charVec);
engine.Evaluate("str(greetings)"); // print out in the console
string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
} Console.ReadKey(); #endregion #region Test2 // 初始化R环境
//engine = REngine.GetInstance(null, true, null, null); //// .NET Framework array to R vector.
//NumericVector group1 = 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.
//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); //// you should always dispose of the REngine properly.
//// After disposing of the engine, you cannot reinitialize nor reuse it
//engine.Dispose();
//Console.ReadKey(); #endregion }
#endregion #region 配置R环境
public void InitREngine()
{
var oldPath = System.Environment.GetEnvironmentVariable("PATH"); /////C:\Program Files\R\R-3.5.1\bin
var rPath = System.Environment.Is64BitProcess
? @"C:\Program Files\R\R-3.4.1\bin\x64"
: @"C:\Program Files\R\R-3.4.1\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);
// NOTE: you may need to set up R_HOME manually also on some machines
string rHome = "";
var platform = Environment.OSVersion.Platform;
switch (platform)
{
case PlatformID.Win32NT:
break; // R on Windows seems to have a way to deduce its R_HOME if its R.dll is in the PATH
case PlatformID.MacOSX:
rHome = "/Library/Frameworks/R.framework/Resources";
break;
case PlatformID.Unix:
rHome = "/usr/lib/R";
break;
default:
throw new NotSupportedException(platform.ToString());
} if (!string.IsNullOrEmpty(rHome))
{
Environment.SetEnvironmentVariable("R_HOME", rHome);
}
}
#endregion

持续更博中...