Showing posts with label tinn-r. Show all posts
Showing posts with label tinn-r. Show all posts

Debug in R

Development in R usually is performed using JGR, or Tinn-R, editors and the R console.
Since these are no IDEs for R development, this is usually the best one can have.

So, how does one debug a program in R?
The usual answer is: with print.
But the correct answer is: using the browser()function.

The browser() function works as a break point marker.
Just write browser() where a break point is needed and run the program.
When browser() is interpreted, the program hang its execution and a new command line prompt will be provided, Browse[1]>.
This command line allows one to interact with the variables available in the scope where the program is temporary hanged.
This debug command line accepts the following debug commands:
  • n: next, advances to the next line, allows step-by-step.
  • c: continue, continues the execution, until the program ends or a new browser() function is found.
  • traceback(), shows the stack trace function call.
  • Q: quit, stop the program execution.
It does not look as cool as using the mouse to insert a break point on a line, but it works.

./M6

R on Kubuntu

I'm using R on my MSc thesis, which is about a new clustering algorithm, and I found out that I was unable to install it via Adept.
It may be my fault, but there's other things I seem not to find in Adept.
So, I had to do it "manually". It was easy to find the official information, and it was also easy to install and use it.

To get R, just update the repositories and the get r-base:
$ sudo apt-get update
$ sudo apt-get install r-base

I'm not sure that I really need it, but since I'm going to develop a package, I've also installed the development stuff:
$ sudo apt-get install r-base-dev r-recommended

I've also wanted an R editor, so I've used JGR. In order to use it, I've followed the official installation under Linux.
Java is required, so when there's none, Sun's Java should be installed:
$ sudo apt-get install sun-java6-jdk

To install JGR, it's necessary to enable Java support in R:
$ sudo R CMD javareconf

And then install the JGR library:
$ sudo R
> install.packages('JGR')

After installed, run it:
> library(JGR)
> JGR()

JGR It has a small problem though, it has been installed as root and I seem unable to use it with any other user, so root will be.

So, I've automated the startup of R with JGR with a simple shell script, named runr.sh:
sudo R -f /home/m6/msc/jgr.r
While the jgr.r file contains the JGR start commands
library(JGR)
JGR()

Now, running ./runr.sh starts R and JGR all in one. It's still necessary to login as root, though.


There are alternatives to JGR that require a lot less stress. Tinn-R and R Commander are alternatives, in fact Tinn-R is the editor I elected for R in Windows environment.
For know I'll just stick with JGR because I like to explore alternatives in order to decide which is best.

./M6