Bernzweig, Bruce (Consultant)
2007-Jun-25 14:45 UTC
[R] R behaviour related to user input (readline()) and run selection
When I run the below section of code I get the following error: Error in if (co == "A" || co[1] == "O") { : missing value where TRUE/FALSE needed When I run the code in two parts where I first get the user's input then afterwards run the if/else section, there is no problem. Is there a way to stop the "run selection" process until the user has input a value? ------------------------------------------------------------------------ ----- calc_option <- function(){ msg <- cat("Please select an option:\n"," 'O'ne or 'A'll': ") co <- readline(msg) switch(co, O = "O", o = "O", A = "A", a = "A" ) } co <- calc_option() if (co == "A" || co[1] == "O") { print(paste("calc_option = ", co)) } else { print("calc_option is not acceptable") } Thanks, - Bruce -------------- next part -------------- ********************************************************************** Please be aware that, notwithstanding the fact that the pers...{{dropped}}
jim holtman
2007-Jun-25 16:35 UTC
[R] R behaviour related to user input (readline()) and run selection
First of all you don't appear to be returning anything from your function, so 'co' is NULL and your 'if' statement fails. You were also probably cut/paste the code in, and the readline was reading from the statements you pasted. Try sourcing in the code. On 6/25/07, Bernzweig, Bruce (Consultant) <bbernzwe@bear.com> wrote:> > When I run the below section of code I get the following error: > > > > Error in if (co == "A" || co[1] == "O") { : > > missing value where TRUE/FALSE needed > > > > When I run the code in two parts where I first get the user's input > > then afterwards run the if/else section, there is no problem. > > > > Is there a way to stop the "run selection" process until the user > > has input a value? > > > > ------------------------------------------------------------------------ > ----- > > > > calc_option <- function(){ > > msg <- cat("Please select an option:\n"," 'O'ne or 'A'll': > ") > > co <- readline(msg) > > > > switch(co, > > O = "O", o = "O", > > A = "A", a = "A" > > ) > > } > > > > co <- calc_option() > > > > if (co == "A" || co[1] == "O") { > > print(paste("calc_option = ", co)) > > } else { > > print("calc_option is not acceptable") > > } > > > > > > Thanks, > > > > - Bruce > > > > > ********************************************************************** > Please be aware that, notwithstanding the fact that the pers...{{dropped}} > > > ______________________________________________ > R-help@stat.math.ethz.ch 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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]