0

I have a function something like this:

    annotation <- function(){
    start.time <- Sys.time()
    cat("Please add a note to be concatenated to the output folder's name. Press \"Enter\" if you don't need.")
    note <- readline()
    if (note == ""){
      return(note)
    }
    else{
      note <- paste0("_", note)
    }
    end.time <- Sys.time()
    if (end.time - start.time <= 5) {
      return(note)
    }else{
      note <- ""
      return(note)  
      }

    }

However, this is not what I excepted, because what I want is: if there is no action within 5 second, the returned note will be "", otherwise, the returned note is what you input.

I know it lacks certain function to count down, but my R's ability is the bottleneck.

Also, if would be nice to show the time count down in the terminal once you execute the function.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Negrito
  • 220
  • 1
  • 9
  • [`R.utils::withTimeout`](https://github.com/HenrikBengtsson/R.utils/blob/develop/R/withTimeout.R), which ultimately uses base R function [`setTimeLimit`](https://stat.ethz.ch/R-manual/R-patched/library/base/html/setTimeLimit.html). – r2evans Jun 04 '20 at 03:11
  • and https://stackoverflow.com/a/53018594/3358272 – r2evans Jun 04 '20 at 03:13
  • But neither of those work with `readline`: https://stackoverflow.com/q/37317212/3358272 – r2evans Jun 04 '20 at 03:19
  • Sorry, I think that means that not only is this a dupe (not always bad news), but it's a dupe with a "not possible" answer. If you're looking for time-sensitive user input, perhaps a shiny app is an option. – r2evans Jun 04 '20 at 03:21
  • So you mean that maybe the only way (in R terminal ) is to write a C function and implement it in R? – Negrito Jun 04 '20 at 04:53
  • If you want to re-implement the internals of `readline` and see if that works with the `setTimeLimit` R function, then that might be possible, but I don't know. The limitation is (I'm guessing) based on user-input, and I have seen no options that circumvent that (when user input is required). – r2evans Jun 04 '20 at 05:14
  • 1
    Ok, I see what you mean, the `setTimeLimit` approach still doesn't reach my expectation while my C language ability is even worse than R... But thanks for sharing. – Negrito Jun 04 '20 at 05:43

0 Answers0