当前位置: 首页>编程语言>正文

r语言LCMM R语言setwd

R语言编译器中,设置当前工作文件夹可以用setwd()函数。

> setwd("e://桌面//")
> setwd("e:\桌面\")
> setwd("e:/桌面/")

这三种结构都是可以编译通过的,

但是在VS C#中却不行,只有一种能运行成功。

(PS:R语言在VS中运行要先配置环境,还没配置的童鞋先要配置好,才可运行,如有问题可看我前面的随笔。)

就是这种结构,engine.Evaluate("setwd('e:/桌面/')");

我调试了很多次,确实只有这样写才能设置它的工作文件夹,但是必须保证文件夹存在。

下面贴上完整代码,我是在winform中调试的,然后用PictuerBox显示图片。

//配置R语言环境
        private void LoadRPath(string RVersion = "R-3.4.1")//默认R-3.4.1
        {

            string dlldir = @"C:\Program Files\R\" + RVersion + @"\bin\x64";//默认64位

            bool r_located = false;

            var rPath = System.Environment.Is64BitProcess ?

                string.Format(@"C:\Program Files\R\" + RVersion + @"\bin\x64") :

                string.Format(@"C:\Program Files\R\" + RVersion + @"\bin\i386");

            dlldir = rPath;

            while (r_located == false)
            {

                try
                {

                    REngine.SetEnvironmentVariables(dlldir);

                    r_located = true;

                }

                catch
                {

                    if (System.Environment.Is64BitProcess)
                    {

                        MessageBox.Show(@"找不到R运行环境:\R\" + RVersion + @"\bin\x64 " + " \n请手动添加文件夹目录");

                    }

                    else
                    {

                        MessageBox.Show(@"找不到R运行环境:\R\" + RVersion + @"\bin\i386" + " \n请手动添加文件夹目录");

                    }

                    FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {

                        dlldir = @folderBrowserDialog1.SelectedPath;

                    }

                }

            }

        }

        private void CalculateHist()
        {
            //避免产生相同名称文件
            string rnd = System.Guid.NewGuid().ToString().Replace("-", "");
            string fileName = "i" + rnd + "_Hist.png";
            //拿到程序运行目录
            string sysPath = Application.StartupPath;
            string dir = sysPath + "\RImage\Hist\";
            string fullDir = dir + fileName;
            //创建文件夹
            Directory.CreateDirectory(Path.GetDirectoryName(dir));
            //替换
            dir = dir.Replace("\", "/");
            //设置工作文件夹
            engine.Evaluate("setwd('" + dir + "')");
            engine.Evaluate(string.Format("png(file='{0}',bg ='transparent',width={1},height={2})", fileName, this.ptbHist.Width, this.ptbHist.Height));

            //            string Rcode = @"library('scatterplot3d')
            //       z <- seq(-10, 10, 0.01) 
            //       x <- cos(z)
            //       y <- sin(z) 
            //       scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis='blue', col.grid='lightblue',main='3d绘图',pch=20)
            //       ";
            engine.Evaluate(@"x <- (0:12) * pi / 12
                y <- cos(x)
                plot(x,y);
                ");
            //engine.Evaluate(Rcode);
            engine.Evaluate("dev.off()");

            //var x = engine.Evaluate("x <- rnorm(100, mean=50, sd=10)").AsNumeric();

            //engine.Evaluate("hist(x)");
            //var x = engine.Evaluate("x <- 1:100").AsNumeric();

            //var y = engine.Evaluate("y <- 5:105").AsNumeric();

            //engine.Evaluate("model = function (a,    b){23.86+5.525*b-2.5725*a-6.6413*b^2-5.1862*a^2}"); //evaluate function

            //engine.Evaluate("z = outer(x, y ,model)");

            //engine.Evaluate("contour(x,y,z, nlevels = 10)");
            //string path = System.IO.Path.GetFullPath(fileName);
            Bitmap image = new Bitmap(fullDir);
            ptbHist.Image = image;
        }

  欢迎大家交流学习~~~~~~~~

 




https://www.xamrdz.com/lan/5vv1967342.html

相关文章: