readline_time: Readline with waiting limit

Description Usage Arguments Value Note See Also

View source: R/Misc.R

Description

Normally, when using readline, execution is halted until input is supplied, and if it is not, the script hangs forever This functions waits a set time for the user to provide input, then returns control

Usage

1
readline_time(prompt, timeout = 3600, precision = 0.1)

Arguments

prompt

prompt for the user, similar to the one in readline(). Usually ends with a space (or newline).

timeout

timer in seconds. If set to 0, base::readline is used

precision

Polling interval in seconds.
Internally, every precision seconds the script checks if valid input is received. Smaller values increase responsiveness, but also increase CPU-usage.

Value

Input provided by the user, if provided in time (and finished with RETURN)
If the timer has expired, character(0), even if the user has started typing but has not yet pressed RETURN. If used in a non-interactive session, NULL, and if the timer has expired, character(0).
If timeout==0, the same as provided by readline()

Note

Unfortunately, does not work on all platforms/in all environments.
It is dependent on being able to read from file('stdin'), which is different from stdin(), see file for details. In particular, it does not work in RStudio for MacOS, or Rgui for Windows or MacOS. It does confirmed work on R run from terminal on MacOS 10.13.6
Problems manifest by file('stdin') not being connected to anything, i.e. no input is received, so this function always returns character(0), and any input typed is interpreted as a new command. To test, try with a small value of timeout first.

See Also

waitForKey for a function that simply waits for a keypress for a set time.


EmilBode/EmilMisc documentation built on Feb. 24, 2020, 4:11 p.m.