R Lessons Learned
From Theory of Measurements Wiki
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
- R is shorter to spell than "matlab -nodesktop -nojvm -nosplash ^C^C^C^Z ; killall -9 matlab"
- R starts up fast
- R is free as in free beer.
- R is free as in communistic free.
- R is free as in Stallman free.
- R has flexible lists
- R does not contain ambiguous syntax.
- R add-on packages are free.
- R can return functions from functions.
- R can return functions from functions from functions ad infinitum (although I can't see any practical use of this)
-
#!/usr/bin/r - R is lisp without redundant parenthesis
- R is like linux
99 Bad things about R
- 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]
}
