Reputation: 2382
I've been dealing with user input for various graphs. My main aim was to ask the user for an input and then parse this to a plotting function. I managed to do this for scatterplot, but not boxplot and barplot. This is my working example:
n<- function(){
readline(prompt="enter x value to plot: ")
}
m<- function(){
readline(prompt="enter y value to plot: ")
}
plotfun <- function(dat) {
colx <- n()
coly <- m()
plot(dat[,colx], dat[,coly], main="Scatterplot", pch=20,xlab=[,colx] )
}
But when I try something similar with boxplot for example:
plot2<-function(infile){
a<-readline(prompt="which variable")
barplot(table(infile$a))
}
or
a<-readline(prompt="enter...")
Boxplot( ~ a, data=infile, id.method="y")
It doesn't work
Errors were something like: can't find the object, argument "infile" is missing, with no default.
Upvotes: 0
Views: 198
Reputation: 206197
You cannot use "$" with character variable names. You must do the subsetting with [
as you did in the other cases
plot2<-function(infile){
a<-readline(prompt="which variable")
barplot(table(infile[,a]))
}
If your Boxplot
function is the one from car
, then
a<-readline(prompt="enter...")
Boxplot(infile[,a], labels=rownames(infile), id.method="y")
Is the variable friendly equivalent. You can't use character variables in formulas either. They are taken as literal values.
Upvotes: 0
Reputation: 9344
What is infile
?
plot2 <- function(){
a <- readline(prompt = "which variable")
barplot(table(a))
}
Upvotes: 1