[R] apply block of if statements with menu function

PIKAL Petr petr.pikal at precheza.cz
Tue Sep 16 12:50:30 CEST 2014


Hi

> 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?

Instead of writing functions which you are unable to debug and resolve look what elements of your function returns.

Here is what your first line does

> menu1<-switch(menu(c(1,2,3,4),graphics=FALSE,title="select something"))
select something

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

Selection: 3
> menu1
NULL

Regardless of selection menu1 stays NULL, what is probably not what you want.

> switch(menu(c("List letters", "List LETTERS")) + 1,
>       cat("Nothing done\n"), letters, LETTERS)
>
> why is the result changed if ' + 1,' removed?

Because +1 belongs to switch not to menu. You can translate above to:

open menu with 2 items
select one of those 2 items  or press 0
return 0, 1 or 2 based on your choice
add 1 to returned value to enable **switch** to perform first, second or third item
perform desired item e.g print Nothing done, letters or LETTERS.

Nothing more, nothing less.

so in your case it shall be

menu1 <- menu(c(1,2,3,4),graphics=FALSE,title='select something')
if (menu1 == 1) ...

or
menu1 <- menu1+1

switch (menu1, "Nothing selected",
{do something1},
{do something2},
{do something3},
{do something4})

Regards
Petr

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of rl at openmailbox.org
> Sent: Tuesday, September 16, 2014 11:10 AM
> To: David L Carlson
> Cc: r-help at r-project.org
> Subject: Re: [R] apply block of if statements with menu function
>
> 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?
>
> ______________________________________________
> 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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list