On Aug 10, 2012, at 6:23 PM, Elaine Jones wrote:
I am running R version 2.15.1 in Windows XP
I am having problems with a function I'm trying to create to:
1. subset a data.frame based on function arguments (colname &
parmname)
2. rename the PARMVALUE column in the data.frame based on function
argument (xvar)
3. generate charts
plotvar <- function(parentdf,colname, parmname,xvar,yvar ){
subdf <- parentdf[substr(colname,1,nchar(parmname)) == parmname,]
names(subdf) <- sub('PARMVALUE',xvar, names(subdf))
xvarcol <- paste("subdf","$",xvar,sep="")
yvarcol <- paste("subdf","$",yvar,sep="")
hist(eval(parse(text = xvarcol)), xlab = xvar, main = paste
("Distribution of ",xvar,sep=""))
boxplot(eval(parse(text = yvar))~HMA, ylab=yvar, xlab="HMA",
data=subdf)
plot(eval(parse(text = yvarcol))~eval(parse(text = xvarcol)),
main =
paste(yvar," by ",xvar, sep=""), xlab = xvar, ylab = yvar)
scatterplot(eval(parse(text = yvar))~eval(parse(text = xvar)),
boxplots='xy', data=subdf, main = "Testcase_ID=142")
}
When I execute the function:
plotvar(mydf,mydf$PARMNAME, "C_2TAMP","Amp_2T","C1")
everything seems to work until the scatterplot statement. It posts
this
error:
Error in parse(text = yvar) : object 'yvar' not found
Generally you will get better success with the use of as.formula()
than you will from eval(parse(text=<stuff>)). There's even a fortune
about it.
fortunes::fortune("parse")
> fortunes::fortune("parse") # actually this is only one of several
possible results
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
However, when I insert browser() statement before the scatterplot
statement:
Browse[1]> ls()
[1] "colname" "parentdf" "parmname" "subdf" "xvar" "xvarcol"
"yvar" "yvarcol"
yvar is there, and the value is as expected
Browse[1]> yvar
[1] "C1"
Is this a quirk peculiar to scatterplot? (Note that boxplot which has
similar usage worked.)
I would appreciate any suggestions for how to resolve this,
including a
different (better) approach. mydf can have many different parameters
(parmnames), so I am trying to come up with a generalized function
to plot
the data.
You probably should offer a data argument to scatterplot, and not use
the "$" construction. Instead construct the formula using the desired
column names so it can be evaluated in the context of hte data
argument. You should also include code to check to see if the unnamed
package with the scatterplot function is loaded:
> ?scatterplot
No documentation for ‘scatterplot’ in specified packages and libraries:
you could try ‘??scatterplot’
(Yes, I know it's probably the one from 'car'.)
Here's a minimal example:
data(Prestige, package=car)
scplot <- function(xvar, yvar,dvar) { form <-
as.formula( paste(xvar, yvar, sep="~") )
car::scatterplot( form, data = dvar) }
scplot("prestige", "income", Prestige)
As my tai chi teacher likes to say ... "piece of cake".
--
David Winsemius, MD
Alameda, CA, USA
______________________________________________
R-help@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.