Ejemplo n.º 1
0
        public void TestGenericFunction()
        {
            SetUpTest();
            var engine = this.Engine;

            var funcDef = @"
setGeneric( 'f', function(x, ...) {
	standardGeneric('f')
} )

setMethod( 'f', 'integer', function(x, ...) { paste( 'f.integer called:', printPairList(...) ) } )
setMethod( 'f', 'numeric', function(x, ...) { paste( 'f.numeric called:', printPairList(...) ) } )
";

            engine.Evaluate(defPrintPairlist);
            engine.Evaluate(funcDef);
            var f = engine.GetSymbol("f").AsFunction();

            // > f(1, b=2, c=3)
            // [1] "f.numeric called:  b=2; c=3"
            checkInvoke(f.InvokeNamed(tc("x", 1.0), tc("b", "2"), tc("c", "3")), "f.numeric called:  b=2; c=3");
            // > f(1, b=2.1, c=3)
            // [1] "f.numeric called:  b=2.1; c=3"
            checkInvoke(f.InvokeNamed(tc("x", 1.0), tc("b", "2.1"), tc("c", "3")), "f.numeric called:  b=2.1; c=3");
            // > f(1, c=3, b=2)
            // [1] "f.numeric called:  c=3; b=2"
            checkInvoke(f.InvokeNamed(tc("x", 1.0), tc("c", "3"), tc("b", "2")), "f.numeric called:  c=3; b=2");
            // > f(1L, b=2, c=3)
            // [1] "f.integer called:  b=2; c=3"
            checkInvoke(f.InvokeNamed(tc("x", 1), tc("b", "2"), tc("c", "3")), "f.integer called:  b=2; c=3");
            // > f(1L, c=3, b=2)
            // [1] "f.integer called:  c=3; b=2"
            checkInvoke(f.InvokeNamed(tc("x", 1), tc("c", "3"), tc("b", "2")), "f.integer called:  c=3; b=2");

            // .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();

            GenericVector testResult = engine.Evaluate("t.test(group1, group2)").AsList();
            double        p          = testResult["p.value"].AsNumeric().First();

            Assert.Equal(0.09077332, Math.Round(p, 8));

            var           studentTest = engine.Evaluate("t.test").AsFunction();
            GenericVector testResult2 = studentTest.Invoke(new[] { group1, group2 }).AsList();
            double        p2          = testResult2["p.value"].AsNumeric().First();
            double        p3          = testResult2[2].AsNumeric().First();

            Assert.Equal(0.09077332, Math.Round(p2, 8));

            var sexp = studentTest.Invoke(engine.Evaluate("1:10"), engine.Evaluate("7:20"));

            // > format((t.test(1:10, y = c(7:20)) )$p.value, digits=12)
            // [1] "1.85528183251e-05"
            Assert.True(Math.Abs(1.85528183251e-05 - sexp.AsList()["p.value"].AsNumeric()[0]) < 1e-12);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the function. Match the function arguments by name.
        /// </summary>
        /// <param name="argNames">The names of the arguments. These can be empty strings for unnamed function arguments</param>
        /// <param name="args">The arguments passed to the function</param>
        /// <returns></returns>
        protected SymbolicExpression InvokeViaPairlist(string[] argNames, SymbolicExpression[] args)
        {
            var names     = new CharacterVector(Engine, argNames);
            var arguments = new GenericVector(Engine, args);

            arguments.SetNames(names);
            var argPairList = arguments.ToPairlist();

            //IntPtr newEnvironment = Engine.GetFunction<Rf_allocSExp>()(SymbolicExpressionType.Environment);
            //IntPtr result = Engine.GetFunction<Rf_applyClosure>()(Body.DangerousGetHandle(), handle,
            //                                                      argPairList.DangerousGetHandle(),
            //                                                      Environment.DangerousGetHandle(), newEnvironment);
            return(createCallAndEvaluate(argPairList.DangerousGetHandle()));
        }
Ejemplo n.º 3
0
        public void TestListSetNames()
        {
            //  http://stackoverflow.com/questions/33326594/how-can-i-create-named-list-members-object-in-r-net
            var engine = this.Engine;
            var list   = new GenericVector(engine, 2);

            // odpar <- list(mean = c(-1.5, 0, 1.5), var = c(0.5, 0.6, 0.8))
            list[0] = engine.CreateNumericVector(new[] { -1.5, 0, 1.5 });
            list[1] = engine.CreateNumericVector(new[] { 0.5, 0.6, 0.8 });
            list.SetNames("mean", "var");
            engine.SetSymbol("TestListSetNames", list);
            var listNames = engine.Evaluate("names(TestListSetNames)").AsCharacter().ToArray();

            Assert.AreEqual("mean", listNames[0]);
            Assert.AreEqual("var", listNames[1]);
        }