0

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.

2
  • Where does the Boxplot function come from? Commented May 28, 2014 at 5:42
  • @MrFlick car::Boxplot Commented May 28, 2014 at 5:50

2 Answers 2

1

What is infile?

plot2 <- function(){
  a <- readline(prompt = "which variable")
  barplot(table(a))
}
Sign up to request clarification or add additional context in comments.

2 Comments

infile is for example a CSV file in which there is the variable I would want to plot
I've tried this script you provided and it doesn't work, it plots something, but it is of no use. (everything colored in barplot, just a line in Boxplot.
0

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.

2 Comments

Darn, i knew something was wrong.There is one more thing, when I try this while having my dataset imported in R as infile, it says there is infile missing? how is this possible? I am doing my work in Rcmdr, is this important? edit: It works perfectly now. Thank you kind sir.
@user3523464 It's unclear from your description what the error might me. Is it the variable infile is missing or is the problem when you are trying to read the data that it's not there? Make sure you have the right path. But if you have a new problem, it's best to ask a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.