[R] Scoping issue?
Thaden, John J
ThadenJohnJ at uams.edu
Mon Mar 5 04:36:13 CET 2007
Apparently you're right that colnum doesn't exist when it needs to
be evaluated, but why? Why is 'paste' being evaluated so early? It is,
after all, the value of an argument ('main') of my mmatplot function
with colnum being another argument. I thought arguments were lazy-loaded.
Does using mapply change the rules?
Is there a way (like mapply) to loop at some lower level rather than
Explicitly, in the R script, as in your suggestion? For speed's sake?
Thanks. -John
On Sunday Mar 4 2007, jim holtman <jholtman at gmail.com> replied
> First of all, 'colnum' does not exist when the 'paste' is called.
> This probably does what you want:
> for (colnum in 1:ncol(A)){
> mmatplot(colnum, 1:nrow(A), A, main=paste("Array input, column",
colnum))
> }
On 3/4/07, John Thaden <jthaden at uams.edu> wrote:
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
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()
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
Confidentiality Notice: This e-mail message, including any a...{{dropped}}
More information about the R-help
mailing list