Error: could not find function "t.test"

Jul 6, 2015 at 7:29 PM
Edited Jul 6, 2015 at 7:31 PM
Hi, I was trying to implement R.NET for the first time, followed every step as instructed, and tested the code below:

        REngine engine = REngine.GetInstance();
        // REngine requires explicit initialization.
        // You can set some parameters.

        // .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.Evaluate("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.Evaluate("t.test(group1, group2)").AsList();
        double p = testResult["p.value"].AsNumeric().First();

        Textbox1.Text = "Group1: [{0}]" + string.Join(", ", group1);
        Textbox2.Text = "Group2: [{0}]" + string.Join(", ", group2);
        Textbox3.Text = "P-value = {0:0.000}"+  p;

        // you should always dispose of the REngine properly.
        // After disposing of the engine, you cannot reinitialize nor reuse it

The system threw me an "Error: could not find function "t.test""

Anyone might know how to resolve this?

Jul 6, 2015 at 8:15 PM
Hmm strange, could you do me a favor and add this to the top?
you also likely need to add:
engine.SetSymbol("group2", group2);
In there as well.
Jul 6, 2015 at 9:00 PM
Thanks for your reply!

I tried your suggestion and here's what it returned
Error: package or namespace load failed for 'stats'
For reference, here's my namespace setup:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RDotNet;

I was using R 3.2.1 and Visual Studio 2012

Thanks so much!
Jul 6, 2015 at 9:14 PM
Hmm.... this to me suggests something is wrong with your R package loading. Before checking how it is loaded by .NET, best to check it works out in R okay.

Try the following, on the computer just open R normally and try the same set of commands:
group1 <- c(30.02, 29.99, 30.11, 29.97, 30.01, 29.99)
group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)
t.test(group1, group2)
Also, I just realized the SetSymbol command I mentioned earlier was actually unneccesary.
Jul 6, 2015 at 9:41 PM
Hi, thanks for the prompt reply!

I've tried the test in R and it showed no problem.

Could it be problems related to PATH and R_HOME?

I also tried omitting the t.test and directly displaying both group1 and group2 in textboxes, and it seems to be working fine, so it shouldn't be a connection problem I assume?
Jul 6, 2015 at 10:36 PM
Can you try posting the output from this command when executed directly in R and from .NET?

Jul 7, 2015 at 12:14 AM
Both were displaying
"C:/Program Files/R/R-3.2.1/library"

Jul 7, 2015 at 12:57 AM
Hi, evolvedmicrobe and everyone,

I think I've solved the problem (at least for now). I copied everything under "C:\Program Files\R\R-3.2.1\bin\i386" to "C:\Program Files\R\R-3.2.1\library\stats\libs\i386". Right now I can retrieve the p value from the t.test and I'll test it further. If there are no updates from me then everything is good!

Thanks for being so patient, evolvedmicrobe!

Hopefully this will help others, I do see a couple people have the same problem but remained unanswered