Using askYesNo in a repeat loop in R

Viewed 34

I am trying to write a piece of code where it will show you a dataframe then have a pop-up box that asks you if you are happy with the values there. I have used askYesNo but I'm not attached to that.

I want to make it so that if you pick yes it will keep repeating the code with the pop-up box until you pick no.

My problem is that it will repeat the code but isn't repeating the pop-up box as well so there is nowhere for it to break.

If it's possible I'd also like the code to look basic. I struggle to understand big chunks of code. The less lines the better but I'll take what I can get.

Here is the code I have been trying (as this is a test some of this code is messy and has unrepresentative names. It is related to a far bigger bit of code and I am trying to make it easier in the testing station):

rasch_test <- data.frame(a=rnorm(3), b=rnorm(3), c=rnorm(3))
params_test <- rasch_test
item_parameters_test <- params_test[,-c(ncol(params_test))]


if(any(abs(item_parameters_test)>0.5)) {
  html_print(pre(paste0(capture.output(print(item_parameters_test)), collapse="\n")))}    

CHOICE<- askYesNo("ARE YOU WORRIED ABOUT THE RANGE OF THESE VALUES?") 

if (CHOICE == "TRUE"){
  
  repeat { 
    rasch_test <- data.frame(a=rnorm(3), b=rnorm(3), c=rnorm(3))
    params_test <- rasch_test
    item_parameters_test <- params_test[,-c(ncol(params_test))]
    html_print(pre(paste0(capture.output(print(item_parameters_test)), collapse="\n")))} 
  CHOICE<- askYesNo("ARE YOU WORRIED ABOUT THE RANGE OF THESE VALUES?")
  
  if (CHOICE == FALSE){
    break
  }
}

if (CHOICE == "FALSE"){
  print("you chose no")
}
1 Answers

You can place the askYesNo call in the while() condition like this:

library(htmltools)
new_table <- function() {
  rasch_test <- data.frame(a=rnorm(3), b=rnorm(3), c=rnorm(3))
  params_test <- rasch_test
  params_test[,-c(ncol(params_test))]
}

tbl = new_table()
if(any(abs(tbl)>0.5)) {
  html_print(pre(paste0(capture.output(print(tbl)), collapse="\n")))}    

while(askYesNo("ARE YOU WORRIED ABOUT THE RANGE OF THESE VALUES?")) {
  html_print(pre(paste0(capture.output(print(new_table())), collapse="\n")))} 
}
Related