|
8 | 8 |
|
9 | 9 | namespace Sample2 |
10 | 10 | { |
11 | | - class Program |
12 | | - { |
13 | | - static void Main(string[] args) |
14 | | - { |
15 | | - // Sample code used for updating the documentation at the codeplex web site. |
16 | | - using (REngine engine = REngine.GetInstance()) |
17 | | - { |
18 | | - var e = engine.Evaluate("x <- 3"); |
19 | | - // You can now access x defined in the R environment. |
20 | | - NumericVector x = engine.GetSymbol("x").AsNumeric(); |
21 | | - engine.Evaluate("y <- 1:10"); |
22 | | - NumericVector y = engine.GetSymbol("y").AsNumeric(); |
23 | | - |
24 | | - // Invoking functions; Previously you may have needed custom function definitions |
25 | | - var myFunc = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction(); |
26 | | - var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 }); |
27 | | - var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" }); |
28 | | - var df = myFunc.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame(); |
29 | | - |
30 | | - // As of R.NET 1.6, more function call syntaxes are supported. |
31 | | - var expandGrid = engine.Evaluate("expand.grid").AsFunction(); |
32 | | - var d = new Dictionary<string, SymbolicExpression>(); |
33 | | - d["x"] = v1; |
34 | | - d["y"] = v2; |
35 | | - df = expandGrid.Invoke(d).AsDataFrame(); |
36 | | - |
37 | | - // querying data frames |
38 | | - engine.SetSymbol("cases", df); |
39 | | - // As of R.NET 1.6, factor to character expressions work consistently with R |
40 | | - |
41 | | - var letterCases = engine.Evaluate("cases[,'y']").AsCharacter().ToArray(); |
42 | | - // "a","a","a","b","b","b", etc. Same as as.character(cases[,'y']) in R |
43 | | - // This used to return "1", "1", "1", "2", "2", etc. with R.NET 1.5.5 |
44 | | - |
45 | | - // Equivalent: |
46 | | - letterCases = df[1].AsCharacter().ToArray(); |
47 | | - letterCases = df["y"].AsCharacter().ToArray(); |
48 | | - |
49 | | - // Accessing items by two dimensional indexing |
50 | | - string s = (string)df[1, 1]; // "a" |
51 | | - s = (string)df[3, 1]; // "a" |
52 | | - s = (string)df[3, "y"]; // "b" |
53 | | - // s = (string)df["4", "y"]; // fails because there are no row names |
54 | | - df[3, "y"] = "a"; |
55 | | - s = (string)df[3, "y"]; // "a" |
56 | | - df[3, "y"] = "d"; |
57 | | - s = (string)df[3, "y"]; // null, because we have an <NA> string in R |
58 | | - |
59 | | - // invoking a whole script |
60 | | - // engine.Evaluate("source('c:/src/path/to/myscript.r')"); |
61 | | - |
62 | | - // TODO |
63 | | - // Date-time objects |
64 | | - |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - |
69 | | - // Investigate use of the Defer method. Deprecated? |
70 | | - private static string DeferedStatementRepro(REngine engine) |
71 | | - { |
72 | | - // Looking at the issues reported in https://rdotnet.codeplex.com/discussions/458547 |
73 | | - // indeed the R.NET documentation page as of 22 Sept 2013 is off as to what Defer method there is. |
74 | | - // The following memory stream is a workaround out of curiosity: not something intended for ongoing use. |
75 | | - // With the memory stream workaround, it bombs. |
76 | | - // Not sure whether this is because of the conversion to byte, but unlikely to be a false positive. |
77 | | - string deferedStatement = "x <- 3"; |
78 | | - |
79 | | - var byteArr = Array.ConvertAll(deferedStatement.ToArray(), c => Convert.ToByte(c)); |
80 | | - using (var stream = new MemoryStream(byteArr)) |
81 | | - { |
82 | | - // Defer method delays an effect on the R environment. |
83 | | - var e = engine.Defer(stream); |
84 | | - // Error: GetSymbol method returns null at the moment. |
85 | | - // NumericVector x = engine.GetSymbol("x").AsNumeric(); |
86 | | - |
87 | | - // Evaluates the statement. |
88 | | - e.ToArray(); |
89 | | - } |
90 | | - return deferedStatement; |
91 | | - } |
92 | | - } |
| 11 | + class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + // Sample code used for updating the documentation at the codeplex web site. |
| 16 | + using (REngine engine = REngine.GetInstance()) |
| 17 | + { |
| 18 | + var e = engine.Evaluate("x <- 3"); |
| 19 | + // You can now access x defined in the R environment. |
| 20 | + NumericVector x = engine.GetSymbol("x").AsNumeric(); |
| 21 | + engine.Evaluate("y <- 1:10"); |
| 22 | + NumericVector y = engine.GetSymbol("y").AsNumeric(); |
| 23 | + |
| 24 | + // Invoking functions; Previously you may have needed custom function definitions |
| 25 | + var myFunc = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction(); |
| 26 | + var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 }); |
| 27 | + var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" }); |
| 28 | + var df = myFunc.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame(); |
| 29 | + |
| 30 | + // As of R.NET 1.6, more function call syntaxes are supported. |
| 31 | + var expandGrid = engine.Evaluate("expand.grid").AsFunction(); |
| 32 | + var d = new Dictionary<string, SymbolicExpression>(); |
| 33 | + d["x"] = v1; |
| 34 | + d["y"] = v2; |
| 35 | + df = expandGrid.Invoke(d).AsDataFrame(); |
| 36 | + |
| 37 | + // querying data frames |
| 38 | + engine.SetSymbol("cases", df); |
| 39 | + // As of R.NET 1.6, factor to character expressions work consistently with R |
| 40 | + |
| 41 | + engine.Evaluate("head(cases)"); |
| 42 | + |
| 43 | + /* |
| 44 | + x y |
| 45 | +1 1 a |
| 46 | +2 2 a |
| 47 | +3 3 a |
| 48 | +4 1 b |
| 49 | +5 2 b |
| 50 | +6 3 b */ |
| 51 | + |
| 52 | + var letterCases = engine.Evaluate("cases[,'y']").AsCharacter().ToArray(); |
| 53 | + // "a","a","a","b","b","b", etc. Same as as.character(cases[,'y']) in R |
| 54 | + // This used to return "1", "1", "1", "2", "2", etc. with R.NET 1.5.5 |
| 55 | + |
| 56 | + // Equivalent: |
| 57 | + letterCases = df[1].AsCharacter().ToArray(); |
| 58 | + letterCases = df["y"].AsCharacter().ToArray(); |
| 59 | + |
| 60 | + // Accessing items by two dimensional indexing |
| 61 | + string s = (string)df[1, 1]; // "a" |
| 62 | + s = (string)df[3, 1]; // "a" |
| 63 | + s = (string)df[3, "y"]; // "b" |
| 64 | + // s = (string)df["4", "y"]; // fails because there are no row names |
| 65 | + df[3, "y"] = "a"; |
| 66 | + s = (string)df[3, "y"]; // "a" |
| 67 | + df[3, "y"] = "d"; |
| 68 | + s = (string)df[3, "y"]; // null, because we have an <NA> string in R |
| 69 | + |
| 70 | + // invoking a whole script |
| 71 | + // engine.Evaluate("source('c:/src/path/to/myscript.r')"); |
| 72 | + |
| 73 | + // TODO |
| 74 | + // Date-time objects |
| 75 | + |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + // Investigate use of the Defer method. Deprecated? |
| 81 | + private static string DeferedStatementRepro(REngine engine) |
| 82 | + { |
| 83 | + // Looking at the issues reported in https://rdotnet.codeplex.com/discussions/458547 |
| 84 | + // indeed the R.NET documentation page as of 22 Sept 2013 is off as to what Defer method there is. |
| 85 | + // The following memory stream is a workaround out of curiosity: not something intended for ongoing use. |
| 86 | + // With the memory stream workaround, it bombs. |
| 87 | + // Not sure whether this is because of the conversion to byte, but unlikely to be a false positive. |
| 88 | + string deferedStatement = "x <- 3"; |
| 89 | + |
| 90 | + var byteArr = Array.ConvertAll(deferedStatement.ToArray(), c => Convert.ToByte(c)); |
| 91 | + using (var stream = new MemoryStream(byteArr)) |
| 92 | + { |
| 93 | + // Defer method delays an effect on the R environment. |
| 94 | + var e = engine.Defer(stream); |
| 95 | + // Error: GetSymbol method returns null at the moment. |
| 96 | + // NumericVector x = engine.GetSymbol("x").AsNumeric(); |
| 97 | + |
| 98 | + // Evaluates the statement. |
| 99 | + e.ToArray(); |
| 100 | + } |
| 101 | + return deferedStatement; |
| 102 | + } |
| 103 | + } |
93 | 104 | } |
0 commit comments