public void As2DArrayInt_SexpConstructedUsingIntOrIEnumerableOfInt_ThrowsNotSupportedException() { // Arrange // ReSharper disable RedundantExplicitArraySize var values1 = new int[1] { 2 }; var values2 = new int[2] { 3, 5 }; var values3 = new int[4] { 8, SexpArrayInt.Na, 7, 4 }; const int values4 = 5; // ReSharper restore RedundantExplicitArraySize // Act Sexp sexp1 = Sexp.Make(values1); Sexp sexp2 = Sexp.Make(values2); Sexp sexp3 = Sexp.Make(values3); Sexp sexp4 = Sexp.Make(values4); // Assert Assert.Throws <NotSupportedException>(() => sexp1.As2DArrayInt); Assert.Throws <NotSupportedException>(() => sexp2.As2DArrayInt); Assert.Throws <NotSupportedException>(() => sexp3.As2DArrayInt); Assert.Throws <NotSupportedException>(() => sexp4.As2DArrayInt); }
public void AsDates_SexpConstructedUsingConstructorOrMake_ReturnsSameSetOfDates() { // Arrange var dates1 = new DateTime(1953, 10, 12); var dates2 = new[] { new DateTime(1970, 1, 1), new DateTime(1970, 1, 2), new DateTime(1970, 1, 3), new DateTime(2012, 10, 12), new DateTime(1953, 10, 12) }; var dates3 = new DateTime[] { }; var dates4 = new[] { 0, 1, 2, 15625, -5925 }; // Act Sexp sexp1A = Sexp.Make(dates1); Sexp sexp1B = new SexpArrayDate(dates1); Sexp sexp2A = Sexp.Make(dates2); Sexp sexp2B = new SexpArrayDate(dates2); Sexp sexp3A = Sexp.Make(dates3); Sexp sexp3B = new SexpArrayDate(dates3); Sexp sexp4 = new SexpArrayDate(dates4); // Assert Assert.Equal(new [] { dates1 }, sexp1A.AsDates); Assert.Equal(new[] { dates1 }, sexp1B.AsDates); Assert.Equal(dates2, sexp2A.AsDates); Assert.Equal(dates2, sexp2B.AsDates); Assert.Equal(dates3, sexp3A.AsDates); Assert.Equal(dates3, sexp3B.AsDates); Assert.Equal(dates2, sexp4.AsDates); }
public void AsInts_SexpConstructedUsingConstructorOrMake_ReturnsSameSetOfInts() { // Arrange var values1 = new int[1, 1] { { 2 } }; var values2 = new int[3, 4] { { 8, 2, 7, SexpArrayInt.Na }, { 0, -9, 5, -2 }, { 1, -4, -3, -8 } }; const int values3 = -5; var values4 = new List <int> { 4, 5, 6 }; // Act Sexp sexp1 = Sexp.Make(values1); Sexp sexp2 = Sexp.Make(values2); Sexp sexp3 = new SexpArrayInt(values3); Sexp sexp4 = new SexpArrayInt(values4); Sexp sexp5 = new SexpArrayInt(); // Assert Assert.Equal(new[] { 2 }, sexp1.AsInts); Assert.Equal(new[] { 8, 0, 1, 2, -9, -4, 7, 5, -3, SexpArrayInt.Na, -2, -8 }, sexp2.AsInts); Assert.Equal(new[] { -5 }, sexp3.AsInts); Assert.Equal(new[] { 4, 5, 6 }, sexp4.AsInts); Assert.Equal(new int[] { }, sexp5.AsInts); }
public void Various_ListTest() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var mylist = new Dictionary <string, object> { { "One", 1 }, { "Two", 2.0 }, { "Three", "three" }, { "Four", true }, { "Five", new DateTime(2012, 01, 01) } }; var x1 = Sexp.Make(mylist); service.RConnection["x1"] = x1; Assert.Equal(x1.Count, mylist.Count); service.RConnection.Eval("x2 <- list(One=1,Two=2.0,Three='three',Four=TRUE,Five=as.Date('2012-01-01'))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { Assert.True(x1[i].Equals(x2[i])); Assert.Equal(x1.Names[i], x2.Names[i]); Assert.True(x1[x1.Names[i]].Equals(x2[x2.Names[i]])); } } }
internal Sexp load(String fileName) { TextReader oldBr = this.br; try { FileStream iin = new FileStream(fileName, FileMode.Open, FileAccess.Read); this.br = new StreamReader(iin); init(); for (; ;) { String str = br.ReadLine(); if (str == null) { break; } Sexp sexp = readFromString(str); Sexp ret = YuLisp.lisp.eval.eval(sexp); } this.br.Close(); return(T.True); } catch (Exception) { this.br = oldBr; return(Nil.NIL); } }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = this.eval.eval(arg.car); Sexp arg2 = this.eval.eval(((List)arg.cdr).car); return((Sexp) new List(arg1, arg2)); }
public void AsDoubles_SexpConstructedUsingConstructorOrMake_ReturnsSameSetOfDoubles() { // Arrange var values1 = new double[1, 1] { { 2 } }; var values2 = new double[3, 4] { { 8, 2, 7, 4 }, { 0, double.NaN, 5, -2 }, { 1, -4, -3, -8 } }; const double values3 = -5d; var values4 = new List <double> { 4, 5, 6 }; // Act Sexp sexp1 = Sexp.Make(values1); Sexp sexp2 = Sexp.Make(values2); Sexp sexp3 = new SexpArrayDouble(values3); Sexp sexp4 = new SexpArrayDouble(values4); Sexp sexp5 = new SexpArrayDouble(); // Assert Assert.Equal(new double[] { 2 }, sexp1.AsDoubles); Assert.Equal(new[] { 8, 0, 1, 2, double.NaN, -4, 7, 5, -3, 4, -2, -8 }, sexp2.AsDoubles); Assert.Equal(new double[] { -5 }, sexp3.AsDoubles); Assert.Equal(new double[] { 4, 5, 6 }, sexp4.AsDoubles); Assert.Equal(new double[] { }, sexp5.AsDoubles); }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = eval.eval(arg.car); String fileName = ((LString)arg1).ValueOf(); return(YuLisp.lisp.read.load(fileName)); }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = this.eval.eval(arg.car); return((Sexp)YuLisp.lisp.read.readFromString( ((LString)arg1).ValueOf())); }
public static void TryGetValue_KeyExistsInTaggedList_AssignSexpToValueOutParameter() { // Arrange var list = new SexpTaggedList { new KeyValuePair <string, Sexp>("foo", Sexp.Make(1)), new KeyValuePair <string, Sexp>("bar", Sexp.Make(2)), new KeyValuePair <string, Sexp>("baz", Sexp.Make(3)), }; Sexp actual1; Sexp actual2; Sexp actual3; var expected1 = Sexp.Make(1); var expected2 = Sexp.Make(2); var expected3 = Sexp.Make(3); // Act list.TryGetValue("foo", out actual1); list.TryGetValue("bar", out actual2); list.TryGetValue("baz", out actual3); // Assert Assert.Equal(expected1, actual1, EqualityComparer <Sexp> .Default); Assert.Equal(expected2, actual2, EqualityComparer <Sexp> .Default); Assert.Equal(expected3, actual3, EqualityComparer <Sexp> .Default); }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = this.eval.eval(arg.car); Sexp arg2 = this.eval.eval(((List)arg.cdr).car); return(((LInteger)arg1).Add((LInteger)arg2)); }
/// <summary> /// Executes the program /// </summary> /// <param name="args">The arguments.</param> public static void Main(string[] args) { using (var s = new RConnection(new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 }))) { // Generate some example data var x = Enumerable.Range(1, 20).ToArray(); var y = (from a in x select(0.5 * a * a) + 2).ToArray(); // Build an R data frame var d = Sexp.MakeDataFrame(); d["x"] = Sexp.Make(x); d["y"] = Sexp.Make(y); s["d"] = d; // Run a linear regression, obtain the summary, and print the result s.VoidEval("linearModelSummary = summary(lm(y ~ x, d))"); var coefs = s["linearModelSummary$coefficients"]; var rSquared = s["linearModelSummary$r.squared"].AsDouble; Console.WriteLine("y = {0} x + {1}. R^2 = {2,4:F}%", coefs[1, 0], coefs[0, 0], rSquared * 100); // Now let's do some linear algebra var matA = new double[, ] { { 14, 9, 3 }, { 2, 11, 15 }, { 0, 12, 17 }, { 5, 2, 3 } }; var matB = new double[, ] { { 12, 25 }, { 9, 10 }, { 8, 5 } }; s["a"] = Sexp.Make(matA); s["b"] = Sexp.Make(matB); Console.WriteLine(s["a %*% b"].ToString()); } Console.WriteLine("Done"); }
public void Various_Bool() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var sexpTrue = new SexpArrayBool(true); var sexpFalse = new SexpArrayBool(false); var sexpNa = new SexpArrayBool(( bool? )null); // ReSharper disable EqualExpressionComparison Assert.True(sexpTrue.Equals(sexpTrue)); Assert.True(sexpFalse.Equals(sexpFalse)); Assert.True(sexpNa.Equals(sexpNa)); // ReSharper restore EqualExpressionComparison Assert.True(!sexpTrue.Equals(sexpFalse)); Assert.True(!sexpTrue.Equals(sexpNa)); Assert.True(sexpTrue.Equals(true)); Assert.True(sexpFalse.Equals(false)); Assert.True(!sexpNa.Equals(true)); Assert.True(!sexpNa.Equals(false)); foreach (var a in new Sexp[] { sexpTrue, sexpFalse, sexpNa }) { Assert.True(!a.Equals(new SexpNull())); } service.RConnection["x.bool"] = Sexp.Make(true); } }
public void As2DArrayDouble_SexpConstructedUsing2dArray_ReturnsSame2dArray() { // Arrange var values1 = new double[1, 1] { { 2.9 } }; var values2 = new double[2, 1] { { 3.9 }, { 5.6 } }; var values3 = new double[1, 2] { { 7.9, 4.3 } }; var values4 = new double[3, 4] { { 8.8, 2.1, 7.5, 4.3 }, { 0.1, -9.8, 5.1, -2.7 }, { 1.1, -4.6, -4.5, -8.7 } }; var values5 = new double[2, 1] { { 3.9 }, { double.NaN } }; // Act Sexp sexp1 = Sexp.Make(values1); Sexp sexp2 = Sexp.Make(values2); Sexp sexp3 = Sexp.Make(values3); Sexp sexp4 = Sexp.Make(values4); Sexp sexp5 = Sexp.Make(values5); // Assert Assert.Equal(values1, sexp1.As2DArrayDouble); Assert.Equal(values2, sexp2.As2DArrayDouble); Assert.Equal(values3, sexp3.As2DArrayDouble); Assert.Equal(values4, sexp4.As2DArrayDouble); Assert.Equal(values5, sexp5.As2DArrayDouble); }
public void As2DArrayInt_SexpConstructedUsing2dArray_ReturnsSame2dArray() { // Arrange var values1 = new int[1, 1] { { 2 } }; var values2 = new int[2, 1] { { 3 }, { 5 } }; var values3 = new int[1, 2] { { 7, 4 } }; var values4 = new int[3, 4] { { 8, 2, 7, 4 }, { 0, -9, SexpArrayInt.Na, -2 }, { 1, -4, -3, -8 } }; // Act Sexp sexp1 = Sexp.Make(values1); Sexp sexp2 = Sexp.Make(values2); Sexp sexp3 = Sexp.Make(values3); Sexp sexp4 = Sexp.Make(values4); // Assert Assert.Equal(values1, sexp1.As2DArrayInt); Assert.Equal(values2, sexp2.As2DArrayInt); Assert.Equal(values3, sexp3.As2DArrayInt); Assert.Equal(values4, sexp4.As2DArrayInt); }
public void As2DArrayDouble_SexpConstructedUsingDoubleOrIEnumerableOfDouble_ThrowsNotSupportedException() { // Arrange // ReSharper disable RedundantExplicitArraySize var values1 = new double[1] { 2.3 }; var values2 = new double[2] { 3.6, 5.2 }; var values3 = new double[4] { 8.3, 2.9, 7.1, -4.4 }; const double values4 = 4.4; // ReSharper restore RedundantExplicitArraySize // Act Sexp sexp1 = Sexp.Make(values1); Sexp sexp2 = Sexp.Make(values2); Sexp sexp3 = Sexp.Make(values3); Sexp sexp4 = Sexp.Make(values4); // Assert Assert.Throws <NotSupportedException>(() => sexp1.As2DArrayDouble); Assert.Throws <NotSupportedException>(() => sexp2.As2DArrayDouble); Assert.Throws <NotSupportedException>(() => sexp3.As2DArrayDouble); Assert.Throws <NotSupportedException>(() => sexp4.As2DArrayDouble); }
//S式定義関数の評価 protected Sexp SexpEval(List lambda, List body, List form) { //引数の評価 List arglist = lambda; int oldstackp = this.StackP; while (true) { Sexp arg = form.car; Sexp xxx = this.eval(arg); this.Stack[this.StackP] = xxx; this.StackP++; if (form.cdr == Nil.NIL) { break; } form = (List)form.cdr; arglist = (List)arglist.cdr; } //スタックへの退避 arglist = lambda; int sp = oldstackp; while (true) { Symbol sym = (Symbol)arglist.car; Sexp swap = sym.GetValue(); sym.SetValue(this.Stack[sp]); this.Stack[sp] = swap; sp++; if (arglist.cdr == Nil.NIL) { break; } arglist = (List)arglist.cdr; } //bodyの評価 Sexp ret = this.EvalBody(body); //スタックからの復帰 arglist = lambda; this.StackP = oldstackp; while (true) { Symbol sym = (Symbol)arglist.car; sym.SetValue(this.Stack[oldstackp]); oldstackp++; if (arglist.cdr == Nil.NIL) { break; } arglist = (List)arglist.cdr; } return(ret); }
public Sexp eval(Sexp form) { //シンボルの処理 if (Lib.Symbol(form)) { Sexp sexp = ((Symbol)form).GetValue(); if (sexp == null) { throw new Error(Lib.UNBOUND, form.serialize()); } return(sexp); } //アトムの処理 if (Lib.Atom(form)) { return(form); } //リストの処理 Sexp car = ((List)form).car; int argnum = ((List)form).size() - 1; if (Lib.Symbol(car)) { Sexp fun = ((Symbol)car).GetValue(); if (fun == null) { throw new Error(Lib.UNDEFINED, car.serialize()); } //carがFunctionの時 if (Lib.Function(fun)) { Sexp arglist = (argnum == 0) ? Nil.NIL : ((List)form).cdr; return(((Function)fun).fun((List)arglist, argnum)); } //carがListの時 if (Lib.List(fun)) { List cdr = (List)((List)fun).cdr; List lambdalist = (List)cdr.car; List body = (List)cdr.cdr; if (lambdalist == Nil.NIL) { return(this.EvalBody(body)); } return(this.SexpEval(lambdalist, body, (List)((List)form).cdr)); } throw new Error(Lib.NOTFUNCTION, car.serialize()); } throw new Error(Lib.NOTSYMBOL, car.serialize()); }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = arg.car; Sexp arg2 = ((List)arg.cdr).car; Symbol sym = (Symbol)arg1; Sexp value = eval.eval(arg2); sym.SetValue(value); return(value); }
public void IsNa_NaValueReadFromR_ReturnTrue() { using (var rWrapper = new Rservice()) { // Arrange & Act Sexp naSexp = rWrapper.RConnection["as.numeric( NA )"]; // Assert Assert.IsType <SexpArrayDouble>(naSexp); Assert.True(naSexp.IsNa); } }
public void Various_LinearAlgebraFunctionsOf2DArrayDouble_MeetsExpectation() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { // Same as for integers -- we'll divide by two to get floating point values that aren't integers var matA = new double[, ] { { 14, 9, 3 }, { 2, 11, 15 }, { 0, 12, 17 }, { 5, 2, 3 } }; var matB = new double[, ] { { 12, 25 }, { 9, 10 }, { 8, 5 } }; var matC = new double[, ] { { 273, 455 }, { 243, 235 }, { 244, 205 }, { 102, 160 } }; var sexpA = Sexp.Make(matA); service.RConnection["a"] = sexpA; service.RConnection["b"] = Sexp.Make(matB); // Some simple tests with A for (int i = 0; i <= 1; i++) { Assert.Equal(matA.GetLength(i), sexpA.GetLength(i)); Assert.Equal(matA.GetLength(i), service.RConnection["a"].GetLength(i)); } for (var row = 0; row < matA.GetLength(0); row++) { for (var col = 0; col < matA.GetLength(1); col++) { Assert.Equal(matA[row, col], sexpA[row, col].AsDouble); Assert.Equal(matA[row, col], service.RConnection["a"][row, col].AsDouble); } } var matD = service.RConnection["a %*% b"]; // check that C and D are equal for (var i = 0; i <= 1; i++) { Assert.Equal(matC.GetLength(i), matD.GetLength(i)); } for (var row = 0; row < matC.GetLength(0); row++) { for (var col = 0; col < matD.GetLength(1); col++) { Assert.Equal(matC[row, col], matD[row, col].AsDouble); } } } }
public void Various_ArrayBool() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var testvals = new bool?[] { true, false, null }; var x1 = new SexpArrayBool(testvals); Assert.Equal(testvals.Length, x1.Count); service.RConnection["x1"] = x1; for (int i = 0; i < x1.Count; i++) { Assert.Equal(new SexpArrayBool(testvals[i]).AsBool, x1[i].AsBool); } service.RConnection.Eval("x2 <- as.logical(c(TRUE,FALSE,NA))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { if (x1[i].IsNa) { Assert.True(x2[i].IsNa); } else { Assert.Equal(x1[i].AsBool, x2[i].AsBool); } } var equals = service.RConnection["x1 == x2"]; Assert.Equal(x1.Count, equals.Count); for (int i = 0; i < x1.Count; i++) { if (!x1[i].IsNa) { Assert.True(( bool )(equals[i].AsBool), equals.ToString()); } } Assert.Equal(x1.IndexOf(Sexp.Make(false)), 1); x1.AsList[0] = false; Assert.Equal(x1[0].AsBool, false); } }
public void Various_ArrayIntTests() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var testInts = new[] { -3, 0, 1, 2, 524566, 0 }; var x1 = Sexp.Make(testInts); x1[x1.Count - 1] = new SexpArrayInt(SexpArrayInt.Na); for (int i = 0; i < x1.Count; i++) { if (!x1[i].IsNa) { Assert.Equal(testInts[i], x1[i].AsInt); } } service.RConnection.Eval("x2 <- as.integer(c(-3,0,1,2,524566,NA))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { if (x1[i].IsNa) { Assert.True(x2[i].IsNa); } else { Assert.True(x1[i].AsDouble == x2[i].AsDouble); } } service.RConnection["x1"] = x1; var equals = service.RConnection["x1 == x2"]; Assert.Equal(x1.Count, equals.Count); for (int i = 0; i < x1.Count; i++) { if (!x1[i].IsNa) { Assert.True(( bool )equals[i].AsBool, equals.ToString()); } } Assert.Equal(x1.IndexOf(new SexpArrayInt(1)), 2); x1.AsList[0] = -5; Assert.Equal(x1[0].AsInt, -5); } }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = arg.car; Sexp args = arg.cdr; Symbol fun = (Symbol)arg1; List lambda = new List(); lambda.car = this.env.Get("LAMBDA"); lambda.cdr = args; fun.SetValue(lambda); return(fun); }
public void AsDoubles_SexpConstructedFromR_ReturnsSameSetOfDoubles() { using (var service = new Rservice()) { // Arrange & Act Sexp sexp1 = service.RConnection["numeric()"]; Sexp sexp2 = service.RConnection["c( 1.1 , 2.1 , 3.1 )"]; Sexp sexp3 = service.RConnection["matrix( c( 1.1 , NA , 3.1 , 4.1 , 5.1 , 6.1 ) , nrow = 2 )"]; // Assert Assert.Equal(new double[] { }, sexp1.AsDoubles); Assert.Equal(new[] { 1.1, 2.1, 3.1 }, sexp2.AsDoubles); Assert.Equal(new[] { 1.1, double.NaN, 3.1, 4.1, 5.1, 6.1 }, sexp3.AsDoubles); } }
public void AsInts_SexpConstructedFromR_ReturnsSameSetOfInts() { using (var service = new Rservice()) { // Arrange & Act Sexp sexp1 = service.RConnection["integer()"]; Sexp sexp2 = service.RConnection["as.integer( c( 1 , 2 , 3 ) )"]; Sexp sexp3 = service.RConnection["matrix( as.integer( c( 1 , 2 , 3 , 4 , 5 , NA ) ) , nrow = 2 )"]; // Assert Assert.Equal(new int[] { }, sexp1.AsInts); Assert.Equal(new[] { 1, 2, 3 }, sexp2.AsInts); Assert.Equal(new[] { 1, 2, 3, 4, 5, SexpArrayInt.Na }, sexp3.AsInts); } }
async Task <SexpList> AddInputData(Parameter[] Parameters, IAsyncStreamReader <global::Qlik.Sse.BundledRows> requestStream) { var Params = GetParams(Parameters); await ConvertToColumnar(Params, requestStream); var data = new List <KeyValuePair <string, object> >(); for (int i = 0; i < Params.Length; i++) { var s = GenerateData(Params[i]); data.Add(new KeyValuePair <string, object>(Parameters[i].Name, s)); } return(Sexp.MakeDataFrame(data)); }
/// <summary> /// Executes the program /// </summary> /// <param name="args">The arguments.</param> public static void Main(string[] args) { using (var s = new RConnection(new System.Net.IPAddress(new byte[] { 192, 168, 37, 10 }), port: 6311, user: "******", password: "******")) { // Generate some example data var x = Enumerable.Range(1, 20).ToArray(); var y = (from a in x select(0.5 * a * a) + 2).ToArray(); // Build an R data frame var d = Sexp.MakeDataFrame(); d["x"] = Sexp.Make(x); d["y"] = Sexp.Make(y); s["d"] = d; // Run a linear regression, obtain the summary, and print the result var linearModelSummary = s["summary(lm(y ~ x, d))"]; Console.WriteLine(linearModelSummary.Count); var coefs = linearModelSummary["coefficients"]; var rSquared = ( double )linearModelSummary["r.squared"]; Console.WriteLine("y = {0} x + {1}. R^2 = {2,4:F}%", coefs[1, 0], coefs[0, 0], rSquared * 100); // Make a chart and transfer it to the local machine s.VoidEval("library(ggplot2)"); s.VoidEval("pdf(\"outfile.pdf\")"); s.VoidEval("print(qplot(x,y, data=d))"); s.VoidEval("dev.off()"); using (var f = File.Create("Data Plot.pdf")) { s.ReadFile("outfile.pdf").CopyTo(f); } s.RemoveFile("outfile.pdf"); // Now let's do some linear algebra var matA = new double[, ] { { 14, 9, 3 }, { 2, 11, 15 }, { 0, 12, 17 }, { 5, 2, 3 } }; var matB = new double[, ] { { 12, 25 }, { 9, 10 }, { 8, 5 } }; s["a"] = Sexp.Make(matA); s["b"] = Sexp.Make(matB); Console.WriteLine(s["a %*% b"].ToString()); } Console.WriteLine("Done"); }
public void AsBools_WriteSexpArrayBoolToRAndReadBack_ReturnsArrayOfNullableBools() { using (var service = new Rservice()) { // Arrange Sexp bool1 = new SexpArrayBool(); Sexp bool2 = new SexpArrayBool(true); Sexp bool3 = new SexpArrayBool(false); Sexp bool4 = new SexpArrayBool(( bool? )null); Sexp bool5 = new SexpArrayBool(new bool?[] { true }); Sexp bool6 = new SexpArrayBool(new bool?[] { false }); Sexp bool7 = new SexpArrayBool(new bool?[] { null }); Sexp bool8 = new SexpArrayBool(new bool?[] { true, null, false }); Sexp bool9 = new SexpArrayBool(new bool?[] { true, false, null, true, false, null }); bool9.Attributes.Add("dim", Sexp.Make(new[] { 2, 3 })); // Act service.RConnection["bool1"] = bool1; service.RConnection["bool2"] = bool2; service.RConnection["bool3"] = bool3; service.RConnection["bool4"] = bool4; service.RConnection["bool5"] = bool5; service.RConnection["bool6"] = bool6; service.RConnection["bool7"] = bool7; service.RConnection["bool8"] = bool8; service.RConnection["bool9"] = bool9; bool1 = service.RConnection["bool1"]; bool2 = service.RConnection["bool2"]; bool3 = service.RConnection["bool3"]; bool4 = service.RConnection["bool4"]; bool5 = service.RConnection["bool5"]; bool6 = service.RConnection["bool6"]; bool7 = service.RConnection["bool7"]; bool8 = service.RConnection["bool8"]; bool9 = service.RConnection["bool9"]; // Assert Assert.Equal(new bool?[] { }, bool1.AsBools); Assert.Equal(new bool?[] { true }, bool2.AsBools); Assert.Equal(new bool?[] { false }, bool3.AsBools); Assert.Equal(new bool?[] { null }, bool4.AsBools); Assert.Equal(new bool?[] { true }, bool5.AsBools); Assert.Equal(new bool?[] { false }, bool6.AsBools); Assert.Equal(new bool?[] { null }, bool7.AsBools); Assert.Equal(new bool?[] { true, null, false }, bool8.AsBools); Assert.Equal(new bool?[] { true, false, null, true, false, null }, bool9.AsBools); } }
public override Sexp fun(List arg, int argnum) { Sexp arg1 = arg.car; Sexp args = arg.cdr; Sexp arg2 = ((List)args).car; Sexp arg3 = ((List)((List)args).cdr).car; if (eval.eval(arg1) != Nil.NIL) { return(this.eval.eval(arg2)); } else { return(this.eval.eval(arg3)); } }
/// <summary> /// Adds an item to the ICollection /// </summary> /// <param name="item">The object to add to the ICollection.</param> public override void Add( Sexp item ) { Add( item.AsDates.Select( DateToRInt ) ); }
/// <summary> /// Determines whether the ICollection contains a specific value. /// </summary> public override bool Contains( Sexp item ) { return Value.Contains( item.AsDouble ); }
/// <summary> /// Determines whether the ICollection contains a specific value. /// </summary> /// <param name="item">The object to locate in the ICollection.</param> /// <returns> /// true if item is found in the ICollection; otherwise, false. /// </returns> public override bool Contains( Sexp item ) { return Value.Contains( item.AsString ); }
/// <summary> /// Determines whether the ICollection contains a specific value. /// </summary> /// <param name="item">The object to locate in the ICollection.</param> /// <returns> /// true if item is found in the ICollection; otherwise, false. /// </returns> public override bool Contains( Sexp item ) { return base.Contains( new SexpArrayInt( DateToRInt( item.AsDate ) ) ); }
/// <summary> /// Inserts an item to the IList at the specified index. /// </summary> /// <param name="index">The zero-based index at which item should be inserted.</param> /// <param name="item">The object to insert into the IList.</param> public override void Insert( int index , Sexp item ) { Value.Insert( index , item.AsDouble ); }
/// <summary> /// Determines the index of a specific item in the IList. /// </summary> /// <param name="item">The object to locate in the IList.</param> /// <returns> /// The index of item if found in the list; otherwise, -1. /// </returns> public override int IndexOf( Sexp item ) { return Value.IndexOf( item.IsNa ? NaValue : item.AsInt ); }
/// <summary> /// Determines whether the ICollection contains a specific value. /// </summary> /// <param name="item">The object to locate in the ICollection.</param> /// <returns> /// true if item is found in the ICollection; otherwise, false. /// </returns> public override bool Contains( Sexp item ) { return false; }
/// <summary> /// Inserts an item to the IList at the specified index. /// </summary> /// <param name="index">The zero-based index at which item should be inserted.</param> /// <param name="item">The object to insert into the IList.</param> public override void Insert( int index , Sexp item ) { Value.Insert( index , item.AsString ); }
/// <summary> /// Encode a Sexp in Qap1 format /// </summary> /// <param name="s">The Sexp to be encoded</param> /// <returns>QAP4-encoded bit stream</returns> private static IEnumerable<byte> EncodeSexp( Sexp s ) { var t = s.GetType(); var res = new List<byte>(); byte xt; SexpTaggedList attrs = null; if ( s.Attributes.Count > 0 ) { attrs = new SexpTaggedList(); foreach ( var a in s.Attributes ) { attrs.Add( a.Key , a.Value ); } res.AddRange( EncodeSexp( attrs ) ); } if ( t == typeof( SexpNull ) ) { xt = XtNull; } else if ( t == typeof( SexpArrayDouble ) ) { xt = XtArrayDouble; var v = ( ( SexpArrayDouble )s ).Value; foreach ( var t1 in v ) { res.AddRange( BitConverter.GetBytes( t1 ) ); } } else if ( t == typeof( SexpArrayInt ) ) { xt = XtArrayInt; var v = ( ( SexpArrayInt )s ).Value; foreach ( var t1 in v ) { res.AddRange( BitConverter.GetBytes( t1 ) ); } } else if ( t == typeof( SexpArrayDate ) ) { xt = XtArrayInt; var v = ( ( SexpArrayInt )s ).Value; foreach ( var t1 in v ) { res.AddRange( BitConverter.GetBytes( t1 ) ); } } else if ( t == typeof( SexpArrayBool ) ) { xt = XtArrayBool; var v = ( SexpArrayBool )s; res.AddRange( BitConverter.GetBytes( v.Count ) ); // R logical is false if 0, true if 1, and NA if 2 res.AddRange( v.Cast<SexpArrayBool>().Select( x => x.AsByte ) ); // protocol requires us to pad with null while ( res.Count % 4 != 0 ) { res.Add( 0 ); } } else if ( t == typeof( SexpTaggedList ) ) { xt = XtListTag; var v = ( SexpTaggedList )s; foreach ( var a in v.AsSexpDictionary ) { res.AddRange( EncodeSexp( a.Value ) ); res.AddRange( EncodeSexp( new SexpSymname( a.Key ) ) ); } } else if ( t == typeof( SexpList ) ) { xt = XtVector; var v = ( ( SexpList )s ).Value; foreach ( var a in v ) { res.AddRange( EncodeSexp( a ) ); } } else if ( t == typeof( SexpArrayString ) ) { xt = XtArrayString; var v = ( ( SexpArrayString )s ).Value; foreach ( var a in v ) { // Rserve represents NA strings using 0xff (255). if ( a == null ) { res.Add( 255 ); } else { var b = Encoding.UTF8.GetBytes( a ); // If 0xff occurs in the beginning of a string it should be doubled to avoid misrepresentation. if ( ( b.Length > 0 ) && ( b[ 0 ] == 255 ) ) { res.Add( 255 ); } res.AddRange( b ); } res.Add( 0 ); } } else if ( t == typeof( SexpSymname ) ) { xt = XtSymName; var v = ( ( SexpSymname )s ).Value; var b = Encoding.UTF8.GetBytes( v ); res.AddRange( b ); res.Add( 0 ); } else { throw new RserveException( "Cannot encode an unknown Sexp type " + t.GetType().Name ); } if ( attrs != null ) { xt |= XtHasAttr; } // get payload length long len = res.LongCount(); byte[] lenBytes = BitConverter.GetBytes( len ); // populate header (first four bytes) IEnumerable<byte> header = lenBytes.Take( 3 ); // a large dataset is > 16MB, it requires the XtLarge flag and an extra 4 bytes in the header to esablish correct payload size bool isLargeData = len > 0xfffff0; if ( isLargeData ) { xt |= XtLarge; header = lenBytes.Take( 7 ); } // insert header res.InsertRange( 0 , header ); res.Insert( 0 , xt ); return res; }
/// <summary> /// Copies the elements of the ICollection to an Array, starting at a particular Array index. /// </summary> /// <param name="array">The one-dimensional Array that is the destination of the elements copied from ICollection. The Array must have zero-based indexing.</param> /// <param name="arrayIndex">The zero-based index in array at which copying begins</param> public override void CopyTo( Sexp[] array , int arrayIndex ) { throw new NotSupportedException(); }
/// <summary> /// Asynchronously assign an R variable on the server. /// </summary> /// <param name="symbol"> /// Variable name /// </param> /// <param name="val"> /// Sexp to be assigned to the variable /// </param> public Task AssignAsync( string symbol , Sexp val ) => _protocol.CommandAsync( CmdAssignSexp , new object[] { symbol , val } );
/// <summary> /// Assign an R variable on the server. this[symbol] = value is syntactic sugar for this operation. /// </summary> /// <param name="symbol"> /// Variable name /// </param> /// <param name="val"> /// Sexp to be assigned to the variable /// </param> public void Assign( string symbol , Sexp val ) => Async.RunSynchronously( AssignAsync( symbol , val ) );
/// <summary> /// Removes the first occurrence of a specific object from the ICollection. /// </summary> /// <param name="item">The object to remove from the ICollection.</param> /// <returns> /// true if item was successfully removed from the ICollection; otherwise, false. This method also returns false if item is not found in the original ICollection. /// </returns> public override bool Remove( Sexp item ) { return Value.Remove( item.AsDouble ); }
/// <summary> /// Removes the first occurrence of a specific object from the ICollection. /// </summary> /// <param name="item">The object to remove from the ICollection.</param> /// <returns> /// true if item was successfully removed from the ICollection; otherwise, false. This method also returns false if item is not found in the original ICollection. /// </returns> public override bool Remove( Sexp item ) { return Value.Remove( item.AsString ); }
/// <summary> /// Determines the index of a specific item in the IList. /// </summary> /// <param name="item">The object to locate in the IList.</param> /// <returns> /// The index of item if found in the list; otherwise, -1. /// </returns> public override int IndexOf( Sexp item ) { return Value.IndexOf( item.AsString ); }
/// <summary> /// Assign an R variable on the server. this[symbol] = value is syntactic sugar for this operation. /// </summary> /// <param name="symbol"> /// Variable name /// </param> /// <param name="val"> /// Sexp to be assigned to the variable /// </param> public void Assign( string symbol , Sexp val ) { _protocol.Command( CmdAssignSexp , new object[] { symbol , val } ); }
/// <summary> /// Determines whether the ICollection contains a specific value. /// </summary> /// <param name="item">The object to locate in the ICollection.</param> /// <returns> /// true if item is found in the ICollection; otherwise, false. /// </returns> public override bool Contains( Sexp item ) { return Value.Contains( item.IsNa ? NaValue : item.AsInt ); }
/// <summary> /// Removes the first occurrence of a specific object from the ICollection. /// </summary> /// <param name="item">The object to remove from the ICollection.</param> /// <returns> /// true if item was successfully removed from the ICollection; otherwise, false. This method also returns false if item is not found in the original ICollection. /// </returns> public override bool Remove( Sexp item ) { return base.Remove( new SexpArrayInt( DateToRInt( item.AsDate ) ) ); }
/// <summary> /// Determines the index of a specific item in the IList. /// </summary> /// <param name="item">The object to locate in the IList.</param> /// <returns> /// The index of item if found in the list; otherwise, -1. /// </returns> public override int IndexOf( Sexp item ) { return Value.IndexOf( item.AsDouble ); }
/// <summary> /// Adds an item to the ICollection. /// </summary> /// <param name="item">The object to add to the ICollection.</param> public override void Add( Sexp item ) { Value.Add( item.IsNa ? NaValue : item.AsInt ); }
/// <summary> /// Inserts an item to the IList at the specified index. /// </summary> /// <param name="index">The zero-based index at which item should be inserted.</param> /// <param name="item">The object to insert into the IList.</param> public override void Insert( int index , Sexp item ) { base.Insert( index , new SexpArrayInt( DateToRInt( item.AsDate ) ) ); }
/// <summary> /// Copies the elements of the ICollection to an Array, starting at a particular Array index. /// </summary> /// <param name="array">The one-dimensional Array that is the destination of the elements copied from ICollection. The Array must have zero-based indexing.</param> /// <param name="arrayIndex">The zero-based index in array at which copying begins</param> public override void CopyTo( Sexp[] array , int arrayIndex ) { for ( int i = 0 ; i < Value.Count ; i++ ) { array[ arrayIndex + i ] = new SexpArrayInt( Value[ i ] ); } }
/// <summary> /// Determines the index of a specific item in the IList. /// </summary> /// <param name="item">The object to locate in the IList.</param> /// <returns> /// The index of item if found in the list; otherwise, -1. /// </returns> public override int IndexOf( Sexp item ) { return IndexOf( DateToRInt( item.AsDate ) ); }
/// <summary> /// Inserts an item to the IList at the specified index. /// </summary> /// <param name="index">The zero-based index at which item should be inserted.</param> /// <param name="item">The object to insert into the IList.</param> public override void Insert( int index , Sexp item ) { Value.Insert( index , item.IsNa ? NaValue : item.AsInt ); }
/// <summary> /// Removes the first occurrence of a specific object from the ICollection. /// </summary> /// <param name="item">The object to remove from the ICollection.</param> /// <returns> /// true if item was successfully removed from the ICollection; otherwise, false. This method also returns false if item is not found in the original ICollection. /// </returns> public override bool Remove( Sexp item ) { return Value.Remove( item.IsNa ? NaValue : item.AsInt ); }
/// <summary> /// Adds an item to the ICollection. /// </summary> public override void Add( Sexp item ) { Value.AddRange( item.AsDoubles ); }
/// <summary> /// Adds an item to the ICollection. /// </summary> /// <param name="item">The object to add to the ICollection.</param> public override void Add( Sexp item ) { Value.AddRange( item.AsStrings ); }