[R] interactive menu in scripts

Henrique Dallazuanna wwwhsd at gmail.com
Sat Jan 26 21:14:32 CET 2008


On 26/01/2008, WCD <vydramail at quick.cz> wrote:
>
> Yes, the switch function works fine.
> To store generated values in some variable I just use:
>   switch(menu(c("Normal", "Uniform")), a<-rnorm(5), a<-runif(5))
>
> But why does not work this? (I know there is waste of variables, but in
> principle):
>   b<-numeric(10)
>   switch(menu(c("Normal", "Uniform")), a<-1, a<-2)
>   if(a==1) {b<-rnorm(10)} else {b<-runif(10)}
> When I run these commands from a script, the choice is offered after the
> condition is performed, so "b" stores ten zeros.
>

 b <- switch(menu(c("Normal", "Uniform")), rnorm(10), runif(10))


> Also readline() function seems to be what I am looking for. But I did not
> manage to use it for my reason;  this does not work too:
>   var<-numeric(5) #Is not necessary
>   rdf<-function() {
>   x<-readline("Enter 1 for normal, 2 for uniform distribution ")
>   if (x==1) {var<-rnorm(5)} else {var<-runif(5)}
>   }
>   rdf()

You need return the var object in the function
rdf<-function() {
   x<-readline("Enter 1 for normal, 2 for uniform distribution ")
   if (x==1) {var<-rnorm(5)} else {var<-runif(5)}
   return(var)
}
 rdf()


> Even this does not work (object "x" is not found):
>   rdf2<-function() {
>   x<-readline("enter your year of birth ")
>   }
>   rdf2()

The same that the previous, in this case return(x)

> Can you see what am I doing wrong?
>
> Anyway, I am sorry for my maybe bizzar explanations. I am quitr new to R and
> R forum.
> My aim is to write an R script, where the user can make some choices or set
> some parameters via the R console. Filip Kral.
>
>
> Henrique Dallazuanna wrote:
> >
> > Try like this:
> >
> > switch(menu(c("Normal", "Poisson")), rnorm(5), rpois(5, 3))
> >
> >
> > On 25/01/2008, WCD <vydramail at quick.cz> wrote:
> >>
> >> ______________________________________________
> >> R-help at 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.
> >>
> >
> >
> > --
> > Henrique Dallazuanna
> > Curitiba-Paraná-Brasil
> > 25° 25' 40" S 49° 16' 22" O
> >
> > ______________________________________________
> > R-help at 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/interactive-menu-in-scripts-tp15095049p15111894.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list