[R] Scoping issue?

Thaden, John J ThadenJohnJ at uams.edu
Sun Mar 4 19:31:08 CET 2007


Hello, The code below is supposed to be a wrapper for matplot to 
do columnwise visible comparison of several matrices, but I'm 
doing something wrong because I can't access an argument called
'colnum'.  I'd be most grateful for some insight.

Thanks,
John Thaden
Little Rock, AR
################################
# mmatplot is a matplot wrapper to compare the same column of 
# several matrices. Arg y is either a list of matrices with 
# equal number of rows, or an array. The scalar n gives the 
# column of each matrix or array slab to plot. par values and
# matplot args are accepted, e.g., ylog.  mmatplot is intended
# to be mapply-compatible to test multiple columns.

mmatplot <- function(colnum, x, y, ...){
  switch(class(y),
    array = y <- y[, colnum, ], 
    list = y <- sapply(X = y, FUN = subset, select = colnum))
  stopifnot(is.matrix(y))
  matplot(x, y, ...)
}

#This is just a tester function
mmatplotTest <- function(){
  oldmf <- par("mfrow")
  par(mfrow = c(2,3))
  A <- array(data = rnorm(90), dim = c(10, 3, 3))
  L <- list(A[, , 1], A[, , 2], A[, , 3])

  # The 'main' argument below throws the error, but if
  # commented out, another error crops up due to 'colnum'.
  # Test with class(y) == "array"
  mapply(X = 1:ncol(A), FUN = mmatplot, x = 1:nrow(A), y = A,
                 main = paste("Array input, column", colnum))
  # Test with class(y) == "list"
  mapply(1:ncol(L[[1]]), mmatplot, x = 1:nrow(L[[1]]), y = L,
                 main = paste("List input, column", colnum))
  par(mfrow = oldmf) 
}

#Run the test
mmatplotTest()



More information about the R-help mailing list