domingo, 30 de enero de 2011

R installation - Latest version on ubuntu in order to allow syntactic sugar in Rcpp

Today (the day that I begin the post) is 30-01-2010.

I don't remember exactly how I install R in my Ubuntu 9.10 Karmic Koala (I have a Pentium Dual Core T4200 which corresponds to x86_64). But when I startup R (typing R in a ubuntu terminal) then it says: R version 2.9.2 (2009-08-24). In the R FAQ page (today) it says that the actual version is 2.12.1 so I have an outdated R version.

Normally I will not have problem about having outdated versions of a given soft meanwhile it is useful to me. But, today I tried to install the Rcpp package and fails (as I will state later it seems that the reason is that the R version is too old). At least, I couldn't from the R prompt via:

> install.packages("Rcpp")
Warning in install.packages("Rcpp") :
  argument 'lib' is missing: using '/home/juan/R/x86_64-pc-linux-gnu-library/2.9'
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
  package ‘Rcpp’ is not available

So, I ask my self: what happens? Then (obviously) I google and found this post but it seems that it doesn't solve the problem (or at least I couldn't figure out how from it). Then I found this other which says that it could be that the R version is too old. This is the reason for that I want the latest version of R in my computer. I want to install the Rcpp package with syntactic sugar.

But now, there is another problem. I can install R from the synaptic manager of ubuntu but I don't get the latest one (I get the 2.9.2 as I state above), in fact I can install the Rcpp package from there. But the package version also is too old as I can see via:
> z<-installed.packages()
> z["Rcpp","Version"]
[1] "0.6.5".

In fact, as is told here, syntactic sugar is included in the Rcpp package from the version 0.8.3

So, the solution seems to install the latest version of R. But:

1) From where?
2) How?.
3) I'll need to uninstall my actual version of R?

I'm a bit (ok, more than a bit) scared to experiment with it (now) because I'm ending my PhD. Thesis. I don't want my system down now !!!
Anyway, lets try...
So, I try what it says about ubuntu in CRAN, that is I look in and via (the ubuntu menu) System -> Administration -> Software Sources -> Other Software tab add in the /etc/apt/sources.list file, a line like this:

deb http://<my.favorite.cran.mirror>/bin/linux/ubuntu karmic

