R Lessons Learned

From Theory of Measurements Wiki

Jump to: navigation, search

Contents

Graphics

CairoDevice: cross-platform anti-aliased vector graphics. To set Cairo as default graphic device, add the following to ~/.Rprofile:

library(cairoDevice)
options(device="Cairo")

--Juha 21:29, 3 October 2006 (EEST)

Help R help you with helping yourself

> help(function)
> help.search("keyword")
> RSiteSearch("keyword")  # if nothing else helps, this will.

--Juha 02:28, 9 August 2006 (EEST)

99 Good things about

  1. R is shorter to spell than "matlab -nodesktop -nojvm -nosplash ^C^C^C^Z ; killall -9 matlab"
  2. R starts up fast
  3. R is free as in free beer.
  4. R is free as in communistic free.
  5. R is free as in Stallman free.
  6. R has flexible lists
  7. R does not contain ambiguous syntax.
  8. R add-on packages are free.
  9. R can return functions from functions.
  10. R can return functions from functions from functions ad infinitum (although I can't see any practical use of this)
  11. #!/usr/bin/r
  12. R is lisp without redundant parenthesis
  13. R is like linux

99 Bad things about R

  1. It is difficult to search anything related with R, because the name only has one letter!

Making functions with functions

Every day I get a better grasp of what R can do, vaguely remembering things that were possible with Lisp, but where rarely possible in C, C++, Fortran, perl or even python.

RgtX <- function(x)
{
  critf <- function(cs)
  {
    r <- FALSE
    if( cs $ R > x )
      r <- TRUE
    r
  }
  critf
}

--Juha 13:43, 1 August 2006 (EEST)

sh-bang with R

R has a R CMD BATCH command for batch running scripts, but I find it lacking and inelegant. A more elegant and practical way is to use rinterp, and prepend #!/usr/bin/rinterp to your script. This way the script can be used as you would use any other unix script -- command lines arguments can be passed and standard input stream can be accessed. More about rinterp here: [1]

Repetetive dictionary call and array pruning

R offers nice data structures via syntactic sugar, but sometimes their use results in a considerable slow-down that might not be evident from the code. Here is one example:

# setup 
N = 100000
structure <- list()
structure[["vector"]] <- rnorm(N)
s = 0

and two alternative ways which result in completely different execution times:

# fast O(N)
arr <- structure[["vector"]]
for (i in 1:N)
{
  s <- s + arr[i]
}

# slow O(N^2)
for (i in 1:N)
{
  s <- s + structure[["vector"]][i]
}

Personal tools