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,我这里是2012。
1.新建控制台
2.添加引用
3.可以将下面这段代码拷走测试
先设置R语言路径、环境,后进行函数调用。
这里给了两种测试代码。
1 static void Main(string[] args) 2 { 3 Program mypro = new Program(); 4 mypro.ExcuteCode(); 5 } 6 private REngine engine; 7 8 #region 测试代码 9 public void ExcuteCode() 10 { 11 InitREngine(); 12 #region Test1 13 14 using (engine = REngine.GetInstance(null, true, null, null)) 15 { 16 engine.Initialize(); // required since v1.5 17 CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" }); 18 engine.SetSymbol("greetings", charVec); 19 engine.Evaluate("str(greetings)"); // print out in the console 20 string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray(); 21 } 22 23 Console.ReadKey(); 24 25 #endregion 26 27 #region Test2 28 29 // 初始化R环境 30 //engine = REngine.GetInstance(null, true, null, null); 31 32 //// .NET Framework array to R vector. 33 //NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 }); 34 //engine.SetSymbol("group1", group1); 35 //// Direct parsing from R script. 36 //NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric(); 37 38 //// Test difference of mean and get the P-value. 39 //GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList(); 40 //double p = testResult["p.value"].AsNumeric().First(); 41 42 //Console.WriteLine("Group1: [{0}]", string.Join(", ", group1)); 43 //Console.WriteLine("Group2: [{0}]", string.Join(", ", group2)); 44 //Console.WriteLine("P-value = {0:0.000}", p); 45 46 //// you should always dispose of the REngine properly. 47 //// After disposing of the engine, you cannot reinitialize nor reuse it 48 //engine.Dispose(); 49 //Console.ReadKey(); 50 51 #endregion 52 53 } 54 #endregion 55 56 #region 配置R环境 57 public void InitREngine() 58 { 59 var oldPath = System.Environment.GetEnvironmentVariable("PATH"); /////C:\Program Files\R\R-3.5.1\bin 60 var rPath = System.Environment.Is64BitProcess 61 ? @"C:\Program Files\R\R-3.4.1\bin\x64" 62 : @"C:\Program Files\R\R-3.4.1\bin\i386"; 63 if (Directory.Exists(rPath) == false) 64 { 65 throw new DirectoryNotFoundException( 66 string.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath)); 67 } 68 69 var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath); 70 System.Environment.SetEnvironmentVariable("PATH", newPath); 71 // NOTE: you may need to set up R_HOME manually also on some machines 72 string rHome = ""; 73 var platform = Environment.OSVersion.Platform; 74 switch (platform) 75 { 76 case PlatformID.Win32NT: 77 break; // R on Windows seems to have a way to deduce its R_HOME if its R.dll is in the PATH 78 case PlatformID.MacOSX: 79 rHome = "/Library/Frameworks/R.framework/Resources"; 80 break; 81 case PlatformID.Unix: 82 rHome = "/usr/lib/R"; 83 break; 84 default: 85 throw new NotSupportedException(platform.ToString()); 86 } 87 88 if (!string.IsNullOrEmpty(rHome)) 89 { 90 Environment.SetEnvironmentVariable("R_HOME", rHome); 91 } 92 } 93 #endregion
持续更博中...