In fact, this line is a kind of prototype, in the "real" one must replace <my.favorite.cran.mirror> by your prefered CRAN mirror (you can get a list of them here In my case this line becames: karmic

(note that this line depends on the ubuntu version, look in for how to be done in the other versions). Then ubuntu ask me to update, so I do that but I get this error:

W: GPG error: karmic/ Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY D67FC6EAE2A11821

So I simply do what is said here, that is, in a ubuntu terminal I run the commands:

gpg --keyserver --recv D67FC6EAE2A11821
gpg --export --armor D67FC6EAE2A11821 | sudo apt-key add -

Then ubuntu ask me to update again and it works. So I continue with what is said in, that is I run in a ubuntu terminal the commands:

sudo apt-get update
sudo apt-get install r-base
sudo apt-get install r-base-dev

Now I have the 2.12.1 R Version as I can view executing R in a ubuntu terminal.
Now it is time to see if I can install the latest version of Rcpp. So the first thing is to see if it is already installed. So I get:

> library("Rcpp")
Error: package 'Rcpp' was built before R 2.10.0: please re-install it

Now, it happens that when I tried (before upgrade R) to update all packages via:

> update.packages()

it get stuck in some "place" trying to update the network package. Lets see what happens now... Ok, after a while I get:

Warning messages:
1: In install.packages(update[instlib == l, "Package"], l, contriburl = contriburl,  :
  installation of package 'Rcpp' had non-zero exit status
2: In install.packages(update[instlib == l, "Package"], l, contriburl = contriburl,  :
  installation of package 'foreach' had non-zero exit status
3: In install.packages(update[instlib == l, "Package"], l, contriburl = contriburl,  :
  installation of package 'doMC' had non-zero exit status

So it seems to work partially, and in fact it fails with "Rcpp", the one that I mainly want to install !!! So, now I will try:

> install.package("Rcpp")

It seems that it work because I get:

> library("Rcpp")
> z <- installed.packages()
> z["Rcpp","Version"]
[1] "0.9.0"

Lets see if I can install the other 2 packages that give me an error before. So I first try:
> install.packages("foreach")

and I get:

> library("foreach")
Loading required package: iterators
Loading required package: codetools
foreach: simple, scalable parallel programming from REvolution Computing
Use REvolution R for scalability, fault tolerance and more.
> z <- installed.packages()
> z["foreach","Version"]
[1] "1.3.0"

With the other package I get this:

> install.packages("doMC")
Installing package(s) into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL ''
Content type 'application/x-gzip' length 93727 bytes (91 Kb)
opened URL
downloaded 91 Kb

* installing *source* package ‘doMC’ ...
** R
** demo
** inst
** preparing package for lazy loading
Loading required package: iterators
Loading required package: codetools
Error : package 'multicore' was built before R 2.10.0: please re-install it
ERROR: lazy loading failed for package ‘doMC’
* removing ‘/usr/local/lib/R/site-library/doMC’

The downloaded packages are in
Warning message:
In install.packages("doMC") :
  installation of package 'doMC' had non-zero exit status

So, something goes wrong. It seems that the problem is that the multicore package is outdated, so I reinstall it:

> install.packages("multicore")
... BLA BLA BLA ...
> library("multicore")
> z <- installed.packages()
> z["multicore","Version"]
[1] "0.1-3"
> install.packages("doMC")
... BLA BLA BLA ...

> library("doMC")
> z <- installed.packages()
> z["doMC","Version"]
[1] "1.2.1"

So it seems it works. Now lets try with the igraph package:

> library("igraph")
Error in library("igraph") : there is no package called 'igraph'
> install.packages("igraph")
... A LOT OF BLA BLA ...
> library("igraph")
> z <- installed.packages()
> z["igraph","Version"]
[1] "0.5.5-1"

So it seems to work. Lets try the last one, the network package:

> library("network")
Error in library("network") : there is no package called 'network'
> install.packages("network")
... BLA BLA ...

> library("network")
     Classes for Relational Data
Version      1.6 created on      January 28, 2011.
copyright (c) 2005, Carter T. Butts, University of California-Irvine
                    Mark S. Handcock, University of Washington
                    David R. Hunter, Penn State University
                    Martina Morris, University of Washington
For citation information, type citation("network").
Type help("network-package") to get started.

Attaching package: 'network'

The following object(s) are masked from 'package:igraph':

    add.edges, add.vertices, %c%, delete.edges, delete.vertices,
    get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
    is.directed, list.edge.attributes, list.vertex.attributes, %s%,
    set.edge.attribute, set.vertex.attribute

> z <- installed.packages()
> z["network","Version"]
[1] "1.6"

So it also seems to work.
Now it remains to make some test to see if the installed packages works. Principally Rcpp and igraph.

So, igraph works nicely with:

> g <-
> jpeg("test.jpg")
> plot(g, layout=layout.fruchterman.reingold, vertex.size=4, vertex.label.dist=0.5, vertex.color="red", edge.arrow.size=0.5)

it gives:

Lets see Rcpp. First I install the inline package (as required) and then run (as sugested here):

> require(inline)
Loading required package: inline
> testfun = cxxfunction(
+ signature(x="numeric",i="integer"),
+ body = 'NumericVector xx(x);
+         int ii = as<int>(i);
+         xx = xx * ii;
+         return( xx );
+        ', 
+ plugin="Rcpp")
> testfun(1:5,3)
[1]  3  6  9 12 15

So, it was a success !!!


3 comentarios:

Anónimo dijo...

Thank you so much for this writeup! I too am near the end of my PhD thesis, and this saved my life.

Ary dijo...

Mire usted que chico es el mundo... vamos a ver si puedo arreglar este problemita..
Leo Saravia

rayo dijo...

jajajaj, hola leo, como va? es verdad, que chico que es el mundo !!! cualquier cosa preguntá. Abrazo !!!