[Rcom-l] Passing an array from R to VB.NET Part 2
Erich Neuwirth
erich.neuwirth at univie.ac.at
Tue Sep 14 22:20:21 CEST 2004
Here is another (possibly safer) way of doing this.
cmdString is some R command.
cmdString = "temp <- capture.output(print(try({" & _
cmdString & "},silent=TRUE)))"
try behaves more sensibly when an error occurs.
Robert Byrnes wrote:
> Perhaps something like this is needed. This code (written in C#) captures
> display output intended for the R GUI and sends it:
>
> STATCONNECTORSRVLib.IStatConnector sc;
> sc = new STATCONNECTORSRVLib.StatConnectorClass();
>
> // intermediate code
> // construct a neural network
> sc.EvaluateNoReturn(@"temp<-capture.output(nn1<-(nnet(........))");
>
> String[] temp = (String[]) sc.Evaluate(@"as.character(temp)");
>
> // send temp to display
> ....
>
> --Bob Byrnes
>
>
> On Tue, 14 Sep 2004, Erich Neuwirth wrote:
>
>
>>No guarantee, but instead of
>>
>>oLogRets = comStat.GetSymbol("logrets")
>>
>>try
>>
>>oLogRets = comStat.GetSymbol("as.matrix(logrets)")
>>
>>
>>
>>Taher Khan wrote:
>>
>>
>>>Greetings!
>>>
>>>Sorry to bother all of you again, and many thanks to Dr. van der Wijk
>>>for the help!
>>>
>>>OK, so instead of a dataset, I pass the values to a double array, and
>>>that works fine for passing to R. But then, when I want to get a double
>>>array back, even when I dimensionalize the receiving array perfectly to
>>>accept, it won't take it! The first few parts, which does the same
>>>execution but to get a single value (mean and sd) work just fine, but to
>>>get the vector doesn't unless you loop it through value by value.
>>>
>>>I wonder if it has to do with the fact that VB.NET is base 0 and R is
>>>base 1, but then again, C# is base 0 also, and that works, so must be
>>>something else! Maybe a simple error in how I pass the values? Is there
>>>a syntax error in the last line that is included, the one that doesn't
>>>work?
>>>
>>>Anyways, it does work when I loop through each value, I have provided
>>>the code below. But is there a smarter way to do it?
>>>
>>>=======================================================================
>>>
>>>Dim comStat As New StatConnector
>>>
>>> 'initialize R server and transfer array to R variable
>>> comStat.Init("R")
>>> comStat.SetSymbol("navs", dblNAV)
>>>
>>> 'create a variable for average value of logrets and get from R
>>>using Evaluate and put in textbox
>>> Dim oAvNAV As Object
>>> oAvNAV = comStat.Evaluate("avnav <- mean(diff(log(navs)))")
>>> txbAvNAV.Text = Format$(CDbl(oAvNAV), "#.####").ToString 'Format
>>>value to four decimals
>>>
>>> 'create a variable for standard deviation, get from R using
>>>GetSymbol, and put in textbox
>>> Dim oStDev As Object
>>> ' oStDev = comStat.Evaluate("std <- sd(diff(log(navs)))") 'works,
>>>just wanted to test a different way below to make sure GetSymbol works
>>>as well...
>>> comStat.EvaluateNoReturn("std <- sd(diff(log(navs)))")
>>> oStDev = comStat.GetSymbol("std")
>>> txbStDev.Text = Format$(CDbl(oStDev), "#.####").ToString 'Format
>>>value to four decimals
>>>
>>> 'create a variable for logrets, get from R, first null value
>>>removed automatically by R
>>> comStat.EvaluateNoReturn("logrets <-
>>>as.numeric(diff(log(navs)))")
>>>
>>> 'measure length of R vector
>>> comStat.EvaluateNoReturn("logrets.length <- length(logrets)")
>>>
>>> 'use length of R vector as counter
>>> Dim iLength As Integer
>>> iLength = comStat.GetSymbol("logrets.length")
>>>
>>> 'create object array , get each index value and loop through,
>>>base 0 for VB, base 1 for R
>>> Dim oLogRets As Object() = New Object(iLength - 1) {}
>>> comStat.EvaluateNoReturn("i <- (1)")
>>> For i As Integer = 0 To (iLength - 1)
>>> comStat.EvaluateNoReturn("singleval <- logrets[i]")
>>> oLogRets(i) = comStat.GetSymbol("singleval")
>>> comStat.EvaluateNoReturn("i <- i+1")
>>> Next
>>> 'OK, so that works the hard way, but what about the easy way...
>>> 'THE FOLLOWING LINE DOES NOT WORK, WHAT IS WRONG WITH MY
>>>THINKING? oLogRets = comStat.GetSymbol("logrets")
>>>
>>>OK, just in case someone has been down the same road before, please let
>>>me know, and if not, well, here is somewhat of an answer for posterity
>>>in the list...
>>>
>>>Thanks in advance and best regards,
>>>
>>>tk.
>>>
>>>_______________________________________________
>>>Rcom-l mailing list
>>>Rcom-l at mailman.csd.univie.ac.at
>>>http://mailman.csd.univie.ac.at/mailman/listinfo/rcom-l
>>>
>>
>>
>>
>
> _______________________________________________
> Rcom-l mailing list
> Rcom-l at mailman.csd.univie.ac.at
> http://mailman.csd.univie.ac.at/mailman/listinfo/rcom-l
>
--
Erich Neuwirth, Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-38624 Fax: +43-1-4277-9386
More information about the Rcom-l
mailing list