[R] apply block of if statements with menu function

rl at openmailbox.org rl at openmailbox.org
Tue Sep 16 11:09:54 CEST 2014


On 2014-09-15 14:22, David L Carlson wrote:
> I think switch() should work for you here, but it is not clear how
> much flexibility you are trying to have (different tests based on the
> first response; different tests based on first, then second response;
> different tests based on each successive response).
> 

The help page for 'menu' shows:

switch(menu(c("List letters", "List LETTERS")) + 1,
	cat("Nothing done\n"), letters, LETTERS)

why is the result changed if ' + 1,' removed?

The help page for switch states that "arguments" are required, but this 
conflicts with the syntax for the function 'menu':

> testswitch<-function (testfunctionname) {
+ menu1<-switch(menu(c(1,2,3,4),graphics=FALSE,title='select 
something'))
+ if (menu1==1)
+ menu1a<-menu(c('1a','2a','3a','4a'),graphics=FALSE,title='select 
something else')
+ if (menu1a==1)
+ object1<-seq(1:10)
+ object2<-seq(20:30)
+ menu2a<-menu(c('5a','6a','7a','8a'),graphics=FALSE,title='select 
something else again')
+ if (menu2a==2)
+ object3<-'3y'
+ finalobject<<-object3
+ if (menu1==2)
+ menu2x<-menu(c('5a','15a','20a'),graphics=FALSE,title='select 
something else')
+ if (menu2x==1)
+ object22<-seq(10:100)
+ object22<-seq(2:30)
+ object33<-rnorm(10)
+ menu2y<-menu(c('5a','6a','7a','8a'),graphics=FALSE,title='select 
something else again')
+ if (menu2y==2)
+ object3<-'3y'
+ finalobject<<-object3
+ }
> testswitch(testfunctionname)
select something

1: 1
2: 2
3: 3
4: 4

Selection: 1
Error in if (menu1 == 1) menu1a <- menu(c("1a", "2a", "3a", "4a"), 
graphics = FALSE,  :
	argument is of length zero

How to correct this error please, for this zero length argument?



More information about the R-help mailing list