使用R.Net将读取文件命令从C#传递给R时出错

时间:2013-09-09 13:18:35

标签: c# r

我使用R.Net将R集成到C#程序中。我有这段代码:

private void drawChart(REngine engine)
    {
        StringBuilder plotCommmand = new StringBuilder();
        plotCommmand.Append("sigmod = read.table(file=\"inproceedings.csv\", header=TRUE, sep=\"~\", quote=\"\", comment=\"\");");            
        Console.WriteLine(plotCommmand.ToString());
        engine.EagerEvaluate(plotCommmand.ToString());            
    }

运行程序时,我收到异常“应用程序中的错误”。在这一行:

engine.EagerEvaluate(plotCommmand.ToString());

请告诉我我的代码有什么错误?

2 个答案:

答案 0 :(得分:0)

您正在使用EagerEvaluate,这是一种过时的方法,建议您使用旧版本的R.NET。以下代码适用于我,使用最新的代码库,我希望它可以通过NuGet或https://rdotnet.codeplex.com/releases

获得最新版本。
StringBuilder plotCommmand = new StringBuilder();
plotCommmand.Append("sigmod = read.table(file='c:/tmp/test.csv', header=TRUE, sep='~', quote='', comment='')");
Console.WriteLine(plotCommmand.ToString());
engine.Evaluate(plotCommmand.ToString());
engine.Evaluate("str(sigmod)");

答案 1 :(得分:0)

我通过替换此代码解决了我的问题:

plotCommmand.Append("sigmod = read.table(file=\"inproceedings.csv\", header=TRUE, sep=\"~\", quote=\"\", comment=\"\");");

由:

plotCommmand.Append(@"sigmod <- read.table(file='C:\\Users\\admin\\Documents\\R\\inproceedings.csv', header=TRUE, sep='~', quote='', comment.char='');");