Tips for working with R in an Add-in

For tips, questions and general information about writing Add-ins, how to package them, and how to submit them to EViews for publication.

Moderators: EViews Gareth, EViews Moderator

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11411
Joined: Tue Sep 16, 2008 5:38 pm

Tips for working with R in an Add-in

Postby EViews Gareth » Wed Apr 14, 2010 11:39 am

Here's a few tips for writing add-ins that call R routines/functions. I'll add more as/if I think of them – feel free to add your own too.

Use a !debug variable

I find that declaring a !debug variable at the top of my program is a very useful way to easily switch between a "debug" mode and a non "debug" mode in my add-ins. This is particularly useful when dealing with R, since you can write your program to easily switch on or off the R Log. Thus something like:

Code: Select all

!debug = 1

'various lines of code here

if !debug = 1 then
   xopen(r)
else
   xopen(r, nolog)    'if not in debugging mode, do not show the R Log.
endif

You can use the !debug variable for other things too – such as turning on or off the
logmode +addin
command, or for deleting intermediary variables at the end of the program.

Having one variable at the top of your program is much easier than having to search through your program finding all the different places you need to change code whenever you want to debug/edit your program.

Use the case= option in the xopen command

Either use case=lower or case=upper in the xopen command:

Code: Select all

xopen(r, case=lower)

Otherwise you don't know whether EViews will be sending your variables, using the XPUT command, to R in upper or lower case, which makes it hard to then run R commands that use those variables.

Be careful of series naming

If you're passing over variables to R that have been defined by the end-user, there are a few tricks you can use to make sure that it works.

The first is with the variable C. In EViews C has two uses – a constant in an equation, and the standard coefficient vector. When you use the XPUT command to send "C" to R, it will send the coefficient vector. If you are sending variables that are regressors in an equation, you do not want this, rather you need to send a series full of ones. Thus you should have some code that looks something like this:

Code: Select all

%regs = "C X1 X2 X3"

for !i=1 to @wcount(%regs)
   %word = @word(%regs,!i)
   if @upper(%word)="C" then
      series ones=1
      xput ones
   else
     xput {%word}
   endif
next


You'll then have to remember that there is a variable called "ones" inside R, rather than a variable called "C".

Which brings me to the second issue with naming. I find it best to always use the @xputnames command to retrieve the name of the variable that I just put into R. For a standard series this is not really required, if I do XPUT X1, I know that I just created a variable called X1. However with auto-series, this isn't so clear. For example I can do XPUT LOG(X1), which will create a series full of the values of the log of X1 and send that over to R, but the created variable will not be called LOG(X1). Thus you'll end up with some code looking like:

Code: Select all

%regs = "X1 C LOG(X2):

%rnames = ""
for !i=1 to @wcount(%regs)
   %word = @word(%regs,!i)
   if @upper(%word)="C" then
      series ones=1
      xput ones
   else
     xput {%word}
   endif
   %putname = @xputnames
   %rnames = %rnames + " " + %putname
next

Which will put all of the variables into R, and leave you with a string variable, %rnames, containing a space delimited list of the names inside R.
Follow us on Twitter @IHSEViews

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 11411
Joined: Tue Sep 16, 2008 5:38 pm

Re: Tips for working with R in an add-in

Postby EViews Gareth » Wed Apr 14, 2010 12:07 pm

Detecting R

If you're writing an Add-in that requires R, you're going to need to ensure that the end-user has R installed and properly set up for communication with EViews. Of course the easiest way to do this is to just do xopen(r). If the end-user doesn't have R setup, then that command will fail. The unfortunately thing, though, is that it gives a somewhat inelegant error message of StatConnectorSrv.StatConnector is not registered.

I prefer to trap that error and give a somewhat useful error message, along the lines of:

Code: Select all

setmaxerrs 10
xopen(r)
%str = @lasterrstr
if (@instr(%str,"StatConnectorSrv")>0) then
   @uiprompt("This Add-in requires R, but it appears as though R is not installed. Please check your EViews documentation for instructions on installing R.")
   stop
endif


First we set the maximum errors to something greater than 1 (arbitrarily 10 in this case), so that the Add-in won't stop straight away if the xopen command fails. Then we trap any error message caused by the xopen(r) command, and if that error message contains "StatConnectorSrv", we put up a new error message and stop the Add-in.


Loading a package
You can use:

Code: Select all

xrun library(PACKAGE)

to load a library/package inside R (where you replace the word "package" with the name of the package you are trying to load). However, this will only work successfully if the end-user has already installed that package in their copy of R. With the following code:

Code: Select all

xrun "if (sum(.packages(TRUE)==""PACKAGE"")==0) install.packages(""PACKAGE"")"
xrun "library(PACKAGE)"

You can tell R to check whether PACKAGE is installed, and if not, install it, and then load it.
Follow us on Twitter @IHSEViews


Return to “Add-in Writing area”

Who is online

Users browsing this forum: No registered users and 1 guest