Steve
Steve

Reputation: 45

R-Excel VBA: how to extract values returned by GetArrayToVBA?

I'm trying to write an R-Excel vba addin and am having trouble using GetArrayToVBA.

Example:

RInterface.StartRServer
RInterface.RRun "mytst<-4"
Dim tstVar As Variant, tst As Double
tstVar = RInterface.GetArrayToVBA("mytst")
tst = CDbl(testVar)
MsgBox "count = " & CStr(tst)
RInterface.StopRServer

Results in messagebox showing count = 0. I was expecting count = 4.

Upvotes: 2

Views: 909

Answers (1)

goodside
goodside

Reputation: 4629

This is a VBA issue. You can't typecast a 1x1 array into a double with CDbl() to get the value that's in that array. You have to give it the index (0,0) that you want from the array. The following works:

RInterface.StartRServer
RInterface.RRun "mytst<-as.matrix(4)"
Dim tstVar As Variant, tst As Double
tstVar = RInterface.GetArrayToVBA("mytst")
tst = CDbl(tstVar(0, 0))
MsgBox "count = " & CStr(tst)
RInterface.StopRServer

Upvotes: 2

Related Questions