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.