Wednesday, February 13, 2013

[R] install package error and solution


1) Older version of R


Warning message:
In install.packages(c("sp")) : package ‘sp’ is not available

This is the message that you get when the CRAN package you’re interested in requires a more recent version of R than you have.  Remember, the default behavior of install.packages() is to grab the latest version of a package.
In this case you have to poke around in the “Old sources” link on the CRAN page for that package and use trial-and-error to find an older version of the package that will work with your version of R.  You should start by determining what version of R you have:

$ R --version
R version 2.8.1 (2008-12-22)

This version of R was released at the end of 2008 and any version of the “sp” package released in 2008 should work. At least some of the 2009 releases should also work. Perusing the sp archive, we might try installing version 0.9-37, the last of the 0.9-3x series which was released in May of 2009:

$ wget http://cran.r-project.org/src/contrib/Archive/sp/sp_0.9-37.tar.gz
$ sudo CMD INSTALL sp_0.9-37.tar.gz
...
$ # Success!

2) Unable to execute files in /tmp directory


ERROR: 'configure' exists but is not executable -- see the 'R Installation and Administration Manual'

By default, R uses the /tmp directory to install packages.  On security conscious machines, the /tmp directory is often marked as “noexec” in the /etc/fstab file.  This means that no file under /tmp can ever be executed.  Packages that require compilation or that have self-inflating data will fail with the error above.  One such package is RJSONIO.
The solution is to set the TMPDIR environment variable which R will use as the compilation directory. For csh shell:

$ mkdir ~/tmp
$ setenv TMPDIR ~/tmp

And for bash:

$ mkdir ~/tmp
$ export TMPDIR=~/tmp

Friday, November 9, 2012

[R] Difference between cat and print


I think the main difference is that cat does not know about
objects.  It just unclasses the input (someone can correct
me on this if this is not true), converts each of the
unclassed inputs to character and concatenates and displays
the contatenated result on the standard output or to a file,
if specified. For example, 

 x <- 3
 class(x) <- "myclass"
 cat(x, "\n")
 cat(unclass(x), "\n")  # same

In contrast, print, format and as.character are S3 generics
which look at the class of their first argument and dispatch
a different method depending on which class is.  e.g.

 # using x from above
 print.myclass <- function(x) cat("***", x, "***\n")
 print(x)

The dispatched method will understand how to represent the
object as a character string so that it can display it in
the case of print or return it as a character string in the
case of format and as.character.  as.character is often just
a wrapper around format, e.g. the source for
as.character.Date is:

 as.character.Date <- function(x, ...) format(x, ...)

You can find out which methods are actually available using:

 methods(print)
 methods(format)
 methods(as.character)

The default method is used, e.g. print.default, if there is no 
available method for the class of the object.

If one wants to display a character string with control over
newlines then one typically uses cat.  If one wants to display
an object one uses print or else converts it to a character string
using format or as.character and then display it using cat.

If you just type the name of an object into R then it invokes
the print method for that object.  e.g