Пример #1
0
        public void TestMultipleAppDomains()
        {
            var e = REngine.GetInstance(); // need to trigger the R main loop setup once, and it may as well be in the default appdomain

            TestAppDomain("test1");        // works
            TestAppDomain("test2");        // hangs at the last line in Job.Execute()
        }
Пример #2
0
        // Marking this test as ignore, as it is incompatible with trying to get all unit tests
        // run from NUnit to pass successfully.
        // Keeping the test code as a basis for potential further feasibility investigations
        public void TestCreateEngineTwice()
        {
            // Investigate:
            // https://rdotnet.codeplex.com/workitem/54
            var engine = REngine.GetInstance();

            engine.Initialize();
            var paths = engine.Evaluate(".libPaths()").AsCharacter().ToArray();

            Console.WriteLine(engine.Evaluate("Sys.getenv('R_HOME')").AsCharacter().ToArray()[0]);
            // engine.Evaluate("library(rjson)");
            engine.Dispose();
            Console.WriteLine("Before second creation");
            engine = REngine.GetInstance();
            Console.WriteLine("Before second initialize");
            engine.Initialize();
            Console.WriteLine(engine.Evaluate("Sys.getenv('R_HOME')").AsCharacter().ToArray()[0]);
            paths = engine.Evaluate(".libPaths()").AsCharacter().ToArray();
            try
            {
                engine.Evaluate("library(methods)");
            }
            catch
            {
                engine.Evaluate("traceback()");
                throw;
            }
            finally
            {
                engine.Dispose();
            }
            Assert.That(engine.IsRunning, Is.False);
        }
Пример #3
0
        public void TestGetPathInitSearchLog()
        {
            REngine.GetInstance();
            var log = NativeUtility.SetEnvironmentVariablesLog;

            Assert.AreNotEqual(string.Empty, log);
        }
Пример #4
0
            // initializes REngine
            private REngine InitREngine()
            {
                var engine = REngine.GetInstance(initialize: false);

                engine.Initialize(null, new NullCharacterDevice(), setupMainLoop: false);  // real char device?
                //AppDomain.CurrentDomain.DomainUnload += (EventHandler)((o, e) => engine.Dispose());
                return(engine);
            }
Пример #5
0
 protected virtual void SetUpFixture()
 {
     if (initializeOnceOnly && engine != null)
     {
         return;
     }
     REngine.SetEnvironmentVariables();
     engine = REngine.GetInstance(dll: null, initialize: true, parameter: null, device: Device);
 }
Пример #6
0
        [Test, Ignore] // cannot test this easily with new API. Rethink
        public void TestIsRunning()
        {
            var engine = REngine.GetInstance();

            Assert.That(engine, Is.Not.Null);
            Assert.That(engine.IsRunning, Is.False);
            engine.Initialize();
            Assert.That(engine.IsRunning, Is.True);
            engine.Dispose();
            Assert.That(engine.IsRunning, Is.False);
        }
Пример #7
0
        [Fact(Skip = "cannot test this easily with new API. Rethink")] // cannot test this easily with new API. Rethink
        public void TestIsRunning()
        {
            var engine = REngine.GetInstance();

            Assert.NotNull(engine);
            Assert.False(engine.IsRunning);
            engine.Initialize();
            Assert.True(engine.IsRunning);
            engine.Dispose();
            Assert.False(engine.IsRunning);
        }
Пример #8
0
        [Fact(Skip = "Cannot run this in a batch with the new singleton pattern")] // Cannot run this in a batch with the new singleton pattern.
        public void TestInitParams()
        {
            MockDevice device = new MockDevice();

            REngine.SetEnvironmentVariables();
            using (var engine = REngine.GetInstance())
            {
                ulong            maxMemSize = 128 * 1024 * 1024;
                StartupParameter parameter  = new StartupParameter()
                {
                    MaxMemorySize = maxMemSize,
                };
                engine.Initialize(parameter: parameter, device: device);
                Assert.Equal(engine.Evaluate("memory.limit()").AsNumeric()[0], 128.0);
            }
        }
Пример #9
0
        [Test, Ignore] // TODO
        public void TestSeveralAppDomains()
        {
            var engine = REngine.GetInstance();

            engine.Initialize();

            // create another AppDomain for loading the plug-ins
            AppDomainSetup setup = new AppDomainSetup();

            setup.ApplicationBase = Path.GetDirectoryName(typeof(REngine).Assembly.Location);

            setup.DisallowApplicationBaseProbing = false;
            setup.DisallowBindingRedirects       = false;

            var domain = AppDomain.CreateDomain("Plugin AppDomain", null, setup);

            domain.Load(typeof(REngine).Assembly.EscapedCodeBase);
        }
