Example #1
1
        public Form1()
        {
            InitializeComponent();
            this.FormClosed += new FormClosedEventHandler(OnFormClosed);

            REngine.SetEnvironmentVariables();
            engine = REngine.GetInstance();
            // REngine requires explicit initialization.
            // You can set some parameters.
            engine.Initialize();
        }
Example #2
0
        public void Start(params REngineOptions[] list)
        {
            try
            {
                // Only one instance per process
                if (!IsRunning)
                {
                    SetupEnvironment();

                    InstanceID = "RDotNet";
                    //
                    engine = RDotNet.REngine.CreateInstance(InstanceID);
                    engine.Initialize();

                    // Load compiler package on startup - may speed up code if long running
                    LoadPackage("compiler");
                }
                else
                {
                    engine = RDotNet.REngine.GetInstanceFromID(InstanceID);
                }
            }
            catch (SEHException e)
            {
                throw new  ApplicationException(e.Message, e);
            }
            if (engine == null)
            {
                throw new ApplicationException("Could not start R");
            }
        }
Example #3
0
 public frmData()
 {
     InitializeComponent();
     // There are several options to initialize thengine, but by default the following suffice:
     engine = REngine.GetInstance();
     engine.Initialize();
 }
        public CodeController()
        {
            if (_engine != null) return;

            _engine = REngine.GetInstance(null, true, null, CharacterDevice);
            _engine.Initialize();
            _engine. .Install(GraphicsDevice);
        }
Example #5
0
 internal static void InitializeRDotNet()
 {
     try
     {
         REngine.SetEnvironmentVariables();
         _engine = REngine.GetInstance();
         _engine.Initialize();
     }
     catch (Exception ex)
     {
         LogDisplay.WriteLine("Error initializing RDotNet: " + ex.Message);
     }
 }
Example #6
0
 public frmData(int id)
 {
     InitializeComponent();
     // There are several options to initialize thengine, but by default the following suffice:
     engine = REngine.GetInstance();
     engine.Initialize();
     intID = id;
     setFilters(id);
     setGrid(id);
     setButtons(bAdd, bEdit);
     cTitle = Color.Black;
     cAxis = Color.Black;
     cLegend = Color.Black;
     cTick = Color.Black;
 }
Example #7
0
 /// <summary>
 /// Gets a reference to the R engine, creating and initializing it if necessary. In most cases users need not provide any parameter to this method.
 /// </summary>
 /// <param name="dll">The file name of the library to load, e.g. "R.dll" for Windows. You usually do not need need to provide this optional parameter</param>
 /// <param name="initialize">Initialize the R engine after its creation. Default is true</param>
 /// <param name="parameter">If 'initialize' is 'true', you can optionally specify the specific startup parameters for the R native engine</param>
 /// <param name="device">If 'initialize' is 'true', you can optionally specify a character device for the R engine to use</param>
 /// <returns>The engine.</returns>
 /// <example>
 /// <p>A minimalist approach is to just call GetInstance</p>
 /// <code>
 /// REngine.SetEnvironmentVariables();
 /// var engine = REngine.GetInstance();
 /// engine.Evaluate("letters[1:26]");
 /// </code>
 /// <p>In unusual circumstances you may need to elaborate on the initialization in a separate method call</p>
 /// <code>
 /// REngine.SetEnvironmentVariables(rPath=@"c:\my\peculiar\path\to\R\bin\x64");
 /// var engine = REngine.GetInstance(initialize=false);
 /// StartupParameter sParams=new StartupParameter(){NoRenviron=true;};
 /// ICharacterDevice device = new YourCustomDevice();
 /// engine.Initialize(parameter: sParams, device: device);
 /// engine.Evaluate("letters[1:26]");
 /// </code>
 /// </example>
 public static REngine GetInstance(string dll = null, bool initialize = true, StartupParameter parameter = null, ICharacterDevice device = null)
 {
     if (!environmentIsSet) // should there be a warning? and how?
         SetEnvironmentVariables();
     if (engine == null)
     {
         engine = CreateInstance(EngineName, dll);
         if (initialize)
             engine.Initialize(parameter, device);
     }
     if (engine.Disposed)
         throw new InvalidOperationException("The single REngine instance has already been disposed of (i.e. shut down). Multiple engine restart is not possible.");
     return engine;
 }
 private void InitializeRDotNet()
 {
     REngine.SetEnvironmentVariables();
     engine = REngine.GetInstance();
     // REngine requires explicit initialization.
     // You can set some parameters.
     engine.Initialize();
     //load BSky and R packages
     engine.Evaluate("library(uadatapackage)");
     engine.Evaluate("library(foreign)");
     engine.Evaluate("library(data.table)");
     engine.Evaluate("library(RODBC)");
     engine.Evaluate("library(car)");
     engine.Evaluate("library(aplpack)");
     engine.Evaluate("library(mgcv)");
     engine.Evaluate("library(rgl)");
     engine.Evaluate("library(gmodels)");
 }
Example #9
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();
        }
Example #10
0
        private static void InitiateR()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            form1 = new Form1();
            string rhome = System.Environment.GetEnvironmentVariable("R_HOME");
            if (string.IsNullOrEmpty(rhome))
                rhome = @"C:\Program Files\R\R-3.1.1";

            System.Environment.SetEnvironmentVariable("R_HOME", rhome);
            System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("PATH") + ";" + rhome + @"\bin\i386");

            REngine.SetEnvironmentVariables();
            // There are several options to initialize the engine, but by default the following suffice:
            engine = REngine.GetInstance();
            engine.Initialize();
        }
Example #11
0
 private static void MakeEngine()
 {
     if (_engine == null)
     {
         REngine.SetEnvironmentVariables();
         _engine = REngine.GetInstance();
         _engine.Initialize();
     }
 }
Example #12
0
 public TextMiningApi()
 {
     REngine.SetEnvironmentVariables();
     _engine = REngine.GetInstance();
     _engine.Initialize();
 }
        private void InitializeR()
        {
            engine = REngine.GetInstance();
            engine.Initialize();
            var currentDir = System.Environment.CurrentDirectory.Replace('\\', '/');
            if (!m_JADE_Loaded)
            {
               
                engine.Evaluate("install.packages('JADE', repos='http://cran.us.r-project.org')");

                //repos ='https://cran.rstudio.com/bin/windows/contrib/3.2/JADE_1.9-93.zip')");

                engine.Evaluate("library('JADE')");
                m_JADE_Loaded = true;
            }
            
        }