[Rd] SUGGESTION: Option to have menu() and select.list() to use stderr (or even "prompt" output)

Henrik Bengtsson hb at biostat.ucsf.edu
Wed Apr 30 01:46:26 CEST 2014


SEND TO STDERR:
Currently utils::menu() and utils::select.list() with graphics=FALSE
queries the user via the standard output (stdout).  I'd like to
suggest to do this via standard error (stderr) instead, or at least
have an option to choose which output stream.

If they would send to stderr, they could also be used in various
report generators that capture stdout and redirect to the generated
report.  As it is now, trying to use menu() and select.list() in such
frameworks will cause the menu options to appear in the report and not
be visible to the user in an interactive run.  This behavior affects
other querying functions such as setRepositories(graphics=FALSE) and
chooseCRANmirror(graphics=FALSE) so those cannot be used in report
generators either.  Before anyone argues that one shouldn't use
reports that prompts the user, there are several flavors of "report
generators/vignettes" that are natural to use also interactively.


SEND TO CONSOLE "PROMPT" OUTPUT:
Next, I've noticed that both the prompt of readline() and the
"Selection: " prompt of menu() are always displayed to the user, i.e.
they cannot be captured.  This is useful, because at least the
querying prompt can never be hidden from the user, which will always
see/understand that an input is needed.  Here is an example showing
this:

> zz <- file("all.Rout", open="wt"); sink(zz); sink(zz, type="message"); readline("Always here for you: "); sink(type="message"); sink()
Always here for you: ok
> cat(readLines("all.Rout"), sep="\n")
[1] "ok"
>

> zz <- file("all.Rout", open="wt"); sink(zz); sink(zz, type="message"); menu(letters[1:3], graphics=FALSE); sink(type="message"); sink()
Selection: 3
> cat(readLines("all.Rout"), sep="\n")

1: a
2: b
3: c

[1] 3
>

To me it looks like it would be even better if menu() and
select.list() would use this console "prompt" output for all its
output rather than stdout.  Second best is to use stderr.  I'm trying
to see when this is not wanted.  First, since readline(), menu() and
select.list() can only be used in interactive modes, I only assume
that the console prompt is always available so that should not be an
issue, or?  Second, it could be there are packages/software that rely
on being able to capture their output, but that would be solved by
having an argument output=c("stdout", "stderr", "prompt"), say.

Comments?

Henrik

PS. I've verified the above behavior with R 3.1.0 on Windows, OSX and Linux.



More information about the R-devel mailing list