Skip to content

Commit 7618c2a

Browse files
prompt_input can accept input in an Rscript tty
1 parent 763b4fb commit 7618c2a

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

R/ui_functions.R

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ build_cli_args <- function(args=NULL, prompt="> ", instruct = "Enter arguments (
136136
}
137137

138138

139+
# function that allows line input in interactive session or Rscript with tty
140+
getline <- function(prompt = "") {
141+
if (interactive()) {
142+
out <- readline(prompt)
143+
} else if (base::isatty(stdin())) {
144+
cat(prompt)
145+
out <- tryCatch(
146+
scan(file = "stdin", what = "", nmax = 1, quiet = TRUE, sep = "\n", blank.lines.skip = FALSE),
147+
error = function(e) ""
148+
)
149+
if (length(out) == 0) return("")
150+
} else {
151+
stop("getline requires an interactive session or a tty")
152+
}
153+
154+
return(out)
155+
}
139156

140157
#' Obtain user input from the console
141158
#' @param prompt The character string to display before the user input prompt (e.g., `"Enter location"`).
@@ -164,8 +181,13 @@ build_cli_args <- function(args=NULL, prompt="> ", instruct = "Enter arguments (
164181
prompt_input <- function(prompt = "", prompt_eol=">", instruct = NULL, type = "character", lower = -Inf, upper = Inf,
165182
len = NULL, min.len=NULL, max.len=NULL, split = NULL, among = NULL, required = TRUE, uniq=FALSE, default = NULL) {
166183

167-
if (!interactive()) stop("prompt_input() requires an interactive session.")
168-
184+
# In non-interactive sessions launched via Rscript, interactive() returns
185+
# FALSE even though reading from stdin is possible. Allow such cases when the
186+
# standard input is a TTY.
187+
if (!interactive() && !base::isatty(stdin())) {
188+
stop("prompt_input() requires an interactive session.")
189+
}
190+
169191
if (is.null(prompt)) prompt <- ""
170192
if (is.null(prompt_eol)) prompt_eol <- ""
171193

@@ -267,7 +289,9 @@ prompt_input <- function(prompt = "", prompt_eol=">", instruct = NULL, type = "c
267289
# obtain user input
268290
res <- ""
269291
while (is.na(res[1L]) || res[1L] == "") {
270-
r <- readline(prompt)
292+
#r <- readline(prompt)
293+
r <- getline(prompt)
294+
271295
if (!is.null(split) && nzchar(r)) r <- strsplit(r, split, perl = TRUE)[[1]]
272296

273297
if (!is.null(default) && r[1L] == "") {
File renamed without changes.

man/butterworth_filter_4d.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)