R语⾔学习笔记——C#中如何使⽤R语⾔twd()函数在R语⾔编译器中,设置当前⼯作⽂件夹可以⽤twd()函数。
> twd("e://桌⾯//")
> twd("e:\桌⾯\")
> twd("e:/桌⾯/")
这三种结构都是可以编译通过的,
但是在VS C#中却不⾏,只有⼀种能运⾏成功。
(PS:R语⾔在VS中运⾏要先配置环境,还没配置的童鞋先要配置好,才可运⾏,如有问题可看我前⾯的随笔。)
就是这种结构,engine.Evaluate("twd('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 = fal;检车怎么检
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 == fal)
{
try
{
电脑开机密码怎么取消REngine.SetEnvironmentVariables(dlldir);
r_located = true;
}
catch
{
if (System.Environment.Is64BitProcess)
{
灵枢经脉MessageBox.Show(@"找不到R运⾏环境:\R\" + RVersion + @"\bin\x64 " + " \n请⼿动添加⽂件夹⽬录");
}
狗有哪些品种el
{
MessageBox.Show(@"找不到R运⾏环境:\R\" + RVersion + @"\bin\i386" + " \n请⼿动添加⽂件夹⽬录");
}
FolderBrowrDialog folderBrowrDialog1 = new FolderBrowrDialog();
if (folderBrowrDialog1.ShowDialog() == DialogResult.OK)
{
dlldir = @folderBrowrDialog1.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("twd('" + 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 <- q(-10, 10, 0.01)
// x <- cos(z)
// y <- sin(z)
// scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis='blue', id='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;
}
欢迎⼤家交流学习~~~~~~~~