Re: [R] Calling R functions into C# or C++
Hi, In my case I don't have any static data to be written in the .r file. I need to retrieve the data from my Database. later send the data and the .r script to R engine and do the execution. -- View this message in context: http://r.789695.n4.nabble.com/Calling-R-functions-into-C-or-C-tp904267p4685379.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling R functions into C# or C++
The zip file actually works fine for me. Anyhow, here is the code snippet that you need: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Interop.STATCONNECTORSRVLib; namespace RFromCsharp { class RConnector { private StatConnectorClass rdcom = null; private string rcmd; public StatConnectorClass RConnection { get { return rdcom; } set { rdcom = value; } } private bool initR() { try { rdcom = new StatConnectorClass(); rdcom.Init(R); return true; } catch(Exception e) { string errmsg = R Init failed: + rdcom.GetErrorText() + Other: +e.Message.ToString(); Console.WriteLine(errmsg); return false; } } private bool loadR(string table, string filename, bool stripwhite, bool header, string separator) { try { rcmd = table.ToString() + -read.delim(' + filename.ToString() + ',strip.white= + stripwhite.ToString().ToUpper() + ,header= + header.ToString().ToUpper() + ,sep=' + separator.ToString() + '); rdcom.EvaluateNoReturn(rcmd); return true; } catch(Exception e) { string errmsg = rcmd.ToString() + + rdcom.GetErrorText() + Other: + e.Message.ToString(); Console.WriteLine(errmsg); return false; } } private bool closeR() { try { rcmd = graphics.off(); rdcom.EvaluateNoReturn(rcmd); rcmd = rm(list=ls(all=TRUE)); rdcom.EvaluateNoReturn(rcmd); rdcom.Close(); return true; } catch(Exception e) { string errmsg = R Close failed: + rdcom.GetErrorText() + Other: + e.Message.ToString(); Console.WriteLine(errmsg); return false; } } static void Main(string[] args) { RConnector conn = new RConnector(); // Initialize the instance to be used with R conn.initR(); // create an R variable named abc and assign it the value of 5 conn.RConnection.SetSymbol(abc, 5); // Retrieve the value of the R variable named abc and assign that value to the F# value valueForabc var valueForabc = conn.RConnection.GetSymbol(abc); // Evaluate an expression in R and assign that value to an F# value aTestEvaluation var aTestEvaluation = conn.RConnection.Evaluate(8 * sin(4)); // Close the R connection conn.closeR(); Console.BackgroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine(Value of abc: + valueForabc); Console.WriteLine(Value of 8 * sin(4): + aTestEvaluation); Console.WriteLine(Press any key to continue ...); Console.ReadKey(); //- } } } You would also need to reference the Interop.STATCONNECTORSRVLib.dll assembly. Here is snapshot of my references list: http://n4.nabble.com/file/n1744914/RFromCsharpReferences.png Best regards, Fayssal El Moufatich -- View this message in context: http://n4.nabble.com/Calling-R-functions-into-C-or-C-tp904267p1744914.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling R functions into C# or C++
Fayssal, This zip file appears to be corrupted - do you have another version? -- View this message in context: http://n4.nabble.com/Calling-R-functions-into-C-or-C-tp904267p1679002.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling R functions into C# or C++
Hi, It is possible to call R functions from .NET by using a .NET wrapper over the R (D)Com server. http://www.nabble.com/file/p25682136/RFromCsharp.zip Attached is a sample C# project that shows how to call R from C#. Best Regards, Fayssal El Moufatich http://www.nabble.com/file/p25682136/RFromCsharp.zip RFromCsharp.zip Shubha Vishwanath Karanth wrote: Hi R, I have a request... Can I call R functions/routines from C# or C++? If so, how do I do this? Do I need to have some knowledge on DLL to do this? Thanks a lot for your help, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San José * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/Calling-R-functions-into-C--or-C%2B%2B-tp17998762p25682136.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Calling R functions into C# or C++
Hi R, I have a request... Can I call R functions/routines from C# or C++? If so, how do I do this? Do I need to have some knowledge on DLL to do this? Thanks a lot for your help, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San José * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling R functions into C# or C++
Shubha Vishwanath Karanth shubhak at ambaresearch.com writes: Can I call R functions/routines from C# or C++? If so, how do I do this? Do I need to have some knowledge on DLL to do this? You can, using R(DCOM) which is installed by the Windows installer. There is a separate list to discuss this, but it is mainly focused on Excel interaction. http://mailman.csd.univie.ac.at/mailman/listinfo/rcom-l When you select the full install of R(D)COM, you also get an MFC/C++ sample. C#/.NET works, I use it heavily, and you can generate nice interfaces for R with it. You must reference StatConnectorClntLib, StatconnectorCommonLib, StatConnectorSRVLib, and you get an implicit reference to AxStatConnectorClntLib. No explicitely generated DLL is required with Visual Studio, but don't expect this to work easily when you fear DLL creation. There is a major problem with Data Excecution Prevention and installation of C# procedure calling R(D)COM with Vista which I only could get around by disabling DEP. Dieter __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.