Пример #10
0
        public void TestUsingDefaultRPackages()
        {
            // This test was designed to look at a symptom observed alongside the issue https://github.com/rdotnet/rdotnet/issues/127
            var engine = REngine.GetInstance();
            var se     = engine.Evaluate("set.seed");

            Assert.True(engine.Evaluate("Sys.which('R.dll')").AsCharacter()[0].Length > 0);
            Assert.True(engine.Evaluate("Sys.which('RBLAS.dll')").AsCharacter()[0].Length > 0);

            string[] expected   = { "base", "methods", "utils", "grDevices", "graphics", "stats" };
            var      loadedDlls = engine.Evaluate("getLoadedDLLs()").AsList();

            string[] dllnames = loadedDlls.Select(x => x.AsCharacter().ToArray()[0]).ToArray();

            Assert.Equal(expected, dllnames);

            se = engine.Evaluate("set.seed(0)");
            se = engine.Evaluate("blah <- rnorm(4)");
        }
Пример #11
0
        static void Main(string[] args)
        {
            SetupPath(); // current process, soon to be deprecated

            // There are several options to initialize the engine, but by default the following suffice:
            REngine engine = REngine.GetInstance();

            engine.Initialize(); // required since v1.5

            // some random weight samples
            double[] weight = new double[] { 3.2, 3.6, 3.2, 1.7, 0.8, 2.9, 2, 1.4, 1.2, 2.1, 2.5, 3.9, 3.7, 2.4, 1.5, 0.9, 2.5, 1.7, 2.8, 2.1, 1.2 };
            double[] lenght = new double[] { 2, 3, 3.2, 4.7, 5.8, 3.9, 2, 8.4, 5.2, 4.1, 2.5, 3.9, 5, 2.4, 3.5, 0.9, 2.5, 2.7, 2.8, 2.1, 1.2 };

            // introduce the samples into R
            engine.SetSymbol("weight", engine.CreateNumericVector(weight));
            engine.SetSymbol("lenght", engine.CreateNumericVector(lenght));

            // set the weights and lenghts as a data frame (regular R syntax in string)
            engine.Evaluate("df <- data.frame(id=c(1:length(weight)), weight = weight,lenght = lenght )");


            // evaluate and retrieve mean
            double avg = engine.Evaluate("mean(df$weight)").AsNumeric().ToArray()[0];
            // same for standard deviation
            double std = engine.Evaluate("sd(df$weight)").AsNumeric().ToArray()[0];

            // NumericVector coeff = engine.Evaluate("coefficients(lm(df$weight ~ df$lenght ))").AsNumeric();
            // print output in console
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-gb");

            //Show in console the weight and lenght data
            Console.WriteLine(string.Format("Weights: ({0})", string.Join(",",
                                                                          weight.Select(f => f.ToString(ci)) // LINQ expression
                                                                          )));
            Console.WriteLine(string.Format("Length: ({0})", string.Join(",",
                                                                         lenght.Select(f => f.ToString(ci)) // LINQ expression
                                                                         )));
            Console.WriteLine(string.Format("Sample size: {0}", weight.Length));
            Console.WriteLine(string.Format(ci, "Average: {0:0.00}", avg));
            Console.WriteLine(string.Format(ci, "Standard deviation: {0:0.00}", std));

            var result = engine.Evaluate("lm(df$weight ~ df$lenght)");

            engine.SetSymbol("result", result);
            var    coefficients = result.AsList()["coefficients"].AsNumeric().ToList();
            double r2           = engine.Evaluate("summary(result)").AsList()["r.squared"].AsNumeric().ToList()[0];
            double intercept    = coefficients[0];
            double slope        = coefficients[1];

            Console.WriteLine("Intercept:" + intercept.ToString());
            Console.WriteLine("slope:" + slope);
            Console.WriteLine("r2:" + r2);

            string fileName = "myplot.png";

            CharacterVector fileNameVector = engine.CreateCharacterVector(new[] { fileName });

            engine.SetSymbol("fileName", fileNameVector);

            engine.Evaluate("png(filename=fileName, width=6, height=6, units='in', res=100)");
            engine.Evaluate("reg <- lm(df$weight ~ df$lenght)");
            engine.Evaluate("plot(df$weight ~ df$lenght)");
            engine.Evaluate("abline(reg)");
            engine.Evaluate("dev.off()");
            //The file will save in debug directory

            Application.Run(new Form1());
            // After disposing of the engine, you cannot reinitialize nor reuse it
            engine.Dispose();
        }