I made a simple test project with a form and a button.
Click the button to run the sample code, the first time it works well, but it has a memory access error while trying to SetSymble or Evaluate R commands.
Seems the R environment became unaccessable. Did I make any mistake to initialized REngine?
==============================
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
==============================
private void button1_Click(object sender, EventArgs e)
{
// Set the folder in which R.dll locates.
REngine.SetDllDirectory(@"C:\Program Files\R\R-2.13.0\bin\i386");
using (REngine engine = REngine.CreateInstance("RDotNet", new[] { "-q" })) // quiet mode
{
// .NET Framework array to R vector.
NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
engine.SetSymbol("group1", group1);
// Direct parsing from R script.
NumericVector group2 = engine.EagerEvaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();
// Test difference of mean and get the P-value.
GenericVector testResult = engine.EagerEvaluate("t.test(group1, group2)").AsList();
double p = testResult["p.value"].AsNumeric().First();
// string aaa = "jpeg(filename = \"bad lot path by step2.jpg\",width=480,height=300)";
string aaa = "myfun <- function(a){return (mean(a));};myfun(c(1,1,2,3,4))";
Console.WriteLine(aaa);
NumericVector testResult1 = engine.EagerEvaluate(aaa).AsNumeric();
double a = engine.EagerEvaluate(aaa).AsNumeric().First();
Console.WriteLine("a={0:0.000}", a);
Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
Console.WriteLine("P-value = {0:0.000}", p);
}
}
Comments: By design, cannot initialise twice
Click the button to run the sample code, the first time it works well, but it has a memory access error while trying to SetSymble or Evaluate R commands.
Seems the R environment became unaccessable. Did I make any mistake to initialized REngine?
==============================
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
==============================
private void button1_Click(object sender, EventArgs e)
{
// Set the folder in which R.dll locates.
REngine.SetDllDirectory(@"C:\Program Files\R\R-2.13.0\bin\i386");
using (REngine engine = REngine.CreateInstance("RDotNet", new[] { "-q" })) // quiet mode
{
// .NET Framework array to R vector.
NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
engine.SetSymbol("group1", group1);
// Direct parsing from R script.
NumericVector group2 = engine.EagerEvaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();
// Test difference of mean and get the P-value.
GenericVector testResult = engine.EagerEvaluate("t.test(group1, group2)").AsList();
double p = testResult["p.value"].AsNumeric().First();
// string aaa = "jpeg(filename = \"bad lot path by step2.jpg\",width=480,height=300)";
string aaa = "myfun <- function(a){return (mean(a));};myfun(c(1,1,2,3,4))";
Console.WriteLine(aaa);
NumericVector testResult1 = engine.EagerEvaluate(aaa).AsNumeric();
double a = engine.EagerEvaluate(aaa).AsNumeric().First();
Console.WriteLine("a={0:0.000}", a);
Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
Console.WriteLine("P-value = {0:0.000}", p);
}
}
Comments: By design, cannot initialise twice