This project is read-only.

Making a dll using RDotnet.

Mar 19, 2014 at 7:10 AM
Edited Mar 19, 2014 at 7:12 AM
Hi , I have this file like ,




using RDotNet;
using RDotNet.NativeLibrary;
using Stats;


namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {


        string rhome = System.Environment.GetEnvironmentVariable("R_HOME");
        if (string.IsNullOrEmpty(rhome))
            rhome = @"E:\R-3.0.2";

        System.Environment.SetEnvironmentVariable("R_HOME", rhome);
        System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("PATH") + ";" + rhome + @"\bin\i386");

        REngine.SetDllDirectory(@"E:\R-3.0.2\bin\x64");

        using (REngine engine = REngine.CreateInstance("RDotNet"))
        {

              engine.Initialize();

              REngine R = REngine.GetInstanceFromID("RDotNet");

            //  engine.Evaluate("library(randomForest)");
            engine.Evaluate("source('D:/fun.R')");  <<<---- getting  from R   , contain Random Forest  codes.



            Console.ReadKey();

        }
    }
}
}




I am using R-3.0.2 , and would like to make this file as an dll file , can any one help me with this , and I have this question , after making the dll how to access the dll , and do we need R installed if we have to access the dll.




Thanks

ASHIS DEB
Mar 20, 2014 at 2:33 AM
Ashis,

I am not sure what you mean by dll, a C dll, a C# dll and just for your computer (your are hard coding paths in that code) or for distribution.

But in general, see the C# documentation for how to make a dll, and yes, R will need to be installed wherever you use it.
Mar 21, 2014 at 5:14 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using RDotNet;
using System.IO;
//using Microsoft.Win32;
using System.Threading.Tasks;
using Rconnectnuget;
using RDotNet.Graphics;
using RDotNet.NativeLibrary;
using RDotNet.Internals;
using NuGet.Commands;
using RDotNet.Devices;
namespace Rconnectnuget.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";

return View();
}

public ActionResult About()
{
return View();
}
// public static string ExpandEnvironmentVariables(
// string name
//);

string values = " ";
//_______________________________________________________________________________________________
public ActionResult getfrom(string t1)
{
string val = "";

try
{


var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = @"C:\Program Files\R\R-3.0.2\bin\i386";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
//string PATH = System.IO.Path.GetFullPath("i386");
//REngine.SetDllDirectory(@"C:\Program Files\R\R-3.0.2\bin\i386");
//string rh1 = @"C:\Program Files\R\R-3.0.2\bin\i386";
//REngine.SetDllDirectory(rh1);
using (REngine engine = REngine.CreateInstance("RDotNet"))
{

// Initializes settings.
engine.Initialize();
//InitializeComponent();


engine.Evaluate("test<- read.csv('C:\\Documents and Settings\\user_2\\Desktop\\test.csv', header=T)");
NumericMatrix testmat = engine.GetSymbol("test").AsNumericMatrix();
var myvar = 3;

var e = engine.Evaluate("x <- " + myvar);

NumericVector x = engine.GetSymbol("x").AsNumeric();
int[] a = new int[5] { 1, 2, 3, 4, 5 };

//--------------------------------for array converted as string-------------------------------------
for (var i = 0; i < a.Length; i++)
{
if (i == 0)
{
values = a[i].ToString();
}
else
{

values = values + ',' + a[i].ToString();
}
}
//----------------------------------------------------------------------------------------------------
engine.Evaluate("y <- c(" + values + ")");
//var numvec = engine.CreateNumericVector(new[] { 0.0, 1.2, 5.1, 6.2 });
//engine.SetSymbol("Grades", numvec);
//var unitvec = engine.CreateCharacterVector(new[] { "one", "one", "two", "two" });
//engine.SetSymbol("Units", unitvec);
//engine.Evaluate("Frame <- data.frame(Grades,Units)");
//engine.Evaluate("Analysis <- aov(Grades~Units, data=Frame)");
NumericVector y = engine.GetSymbol("y").AsNumeric();
//---------------------------------------------------------------------------------------------------------
engine.Evaluate("a<-c(3,7,19,34)");
engine.Evaluate("b<-c(3,5,6,10)");

engine.Evaluate("c<-c(4,3,3,4,4)");
engine.Evaluate("d<-c(2,2,2,2,2)");
engine.Evaluate("e<-(a*b)*(c/d)");
engine.Evaluate("f<-c(a,b,c,d,e)");

//engine.Evaluate("library(stats)");
//engine.Evaluate("ga<- rgamma(5,5,scale=1)");
engine.Evaluate("mat1<-matrix(f,5,5)");
engine.Evaluate("mat2<-t(mat1)");
engine.Evaluate("mat3<-matrix(1:25,5,5)");
engine.Evaluate("mat4<-matrix(25:50,5,5)");
engine.Evaluate("mat5<-matrix(51:75,5,5)");
//engine.Evaluate("example(barplot)");
engine.Evaluate("resmat<-mat1*(mat2+mat3)-mat4%*%mat5");
//SymbolicExpression g = engine.GetSymbol("g").AsExpression();
NumericMatrix testmat1 = engine.GetSymbol("resmat").AsNumericMatrix();
//engine.Evaluate("mat1<-edit(mat1)");
NumericVector mat1 = engine.GetSymbol("mat1").AsNumeric();
//var r1 = engine.Evaluate("lm(a~b)");
//NumericVector ga = engine.GetSymbol("ga").AsNumeric();
//NumericMatrix lis = engine.GetSymbol("lis").AsNumericMatrix();
//NumericVector mat2 = engine.GetSymbol("mat2").AsNumeric();
NumericMatrix mat2 = engine.GetSymbol("mat1").AsNumericMatrix();
//val = Convert.ToString(mat2[3]);
//val1 = Convert.ToString(mat3[1,1]);
//----------------------------------------------------------------------------------------------------------------
engine.Evaluate("a <- c(1:10)");
engine.Evaluate("b <- c(11:20)");
engine.Evaluate("c <- c(21:30)");
engine.Evaluate("d <- c(31:40)");
engine.Evaluate("e <- c(41:50)");
engine.Evaluate("m<-max(a)");
NumericVector m = engine.GetSymbol("m").AsNumeric();
engine.Evaluate("f <- a*d ");
engine.Evaluate("accounts <-data.frame(PRODUCT_ID=a,QUANTITY=b,PRICE=c,GROSS_WEIGHT=d,NET_WEIGHT=e,TOTAL_AMOUNT=f)");
engine.Evaluate(" accounts1 <- data.frame(id=a,price=b)");
DataFrame df = engine.GetSymbol("accounts").AsDataFrame();
//engine.Evaluate(" accounts<-edit(accounts)");

//------------------------------------------------------------------------------------------------------------------

val = df[1, 5].ToString();
engine.Close();
}
}
catch (Exception e)
{
string error = e.Message.ToString();

}
return Content(val);
}

private void InitializeComponent()
{
throw new NotImplementedException();
}

//private void DllImport(string p)
//{
// throw new NotImplementedException();
//}

//private string EnvironmentVariableTarget(string p)
//{
// throw new NotImplementedException();
//}






//public string r { get; set; }

//public object rnd { get; set; }

//public string rh1 { get; set; }
}
}