[Rd] Option to preserve whitespace in readline()
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Tue Sep 8 11:37:05 CEST 2026
>>>>> Viechtbauer, Wolfgang \(NP\) via R-devel
>>>>> on Mon, 7 Sep 2026 10:38:15 +0000 writes:
> Dear R-Core Team,
> I would like to suggest adding an argument to readline() that allows leading and trailing whitespace to be preserved.
> Currently, readline() strips leading and trailing spaces and tabs. For example,
> x <- readline("Input: ")
> cannot be used when such whitespace is part of the intended input. As far as I can tell, there is currently no way to disable this behavior.
> Would there be interest in adding an argument along the lines of:
> readline(prompt = "", trim = TRUE)
> where the default would retain the current behavior, while trim = FALSE would return the input unchanged (apart from the line ending)?
> Looking at do_readln() in src/main/scan.c, this appears to require only a fairly small change, since the removal of leading and trailing spaces/tabs is done explicitly there. In particular, when trim = FALSE, the initial loop that skips spaces/tabs and the subsequent removal of spaces/tabs at the end could simply be bypassed. Looking at the sources, do_readln() appears to be used exclusively for readline(), so this seems to be a fairly localized change.
> The change would also be fully backwards compatible with trim = TRUE as the default. It would also be useful because reproducing this behavior in a package is not straightforward: readLines(stdin(), n = 1) preserves whitespace but does not necessarily have the same console/frontend behavior as readline().
> Would such an addition be considered?
I haven't seen the use myself, but as you see the usefulness of
this, and I agree the change is relatively small, I'd consider.
As it is really your wish, I'm asking you to use R's bugzilla
and provide a patch for at least these 4 files
src/library/base/R/New-Internal.R
src/library/base/man/readline.Rd
src/main/names.c
src/main/scan.c
(yes, names.c needs a small change, too ...)
Best regards,
Martin
> Best,
> Wolfgang
More information about the R-devel
mailing list