Example #1
0
        //private  DataTable ReadCVS(string filepath, string filename)
        //{
        //    //string cvsDir = filepath;//要读取的CVS路径
        //    DataTable dt = new DataTable();
        //    if (filename.Trim().ToUpper().EndsWith("CSV"))//判断所要读取的扩展名
        //    {
        //        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='text;HDR=NO;FMT=Delimited'";//有列的读取
        //        string commandText = "select * from [" + filename + "]";//SQL语句

        //        OleDbConnection olconn = new OleDbConnection(connStr);
        //        olconn.Open();
        //        OleDbDataAdapter odp = new OleDbDataAdapter(commandText, olconn);
        //        odp.Fill(dt);
        //        olconn.Close();
        //        odp.Dispose();
        //        olconn.Dispose();
        //    }
        //    return dt;
        //}

        private void openCSV(string filepach)
        {
            REngine engine = REngine.GetInstanceFromID("RDotNet") ?? REngine.CreateInstance("RDotNet");
            //bool isok = engine.IsRunning;
            string cmd = string.Format("dataset=read.csv('{0}')", filepach).Replace("\\", "\\\\");//不加Replace错误
            //string cmd = @"dataset=read.csv('C:\\Users\\wm\\Desktop\\煤与瓦斯2.csv')";//不加@错误
            string cmd2 = @"levels(dataset$突出规模)";

            engine.Evaluate(cmd);
            RDotNet.CharacterVector levels = engine.Evaluate(cmd2).AsCharacter();
            dataset = engine.Evaluate("dataset").AsDataFrame();

            for (int i = 0; i < dataset.ColumnCount; ++i)
            {
                dataGridView1.ColumnCount++;
                dataGridView1.Columns[i].Name = dataset.ColumnNames[i];
            }

            for (int i = 0; i < dataset.RowCount; ++i)
            {
                dataGridView1.RowCount++;
                dataGridView1.Rows[i].HeaderCell.Value = dataset.RowNames[i];

                for (int k = 0; k < dataset.ColumnCount; ++k)
                {
                    dataGridView1[k, i].Value = dataset[i, k];
                }
            }
        }
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            var engine = REngine.GetInstanceFromID("RDotNet");

            if (engine != null)
            {
                engine.Dispose();
            }
        }
Example #3
0
        public void TestHasSlot()
        {
            var engine = REngine.GetInstanceFromID(EngineName);
            var obj    = engine.Evaluate("new('testclass', foo='s4', bar=1:4)").AsS4();

            Assert.That(obj.HasSlot("foo"), Is.True);
            Assert.That(obj.HasSlot("bar"), Is.True);
            Assert.That(obj.HasSlot("baz"), Is.False);
        }
Example #4
0
        public void DisposeEngine()
        {
            var engine = REngine.GetInstanceFromID(EngineName);

            if (engine != null)
            {
                engine.Dispose();
            }
        }
Example #5
0
        public void TestSetSlot()
        {
            var engine = REngine.GetInstanceFromID(EngineName);
            var obj    = engine.Evaluate("new('testclass', foo='s4', bar=1:4)").AsS4();
            var foo    = obj.GetSlot("foo").AsCharacter().First();

            Assert.That(foo, Is.EqualTo("s4"));
            obj.SetSlot("foo", engine.CreateCharacterVector(new[] { "new value" }));
            foo = obj.GetSlot("foo").AsCharacter().First();
            Assert.That(foo, Is.EqualTo("new value"));
        }
Example #6
0
        public void TestGetSlot()
        {
            var engine = REngine.GetInstanceFromID(EngineName);
            var obj    = engine.Evaluate("new('testclass', foo='s4', bar=1:4)").AsS4();
            var foo    = obj.GetSlot("foo").AsCharacter().First();

            Assert.That(foo, Is.EqualTo("s4"));
            var bar = obj.GetSlot("bar").AsInteger();

            Assert.That(bar, Is.EquivalentTo(new[] { 1, 2, 3, 4 }));
        }
Example #7
0
        public void TestGetSlotTypes()
        {
            var engine = REngine.GetInstanceFromID(EngineName);
            var obj    = engine.Evaluate("new('testclass', foo='s4', bar=1:4)").AsS4();
            var actual = obj.GetSlotTypes();

            Assert.That(actual.Count, Is.EqualTo(2));
            Assert.That(actual.ContainsKey("foo"), Is.True);
            Assert.That(actual["foo"], Is.EqualTo("character"));
            Assert.That(actual.ContainsKey("bar"), Is.True);
            Assert.That(actual["bar"], Is.EqualTo("integer"));
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            REngine engine = REngine.GetInstanceFromID("RDotNet") ?? REngine.CreateInstance("RDotNet");

            engine.Initialize();
            //this.txtOutput.Text = engine.IsRunning.ToString();
            string RFDir = System.IO.Directory.GetCurrentDirectory().ToString() + "\\model\\RF.Model.RData";
            // string cmd = @"load(file='C:\\Users\\wm\\Desktop\\RF.Model.RData',envir = parent.frame())";
            string cmd  = string.Format("load(file='{0}',envir = parent.frame())", RFDir.Replace("\\", "\\\\"));
            string cmd2 = @"library(randomForest)";

            engine.Evaluate(cmd2);
            engine.Evaluate(cmd);
            // MessageBox.Show(System.IO.Directory.GetCurrentDirectory().ToString());
        }
        private void btnSumbit_Click(object sender, EventArgs e)
        {
            // REngine.SetDllDirectory(@"C:\Program Files\R\R-2.15.0\bin\i386");
            if (!string.IsNullOrEmpty(this.txtPram1.Text) && !string.IsNullOrEmpty(this.txtPram2.Text) && !string.IsNullOrEmpty(this.txtPram3.Text) && !string.IsNullOrEmpty(this.txtPram4.Text) && !string.IsNullOrEmpty(this.txtPram5.Text))
            {
                REngine engine = REngine.GetInstanceFromID("RDotNet") ?? REngine.CreateInstance("RDotNet");
                //engine.Initialize();//只能初始化一次

                string strCreateTest = string.Format("test2=data.frame(X1={0},X2={1},X3={2},X4={3},X5={4},Y=0)", double.Parse(this.txtPram1.Text), double.Parse(this.txtPram2.Text), double.Parse(this.txtPram3.Text), double.Parse(this.txtPram4.Text), double.Parse(this.txtPram5.Text));
                string cmd8          = @"predict(RF.Model,test2[,-6],type='response')";

                engine.Evaluate(strCreateTest);

                IntegerVector index = engine.Evaluate(cmd8).AsInteger();


                this.txtOutput.Text = getResult(index[0]);
            }
            else
            {
                MessageBox.Show("参数不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Example #10
0
        public void TearDown()
        {
            var engine = REngine.GetInstanceFromID(EngineName);

            engine.Evaluate("rm(list=ls())");
        }