Hello R list, I have a little problem with splom. I'd like to wrap it in a function, for example: multi.scatterplot <- function(data,groups,cols,colors) { splom(~data[,cols], groups = as.symbol(groups), data = data, panel = panel.superpose, col=colors) } and then call it like in multi.scatterplot(iris,"Species",1:4,c("green","blue","red")) but the problem is: Error in form$groups[form$subscr] : object is not subsettable if I use groups = groups instead of groups = as.symbol(groups) shomthing is plotted, but not the correct scatterplot. I think the problem is that I don't cast the 'groups' variable to the correct type. Besides as.symbol() I tried also as.expression(), because ?xyplot says "groups: a variable or expression to be evaluated in the data frame specified by 'data'". What is the correct type? What as.* should I use? thank you, regards, Roberto
Roberto: You need to do what ?xyplot says. as.symbol(groups) is not a variable and is certainly not subsettable. If you have a variable named "groups" in your data.frame , then ... groups = groups gives the grouping according to the levels of that variable. If you do not, then it may be picking up a variable named "groups" from somewhere else, probably your global workspace, which may be producihg your unexpected results. Or perhaps there is a variable named "groups" in you data frame which is not what you think it is. Have you checked? In any case, please examine or run the examples in ?xyplot, especially those that use the group = argument. One note: I do grant you that the phrase "variable or expression" may be confusing in this context. But do note that ?as.expression explicitly says: " 'Expression' here is not being used in its colloquial sense, that of mathematical expressions. Those are calls (see call) in R, and an R expression vector is a list of calls etc, typically as returned by parse." What is meant by the phrase in the xyplot help is "expression" in its colloquial sense of a math (or more generally, any R) expression, not a formal expression object, which is what the cast as.expression() gives. Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 650-467-7374 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Roberto Perdisci Sent: Wednesday, February 14, 2007 1:05 PM To: r-help at stat.math.ethz.ch Subject: [R] Putting splom in a function Hello R list, I have a little problem with splom. I'd like to wrap it in a function, for example: multi.scatterplot <- function(data,groups,cols,colors) { splom(~data[,cols], groups = as.symbol(groups), data = data, panel = panel.superpose, col=colors) } and then call it like in multi.scatterplot(iris,"Species",1:4,c("green","blue","red")) but the problem is: Error in form$groups[form$subscr] : object is not subsettable if I use groups = groups instead of groups = as.symbol(groups) shomthing is plotted, but not the correct scatterplot. I think the problem is that I don't cast the 'groups' variable to the correct type. Besides as.symbol() I tried also as.expression(), because ?xyplot says "groups: a variable or expression to be evaluated in the data frame specified by 'data'". What is the correct type? What as.* should I use? thank you, regards, Roberto ______________________________________________ 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.
On 2/14/07, Roberto Perdisci <roberto.perdisci at gmail.com> wrote:> Hello R list, > I have a little problem with splom. I'd like to wrap it in a > function, for example: > > multi.scatterplot <- function(data,groups,cols,colors) { > splom(~data[,cols], groups = as.symbol(groups), data = data, panel > = panel.superpose, col=colors) > } > > and then call it like in > > multi.scatterplot(iris,"Species",1:4,c("green","blue","red")) > > but the problem is: > Error in form$groups[form$subscr] : object is not subsettableNon-standard evaluation is a pain generally. See, e.g., lattice:::stripplot.formula for a possible solution that seems to work.> if I use > groups = groups > instead of > groups = as.symbol(groups) > > shomthing is plotted, but not the correct scatterplot.Try groups = eval(as.name(groups)) Deepayan> I think the problem is that I don't cast the 'groups' variable to the > correct type. Besides as.symbol() I tried also as.expression(), > because ?xyplot says "groups: a variable or expression to be evaluated > in the data frame specified by 'data'". > What is the correct type? What as.* should I use? > > thank you, > regards, > Roberto > > ______________________________________________ > 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. >
If you call splom with do.call then your solution should work: do.call("splom", list(~data[cols], groups = as.symbol(groups), data = data, panel = panel.superpose, col = colors)) or use as.name where as.symbol is which also works. On 2/14/07, Roberto Perdisci <roberto.perdisci at gmail.com> wrote:> Hello R list, > I have a little problem with splom. I'd like to wrap it in a > function, for example: > > multi.scatterplot <- function(data,groups,cols,colors) { > splom(~data[,cols], groups = as.symbol(groups), data = data, panel > = panel.superpose, col=colors) > } > > and then call it like in > > multi.scatterplot(iris,"Species",1:4,c("green","blue","red")) > > but the problem is: > Error in form$groups[form$subscr] : object is not subsettable > > if I use > groups = groups > instead of > groups = as.symbol(groups) > > shomthing is plotted, but not the correct scatterplot. > > I think the problem is that I don't cast the 'groups' variable to the > correct type. Besides as.symbol() I tried also as.expression(), > because ?xyplot says "groups: a variable or expression to be evaluated > in the data frame specified by 'data'". > What is the correct type? What as.* should I use? > > thank you, > regards, > Roberto > > ______________________________________________ > 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. >