Interacting with the user: Programming R in order to ask questions to the user -
i trying create model in r asks questions user, , user answer them.
i have create 2 vector. in 1 there numbers , in other same numbers written in character:
a <- 1:10 words <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten") > [1] 1 2 3 4 5 6 7 8 9 10 > words [1] "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" i create script that, each item in a, automatically asks question like: 1? 2?, , on. user answer, in order go next number, needs answer one, two , on.
if user makes mistake program needs able remember mistake , ask question again after n questions. think should use while command , readline not sure that.
this should fine:
test <- windialogstring("please insert value", default="") then type, say, "hello".
> test [1] "hello" a simple example desired loop be:
a <- "" test <- 1l while(a!=test){ <- windialogstring(paste0(test,"?"), default="") } edit: sorry, misunderstood question before
a <- 1l:10l test <- c("one","two", "three", "four", "five", "six", "seven", "eight", "nine", "ten") res <- "" for(i in seq_along(a)){ if(is.null(res)) break while(res!=test[i]){ res <- windialogstring(paste0(a[i],"?"), default="") if(is.null(res)) break } } the above version stops execution when user press cancel. instead if want user finish exercise following
a <- 1l:10l test <- c("one","two", "three", "four", "five", "six", "seven", "eight", "nine", "ten") res <- null for(i in seq_along(a)) while(is.null(res) || res!=test[i]) res <- windialogstring(paste0(a[i],"?"), default="") keeps asking solution (for number 1 10) until respond correctly, , clicking cancel won't have effect.
Comments
Post a Comment