Hello I wrote a simple program to modify a boxplot: <<< gdsbox <- function(indvar){ boxplot(indvar~gds3lev, main = paste('Boxplot of', substitute(indvar), "for GDS groups"), names = c('1', '3', '4, 5, 6')) }>>>If I attach the dataframe gdsgraph, this works fine. However, I've been warned against attach. When I tried to run this program using 'with', it did not work. e.g. with(gdsgraph, gdsbox(BReT3T5T)) produced an error that gds3level was not found but if I try with(gdsgraph, boxplot(BReT3T5T~gds3lev)) it works fine. Similar problems occurred when I tried to use data What am I missing? Thanks Peter Peter L. Flom, PhD Brainscope, Inc. 212 263 7863 (MTW) 917 488 7176 (ThF) [[alternative HTML version deleted]]
Duncan Murdoch
2008-Aug-27 17:35 UTC
[R] Simple programming problem with 'with' or 'data ='
On 8/27/2008 1:15 PM, Peter Flom wrote:> Hello > > I wrote a simple program to modify a boxplot: > > <<< > > gdsbox <- function(indvar){ > boxplot(indvar~gds3lev, > main = paste('Boxplot of', substitute(indvar), "for GDS groups"), > names = c('1', '3', '4, 5, 6')) > } > >>>> > > If I attach the dataframe gdsgraph, this works fine. However, I've been warned against attach. When I tried to run this program using 'with', it did not work. > > e.g. > > with(gdsgraph, gdsbox(BReT3T5T)) > > produced an error that gds3level was not found > > but if I try > > with(gdsgraph, boxplot(BReT3T5T~gds3lev)) > > it works fine. > > Similar problems occurred when I tried to use data > > What am I missing?You defined your gdsbox function in the global environment, which means it will go looking for non-local variables there. It doesn't matter where you call it from, R uses lexical scoping. To get what you want, you could define your function within the with, i.e. with(gdsgraph, { gdsbox <- function(indvar){ boxplot(indvar~gds3lev, main = paste('Boxplot of', substitute(indvar), "for GDS groups"), names = c('1', '3', '4, 5, 6')) } gdsbox(BReT3T5T) }) but this misses the point of defining gdsbox once, and using it in many places. So I'd say the best thing to do is to change the definition of it to use the data argument, i.e. gdsbox <- function(indvar, data) { boxplot(indvar~gds3lev, main = paste('Boxplot of', substitute(indvar), "for GDS groups"), names = c('1', '3', '4, 5, 6'), data=data) } and call it as gdsbox(BReT3T5T, gdsgraph) There are ways to make your function act as though it doesn't use lexical scoping (parent.env, etc.), but they are ugly, and I don't think they're necessary here. Duncan Murdoch
Adaikalavan Ramasamy
2008-Aug-27 19:09 UTC
[R] Simple programming problem with 'with' or 'data ='
with(gdsgraph, boxplot(BReT3T5T ~ gds3lev)) is equivalent to boxplot(gdsgraph$BReT3T5T ~ gdsgraph$gds3lev), so all the terms are found. gds1lev is not found within the scope of the function gdsbox(). This function has too many hard codings to be of much use generically. Consider rewriting it as (untested function) gdsbox <- function(y, x, data, main.title=NULL, ...){ if(is.null(main.title)){ main.title <- paste('Boxplot of', substitute(y), "for GDS groups") } boxplot( data$y ~ data$x, main=main.title, ... ) } but why bother when you call the more robust, well tested and well documented boxplot() ? If you still want help, can you give a simple reproducible example? Regards, Adai Peter Flom wrote:> Hello > > I wrote a simple program to modify a boxplot: > > <<< > > gdsbox <- function(indvar){ > boxplot(indvar~gds3lev, > main = paste('Boxplot of', substitute(indvar), "for GDS groups"), > names = c('1', '3', '4, 5, 6')) > } > > > If I attach the dataframe gdsgraph, this works fine. However, I've been warned against attach. When I tried to run this program using 'with', it did not work. > > e.g. > > with(gdsgraph, gdsbox(BReT3T5T)) > > produced an error that gds3level was not found > > but if I try > > with(gdsgraph, boxplot(BReT3T5T~gds3lev)) > > it works fine. > > Similar problems occurred when I tried to use data > > What am I missing? > > Thanks > > Peter > > > > > Peter L. Flom, PhD > Brainscope, Inc. > 212 263 7863 (MTW) > 917 488 7176 (ThF) > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.