I need to interface R to some C# application. I installed rscproxy_1.3
and R_Scilab_DCOM3.0-1B5
added COM references to the STATCONNECTORCLNTLib
, StatConnectorCommonLib
and STATCONNECTORSRVLib
but I still cannot get it working.
When I run following test program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//COM references
using STATCONNECTORCLNTLib;
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;
namespace R_TESTING
{
class Program
{
static void Main(string[] args)
{
StatConnector sc1 = new STATCONNECTOR开发者_JAVA百科SRVLib.StatConnectorClass();
sc1.Init("R");
}
}
}
I get this exception:
Unhandled Exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040013
at STATCONNECTORSRVLib.StatConnectorClass.Init(String bstrConnectorName)
Thanks in advance.
UPDATE: Ok, still no luck. I will try to explain what I did so far.
Installed R-2.12.2-win.exe from rproject to the
C:\Program Files\R\R-2.12.2
Downloaded rscproxy_1.3-1.zip and copy/pasted it to the
C:\Program Files\R\R-2.12.2\library
Installed R_Scilab_DCOM3.0-1B5.exe to the
C:\Program Files (x86)\R\(D)COM Server
With Scilab comes a basic test. I tried to run it but I got following error:
Loading StatConnector Server... Done Initializing R...Function call failed Code: -2147221485 Text: installation problem: unable to load connector Releasing StatConnector Server...Done
Than I looked in the PATH/System Variables and found no path/R_HOME/R_USER info. Also, I couldn't find anything R related in the registry.
I guess I am doing something terribly wrong, so I desperately need help from you guys.
You can have a look at R.NET, for another approach...
Ok, I solved it finally. The problem is that R (D)Com doesn't work with current version of R. I installed 2.11.1 and it worked out of box.
Thanks a lot.
Use R.NET (I installed mine from NuGet) and the following code in a new C# console app (which was copied with minor changes from http://rdotnet.codeplex.com/).
It will work when pointed at the 32-bit version of R v2.11.1, but it will not work when pointed at the 64-bit version of R v2.11.1 (as noted in the code below).
When I installed NuGet, it automatically added references to the current project: RDotNet
(RDotNet.dll) and RDotNet.NativeLIbrary
(RDotNet.NativeLibrary.dll). You'll need these references in any new project.
Works under VS2012 (untested under VS2010, but will probably work).
Works when compiled for both "x32" and "All CPU" (under "Build..Configuration Manager" in VS2012).
// Call R from .NET. Advantage is that everything is in process.
// Tested on VS2012, will probably work on VS2010.
using System;
using System.IO;
using System.Linq;
using RDotNet;
class Program
{
static void Main(string[] args)
{
// Set the folder in which R.dll locates.
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = @"C:\Program Files (x86)\R\R-2.11.1\bin";
//var rBinPath = @"C:\Program Files\R\R-2.11.1-x64\bin"; // Doesn't work ("DLL was not found.")
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
using (REngine engine = REngine.CreateInstance("RDotNet"))
{
// Initializes settings.
engine.Initialize();
// .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();
Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
Console.WriteLine("P-value = {0:0.000}", p);
}
}
}
From here:
I think I just remembered how to solve the 80040013 exception. I think it had to do with the fact that my install of the R files did not include a rscproxy.dll. Somewhere along the way, R changed from using a dll named (I think) proxy.dll and started using one called rscproxy.dll. You have to find and download rscproxy.dll to the R\bin folder.
If that doesn't work, check your environment variables to be sure they R_HOME and R_USER values are pointing to the R\bin folder. Make sure the R (D)Com objects are properly registered with Windows.
Here is how to call a custom R Function from an Rdata file ( not just a built-in R function). Actually this is what worked for me. To get StatConnectorClass working, I had to open up the properties for StatConnectorsRVLib and set 'Embed Interop Types' to False.
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;
using STATCONNECTORCLNTLib;
StatConnectorClass rConn = new StatConnectorClass();
try
{
rConn.Init("R"); // here is where we initialize R
Response.Write("Initialized." + "<br />"); Response.Flush();
Response.Write("1" + "<br />"); Response.Flush();
string path = @"C:SOMEPATH\Black-Scholes.RData";
rConn.SetSymbol("path", path);
Response.Write("2" + "<br />"); Response.Flush();
rConn.Evaluate("load(path)");
Response.Write("3" + "<br />"); Response.Flush();
Int16 entry = 27;
rConn.SetSymbol("n1", entry);
Response.Write("6" + "<br />"); Response.Flush();
rConn.Evaluate("x1<-samplefn(n1)" );
Response.Write("Entered : " + entry.ToString() + "<br/> ");
Object o = rConn.GetSymbol("x1");
Response.Write("Ans:" + o.ToString() + "<br />"); Response.Flush();
rConn.Close();
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message );//+ " xx " + rConn.GetErrorText());
rConn.Close();
}
Hope this helps!
Problem:
Loading StatConnector Server... Done Initializing R...Function call failed Code: -2147221485 Text: installation problem: unable to load connector Releasing StatConnector Server...Done
Solution: [By the way, I have R, RStudio, Revolution R in my PC]
1. Install the necessary programs and packages:
For performing exponential smoothing in Eviews (7.2), ExpSmooth add-ins is necessary.
For the ExpSmooth add-ins to properly work, the program R, and the packages forecast and rscproxy must be installed; and statconnDCOM must be installed.
2. Put the files and folders to the correct BIN and LIBRARY locations:
Copy-paste the file rscproxy.dll to the C:\Program Files\R\R-2.15.3\bin
and C:\Revolution\R-Enterprise-6.1\R-2.14.2\bin
folders.
Put the rscproxy folder in rscproxy_2.0-5.zip file to the C:\Revolution\R-Enterprise-6.1\R-2.14.2\library
and C:\Program Files\R\R-2.15.3\library
folders.
3. Additional info:
I did not make any changes to environment variables to overcome the problem in question.
I had set some environment variables for the first time when I installed R and Revolution R like that (this setting is irrelavant with the context here):
System variables - Path:
C:\Rtools\bin;C:\Program Files\R\R-3.0.2\bin\i386;C:\Rtools\gcc-4.6.3\bin;
(I offer you to set the R version that you installed and use).
Run Server 01 – Basic Test
that existed upon installing statconnDCOM: Start - statconn – DCOM - “Server 01 – Basic Test”.
Press to R.
statconnDCOM worked.
精彩评论