[R] apply block of if statements with menu function

David L Carlson dcarlson at tamu.edu
Mon Sep 15 16:22:19 CEST 2014


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). 

?switch

For the second question just index the return value:

> let <- letters[1:4]
> let[menu(let)]

1: a
2: b
3: c
4: d

Selection: 3
[1] "c"

Or a bit more polished:

> cat("Choice: ", let[menu(let)], "\n")

1: a
2: b
3: c
4: d

Selection: 4
Choice:  d

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----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: Monday, September 15, 2014 3:53 AM
To: r-help at r-project.org
Subject: [R] apply block of if statements with menu function

Subscribers,

apply block of if statements with menu function
Subscribers,

For a menu:

menu(c('a','b','c','d'))

How to create a function that will apply to specific menu choice 
objects? For example:

object1<-function (menuifchoices) {
menu1<-menu(c('a','b','c','d'))
	if (menu1==1)
	...
	menu1a<-menu...
		if (menu1a==1)
		...
	menu2a<-menu...
		if (menu2a==1)
		...
menu2
	<-menu(c('a','b','c','d'))
	if (menu1==2)
	...
}

The request action is that a user can select a menu option that will 
activate a series of "multiple choice" questions, results in "menu1" 
being activated without menu2 being activated. If someone could direct 
to the relevant terminology, thank you.

Separate question; for a menu:

menu(c('a','b','c','d'))

1: a
2: b
3: c
4: d

Selection: 1
[1] 1

is it possible to change behaviour so that result of the selection is 
not the integer, but the original menu choice:

Selection: 1
[1] a

______________________________________________
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.



More information about the R-help mailing list