[Rd] Option to preserve whitespace in readline()

Viechtbauer, Wolfgang (NP) wo||g@ng@v|echtb@uer @end|ng |rom m@@@tr|chtun|ver@|ty@n|
Mon Sep 7 12:38:15 CEST 2026


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?

Best,
Wolfgang



More information about the R-devel mailing list