sdgaw erzswer
sdgaw erzswer

Reputation: 2382

Using user input to plot data in R

I am trying to build a program, in which a user would enter the desired part of data to plot and the R program would plot it. For example:

n<- function(){
readline(prompt="enter x value to plot")
}
m<- function{
readline(prompt="enter y value to plot")
}
attach(dataset)
plot(unquote(n()), unquote(m()), main="Scatterplot Example", pch=20)

yet this doesnt work, the plot function doesnt recognise the m() and n()? Am I missing somehing?

Thank you.

Upvotes: 1

Views: 614

Answers (1)

Roland
Roland

Reputation: 132696

Don't use attach.

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 Example", pch=20)      
}

if(interactive()) plotfun(dat=iris)
#enter x value to plot: Sepal.Length
#enter y value to plot: Sepal.Width

Upvotes: 2

Related Questions