0

I'm trying to execute a R code using Excel with the Rexcel tool.

I´ve created a button and an associate macro that executes the following code:

Sub button()
    RInterface.StartRServer
    RInterface.RunRFile "C:/Users/fran21/Downloads/R_NYSE_Hadoop/predicciones.R"
    RInterface.GetArray "array", Range("A24")
    RInterface.StopRServer    
End Sub

I use RunRFile to execute my R code and GetArray to try to show a variable of my R code. The execution doesn't throw errors but I can´t see the results!

Community
  • 1
  • 1

1 Answers1

0

Without seeing predicciones.R its difficult to say what the problem is, but try this:

Sub button()
    RInterface.StartRServer 

This should be done in the sub auto_open or otherwise separately. You don't want R to be started every time you run this kind of macro.

    RInterface.rrun "source('C:/Users/fran21/Downloads/R_NYSE_Hadoop/predicciones.R')"
    RInterface.rrun "print('array')" 

printing just to check, this isn't necessary

    Range("A24") = RInterface.GetRExpressionValueToVBA("array") 

Just for a speed improvement. If it doesn't work then try getarray again. If you're working with large matricies and you want more robust code that does work more quickly, I wrote a large function I can post here

    'RInterface.StopRServer    

I wouldn't do this unless you're not going to run any R code again for the rest of the excel session

End Sub
hedgedandlevered
  • 2,314
  • 2
  • 25
  • 54