s-news
[Top] [All Lists]

Fwd: RE: Using Objects within a user-defined

To: s-news@wubios.wustl.edu
Subject: Fwd: RE: Using Objects within a user-defined
From: "Richard M. Heiberger" <rmh@temple.edu>
Date: Thu, 6 Jan 2005 13:34:48 -0500
Cc: "Thomas D. Fletcher" <tom.fletcher@cox.net>
Ah, The original question wasn't precise enough.  Please send a WORKING
example that shows the problem next time.

uni.g <- function(formula, dat, split=c(1, 1, 2, 2), more=F)
{
  M <- match.call()
  M[[1]] <- as.name("bwplot")
  M$split=NULL
  M$more=NULL
  a <- eval(M, parent=sys.parent())
  result <- print(a, split=split, more=more)
  invisible(result)
}

uni.g(~y1, tmp, more=T)
uni.g(~y1, tmp, split=c(2, 2, 2, 2))

tmp <- data.frame(v1=1:10)
uni.g(~v1, tmp, more=T)
uni.g(~v1, tmp, split=c(2, 2, 2, 2))



With trellis, it is better to stay in the framework of using formulas.
M takes your original call, changes it refer to the bwplot function and removes
the extra arguments that belong to print(), then evaluates it in the original
location.  Then it is printed under the control of the split and more arguments.
The returned result is invisible to avoid a second printing.




---- Original message ----
>Date: Thu, 6 Jan 2005 12:55:46 -0500
>From: "Thomas D. Fletcher" <tom.fletcher@cox.net>  
>Subject: RE: [S] Using Objects within a user-defined  
>To: "Richard M. Heiberger" <rmh@temple.edu>
>
>Thanks Rich,
>
>but, ....
>
>Suppose the data.frame element (i.e., the column or variable) has a
>different name (e.g., y1) than the function element (e.g., v1).
>
>Example:
>
>tmp <- data.frame (y1 = rnorm(50))
>
>This renders the same problem that I was encountering before. The object in
>the function "v1" was not found when running the program.
>
>The program works in the example Rich provides because the example has the
>same variable name in the data.frame as the function. The function I have
>tried to write appears to not be generalizable. So, I am still missing
>something in the program.
>
>Thanks for any help,
>Tom
>
>Rich's revision of the program:
>
>uni.g <- function(v1, dat, split=c(1, 1, 2, 2), more=F)
>{
>  a <- bwplot(~v1, data=dat)
>  result <- print(a, split=split, more=more)
>  invisible(result)
>}
>
>## uni.g(VARNAME,DATAFRAME)
>
>tmp <- data.frame(v1=1:10)
>uni.g(v1, tmp, more=T)
>uni.g(v1, tmp, split=c(2, 2, 2, 2))
>
>
>

<Prev in Thread] Current Thread [Next in Thread>