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
View Code

 

持續更博中...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM