download code
## Chunk 1
 x = 10
 y = x
## Chunk 2
`_foo` = 10
"10:10" = 20
ls()
## Chunk 3
x = 1:10
attr(x, "foo") = 11
x
## Chunk 4
mean
## Chunk 5
methods("mean")
## Chunk 6
length(NULL)
c(1, NULL)
list("a", NULL)
## Chunk 7
typeof(NA)
as.character(NA)
as.integer(NA)
typeof(as.integer(NA))
## Chunk 8
is.na("NA")
## Chunk 9
y = 1/0
y
-y
y-y
## Chunk 10
typeof(y)
typeof(is.na)
typeof(mean)
mode(NA)
storage.mode(letters)
## Chunk 11
is.integer(y)
is.character(y)
is.double(y)
is.numeric(y)
## Chunk 12
1:3
1.3:3.2
6:3
x=11:20
x[4:5]
## Chunk 13
 x = c(1,2,3,4)
 x
 dim(x) = c(2,2)
 x
 typeof(x)
 y = letters[1:10]
 y
 dim(y) = c(2,5)
 y
 typeof(y)
## Chunk 14
 c(1, 3:5)
 c(1, "c")
 numeric(2)
 character(2)
 seq(1, 10, by = 2)
 seq_len(2.2)
 seq_along(numeric(0))
 sample(1:100, 5)
## Chunk 15
 sum(numeric())
 prod(numeric())
## Chunk 16
 a = sqrt(2)
 a * a == 2
 a * a - 2
  (solution chunk)
## Chunk 18
set.seed(123)
x=sample(letters[1:5], 10, replace=TRUE)
y=factor(x)
y
attributes(y)
## Chunk 19
 y=sample(letters[1:5], 20, rep=T)
 v = as.factor(y)
 xx=list(I=c("a","e"), II=c("b", "c", "d"))
 levels(v) = xx
 v
## Chunk 20
 z= ordered(y)
 class(z)
## Chunk 21
 y = list(a=1, 17, b=4:5, c="a")
 y
 names(y)
## Chunk 22
l2 = list(mn = mean, var = var)
l3 = list(l2, y)
  (solution chunk)
## Chunk 24
 e1 = new.env(hash=TRUE)
 e1$a = 10
 ls(e1)
 e1[["a"]]
## Chunk 25
 e1 = new.env()
 attr(e1, "foo") = 10
 e1
 e2 = e1
 attr(e2, "foo") = 20
 e1
## Chunk 26
e1 = new.env()
e1$z = 10
f = function(x) {
  x+z}
environment(f) = e1
f(10)
e1$z = 20
f(10)
## Chunk 27
rm(e1)
f(10)
f
## Chunk 28
capabilities()
## Chunk 29
library("geneplotter")
search()
ls(2)
## Chunk 30
class(cars)
typeof(cars)
str(cars)
object.size(cars)
## Chunk 31
head(cars)
tail(cars)
## Chunk 32
colSums
## Chunk 33
get("+")
## Chunk 34
 x=1:4
 x+5
 myP = get("+")
 myP
 myP(x, 5)
## Chunk 35
"%p%" = function(x, y) paste(x, y, sep="")
"hi" %p% "there"
## Chunk 36
 myl = list(a1 = 10, b=20, c=30)
 myl[c(2,3)]
 myl$a
 myl["a"]
 f = "b"
 myl[[f]]
 myl$f
## Chunk 37
 x= 11:20
 x[c(1,3,5)]
## Chunk 38
  x= 1:10
  x[1:3]
  x[9:11]
  x[0:1]
x[c(1,2,NA)]
## Chunk 39
x=1:5
names(x) = letters[1:5]
x[c("a", "d")]
names(x)[3] = "a"
x["a"]
x[c("a", "a")]
names(x) %in% "a"
## Chunk 40
(letters[1:10])[c(TRUE, FALSE, NA)]
(1:5)[rep(NA,6)]
## Chunk 41
x=matrix(1:9,nc=3)
x[,1]
x[1,]
## Chunk 42
x[,1,drop=FALSE]
x[1,,drop=FALSE]
  (solution chunk)
## Chunk 44
x=array(1:27, dim=c(3,3,3))
y= matrix(c(1,2,3,2,2,2,3,2,1), byrow=TRUE, ncol=3)
x[y]
## Chunk 45
 x[1:3] = 10
 x
## Chunk 46
 x=1:10
 x[-(2:4)] = 10
 x
  (solution chunk)
## Chunk 48
x=matrix(1:10, nc=2)
x[] = sort(x)
## Chunk 49
 x= 11:15
 x + 3
## Chunk 50
 nchar(month.name)
## Chunk 51
 1:10 + 1:3
## Chunk 52
 1:3 + numeric()
 1:3 + NULL
 x = matrix(1:10, nc=2)
 x+(1:2)
## Chunk 53
 x = 1:4
 #x[2] = 10
 x = "[<-"(x, 2, value=10)
 x
## Chunk 54
 names(x) = letters[1:4]
 names(x)
 x = "names<-"(x, LETTERS[1:4])
 x
  (solution chunk)
## Chunk 56
Map(paste, 1:4, letters[1:4], sep="_")
## Chunk 57
set.seed(123)
x = rnorm(1000)
x = ifelse(abs(x) > 2.2, NA, x)
y = x[!is.na(x)]
y2 = Filter(Negate(is.na), x)
all(y == y2)
## Chunk 58
sq1 = function(x) return(x*x)
sq2 = function(x) x*x
  (solution chunk)
## Chunk 60
for(i in 1:3) print(i)
for(i in 1:5) if(i > 3 ) break
i
## Chunk 61
 x = matrix(1:10, nc=2)
 ifelse( x < 2, x, c(10, 11, 12))
## Chunk 62
     centre = function(x, type) {
       switch(type,
             mean = mean(x),
             median = median(x),
             trimmed = mean(x, trim = .1))
     }
     x = rcauchy(10)
     centre(x, "mean")
     centre(x, "median")
     centre(x, "trimmed")
  (solution chunk)
  (solution chunk)
## Chunk 65
## {   top = options(show.error.messages=FALSE)
##    test = try(readLines(biocURL)[1])
##    options(top)
##    if (inherits(test,"try-error"))
##       return(FALSE)
##    else
##       close(biocURL)
##    return(TRUE)
## }
## Chunk 66
foo = function(x) {
    if( x < 3 ) list() + x
    else {
      if(x < 10 )  warning("ouch")
      else 33
   }
}
tryCatch(foo(2), error=function(e) "an error", warning = function(e)
"a warning")
tryCatch(foo(5), error=function(e) "an error", warning = function(e)
"a warning")
tryCatch(foo(29))
## Chunk 67
FNFcondition = function (message, call = NULL){
    class = c("fileNotFound", "error", "condition")
    structure(list(message = as.character(message), 
                   call = call), class = class)
}
v1 = FNFcondition("file not found")
tryCatch( signalCondition(v1), fileNotFound = function(e) e )
tryCatch( signalCondition(v1), 
         condition = function(e) "condition" )
## Chunk 68
## tryCatch(repeat(readline()), 
##     interrupt=function(e) print("howdy"))
## Chunk 69
downloadWithRestarts = function(url, destfile, ...){ 
    repeat 
        withRestarts(return(download.file(url, destfile, ...)), 
                     retryDownload = function() NULL, 
                     tryNewUrl = function(newUrl) 
                     url <<- newUrl) 
} 
## Chunk 70
## withCallingHandlers(downloadWithRestarts("http://foo.bar.org", "xyz"),
##                      error=function(e) {
##                      cat("Error:", conditionMessage(e),"\n")
##                      browser()}
##                      )
## Chunk 71
even = function(i) i %% 2 == 0
testEven = function(i) if (even(i) ) i else stop("not even")
vals = NULL
withCallingHandlers({
    for (i in seq_len(10)) {
        val = withRestarts(testEven(i),
        skipError=function() return(NULL))
        if (!is.null(val))
            vals = c(vals, val)
    }},
        error=function(e) invokeRestart("skipError"))
vals
## Chunk 72
find("+")
get("+")
## Chunk 73
assign("+", function(e1, e2) print("howdy"))
1+10
rm("+")
1+10
## Chunk 74
library(tools)
## Chunk 75
b = get("foo")
b(23)
## Chunk 76
print("a")
v = print("a")
v
## Chunk 77
x=expression(1:10)
x
eval(x)
evalq(x)
eval(quote(x))
## Chunk 78
 e = new.env()
 e$x = 10
 evalq(x, envir=e)
## Chunk 79
     gg = local({
         k = function(y) f(y)
         f = function(x) if(x) x*k(x-1) else 1
     })
     gg
     ls(environment(gg))
     for (i in 1:5) print( gg(i) )
## Chunk 80
loadedNamespaces()
MASS::lda
loadedNamespaces()
search()
## Chunk 81
foo = function() {
  y = 10
  function(x) x+y
}
bar = foo()
bar
is.function(bar)
bar(3)
## Chunk 82
 bar2 = function(x) x + z
 e1 = new.env()
 e1$z = 20
 tryCatch(bar2(11), error=function(x) "bar2 failed")
 environment(bar2) = e1
 tryCatch(bar2(11), error=function(x) "bar2 failed")
## Chunk 83
Rmlfun = 
  function(x) {
    sumx = sum(x)
    n = length(x)
    function(mu)
      n * log(mu) - mu * sumx
  }
## Chunk 84
 efun = Rmlfun(1:10)   # efun is a function!
 efun(3)
 efun2 = Rmlfun(20:30)
 efun2(3)
 efun(3)                # nothing has changed for efun
## Chunk 85
Rmklike = 
  function(data) {
    n = length(data)
    sumx = sum(data)
    lfun = function(mu) n * log(mu) - mu * sumx
    score = function(mu) n / mu - sumx
    d2 = function(mu) -n / mu^2
    list(lfun = lfun, score = score, d2 = d2)
  }
## Chunk 86
newton = function(lfun, est, tol = 1e-7, niter = 500) {
    cscore = lfun$score(est)
    if (abs(cscore) < tol)
      return(est)
    for (i in 1:niter) {
      new = est - cscore / lfun$d2(est)
      cscore = lfun$score(new)
      if (abs(cscore) < tol)
        return(new)
      est = new
    }
    stop("exceeded allowed number of iterations")
  }
## Chunk 87
e1 = new.env()
e1$a = 10
foo = function(x) x+a
environment(foo) = e1
foo(4)
## Chunk 88
e1$a = 20
foo(4)
e1[["a"]]