[R] Scoping issue?

Luke Tierney luke at stat.uiowa.edu
Mon Mar 5 13:33:15 CET 2007


In your test function there is no lexically visible definition for the
`colnum` variable used in defining main, so that error is what you
expect from lexical scoping.  Lazy evaluation dictates when (and if)
the `main` argument is evaluated, but the environment in which it is
evaluated is determined by the context where the expression is written
in the code, i.e. within the function mmatplotTest.

The error you get when you take out the `main` is coming from `subset`
and is due to the fact that subset is one of those functions that uses
non-standard evaluation for some of its arguments, in this case
`select`.  This makes it (slightly) easier to use at intereactive top
level but much more complicated to use within a function. The key in
reading the help page for `subset` is that the argument `select`
should be an expression (actually the literal argument expression is
used, which in this case is the expression consisting of the single
variable `colnum` and is not useful here). You need to use another
function in your sapply call, something like

     function(d) d[,colnum]

may do.

Best,

luke

On Sun, 4 Mar 2007, Thaden, John J wrote:

> 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.
>
>
>
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


More information about the R-help mailing list