Of Ggobi, MacPorts and R

A very short post about my recent frustration with an update of MacPorts that completely removed the ability of R to communicate with Ggobi. The solution: install a VirtualBox Ubuntu 12.10 and run R inside for the moments when one needs rggobi.

First: follow the instructions from the detailed tutorial on osxdaily (http://osxdaily.com/2012/03/27/install-run-ubuntu-linux-virtualbox/)


1. To allow access to the shared folders, in a Terminal window on the new Ubuntu:

sudo usermod -G vboxsf -a username

This command appends the user “username” to the vboxsf group, which is the owner of the shared folders (found in /media/ on the Ubuntu box)

2. To allow higher resolution of screen, you need to install the Guest Additions, something that will allow the host VirtualBox environment to communicate with the installed Ubuntu. In a first step, one need to install some tools via the usual apt-get method:

sudo apt-get install dkms
sudo apt-get install build-essential

Next, launch the installation of the Guest Additions from the menu of the virtual machine:

Menu –> Devices>Install Guest Additions …

R, ggobi, rggobi can be installed with apt-get and work very well. I use a session with cairo-dock and still like synaptic for package management and geany for text editing. Rstudio installs without problems as well. Synapse is  a “must have” for rapid launching of any application. You can share the clipboard in both directions with Mac OS X.

EDIT (April 2013) – Everything works fine on the VirtualBox, even running a server that is accesible from “outside the box”. To be able to see a web server running on the virtual machine, you can use the port redirection option in the configuration of the virtual box. In my case, both ports were 8080:


Edit (2013-07-25): If, for any reason, you find R install on the virtual Ubuntu strange, use the official R repository at (http://cran.at.r-project.org/bin/linux/ubuntu/).

R – using functions as parameters of other functions

The beauty of R, the programming language and environment, lays in its flexibility. Using functions as arguments of other functions is nothing new, but I did not know exactly how to do it in R. One way was mentioned in a discussion, found with Google search. Since it worked well, I’m giving here an example.

Let’s say you want to count how many values from a vector are larger or smaller than a threshold – and do this while varying the threshold values. You could imagine having two different functions ‘count_larger’ and ‘count_smaller’ but why not use the comparison function as a parameter ?

count_x<-function(data_vector, comp_function, threshold){
   comp_f <- match.fun(comp_function)
   return(length(which(comp_f(data_vector, threshold))))

> data <- seq(1, 100, by=2)
> count_x(data, "<", 10)
[1] 5
> count_x(data, ">", 10)
[1] 45
> count_x(data, "!=", 10)
[1] 50
> count_x(data, "==", 10)
[1] 0

Using match.fun is pretty flexible. Maybe there are other ways to do the same thing, any comment is appreciated.

Edit: to format the code, I used the javascript tool on the format my source code page.