From 653ef9394fe63731a7f6f08c0bef83e1553229e3 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 10 Jul 2024 17:19:11 +0800 Subject: [PATCH 1/3] use custom (extended) timeout for showDialog --- DESCRIPTION | 2 +- R/dialogs.R | 27 +++++++++++++++++++-------- R/remote.R | 13 +++++++------ man/showDialog.Rd | 17 +++++++++++------ 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 261d2f3..7b8cdd1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,7 @@ URL: https://rstudio.github.io/rstudioapi/, https://github.com/rstudio/rstudioapi BugReports: https://github.com/rstudio/rstudioapi/issues Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Suggests: testthat, knitr, diff --git a/R/dialogs.R b/R/dialogs.R index 333c4d4..bee7ba6 100644 --- a/R/dialogs.R +++ b/R/dialogs.R @@ -2,17 +2,28 @@ #' #' Shows a dialog box with a given title and contents. #' -#' \preformatted{ showDialog("A dialog", "Showing bold text in the -#' message.") } -#' #' @param title The title to display in the dialog box. +#' #' @param message A character vector with the contents to display in the main -#' dialog area. Contents can contain the following HTML tags: "p", "em", -#' "strong", "b" and "i". -#' @param url An optional url to display under the \code{message}. +#' dialog area. Contents can contain the following HTML tags: "p", "em", +#' "strong", "b" and "i". +#' +#' @param url An optional URL to display under the \code{message}. +#' +#' @param timeout An optional timeout (in seconds). When set, if the user takes +#' longer than this timeout to provide a response, the request will be aborted. +#' #' @note The \code{showDialog} function was added in version 1.1.67 of RStudio. -#' @export showDialog -showDialog <- function(title, message, url = "") { +#' +#' @examples +#' if (rstudioapi::isAvailable()) { +#' rstudioapi::showDialog("Example Dialog", "This is an example dialog.") +#' } +#' +#' @export +showDialog <- function(title, message, url = "", timeout = 60) { + opts <- options(rstudioapi.remote.timeout = timeout) + on.exit(options(opts), add = TRUE) callFun("showDialog", title, message, url) } diff --git a/R/remote.R b/R/remote.R index f0d8f64..af5ad60 100644 --- a/R/remote.R +++ b/R/remote.R @@ -39,7 +39,7 @@ callRemote <- function(call, frame) { # check for active request / response requestFile <- Sys.getenv("RSTUDIOAPI_IPC_REQUESTS_FILE", unset = NA) responseFile <- Sys.getenv("RSTUDIOAPI_IPC_RESPONSE_FILE", unset = NA) - secret <- Sys.getenv("RSTUDIOAPI_IPC_SHARED_SECRET", unset = NA) + secret <- Sys.getenv("RSTUDIOAPI_IPC_SHARED_SECRET", unset = NA) if (is.na(requestFile) || is.na(responseFile) || is.na(secret)) stop("internal error: callRemote() called without remote connection") @@ -50,17 +50,17 @@ callRemote <- function(call, frame) { attr(call, "srcref") <- NULL # ensure rstudioapi functions get appropriate prefix - if (is.name(call[[1L]])) { - call_fun <- call("::", as.name("rstudioapi"), call[[1L]]) + callFun <- if (is.name(call[[1L]])) { + call("::", as.name("rstudioapi"), call[[1L]]) } else { - call_fun <- call[[1L]] + call[[1L]] } # ensure arguments are evaluated before sending request call[[1L]] <- quote(base::list) args <- eval(call, envir = frame) - call <- as.call(c(call_fun, args)) + call <- as.call(c(callFun, args)) # write to tempfile and rename, to ensure atomicity data <- list(secret = secret, call = call) @@ -72,6 +72,7 @@ callRemote <- function(call, frame) { # in theory we'd just do a blocking read but there isn't really a good # way to do this in a cross-platform way without additional dependencies now <- Sys.time() + timeout <- getOption("rstudioapi.remote.timeout", default = 10) repeat { # check for response @@ -80,7 +81,7 @@ callRemote <- function(call, frame) { # check for lack of response diff <- difftime(Sys.time(), now, units = "secs") - if (diff > 10) + if (diff > timeout) stop("RStudio did not respond to rstudioapi IPC request") # wait a bit diff --git a/man/showDialog.Rd b/man/showDialog.Rd index 3e18d85..04728c2 100644 --- a/man/showDialog.Rd +++ b/man/showDialog.Rd @@ -4,7 +4,7 @@ \alias{showDialog} \title{Show Dialog Box} \usage{ -showDialog(title, message, url = "") +showDialog(title, message, url = "", timeout = 60) } \arguments{ \item{title}{The title to display in the dialog box.} @@ -13,15 +13,20 @@ showDialog(title, message, url = "") dialog area. Contents can contain the following HTML tags: "p", "em", "strong", "b" and "i".} -\item{url}{An optional url to display under the \code{message}.} +\item{url}{An optional URL to display under the \code{message}.} + +\item{timeout}{An optional timeout (in seconds). When set, if the user takes +longer than this timeout to provide a response, the request will be aborted.} } \description{ Shows a dialog box with a given title and contents. } -\details{ -\preformatted{ showDialog("A dialog", "Showing bold text in the -message.") } -} \note{ The \code{showDialog} function was added in version 1.1.67 of RStudio. } +\examples{ +if (rstudioapi::isAvailable()) { + rstudioapi::showDialog("Example Dialog", "This is an example dialog.") +} + +} From c142cf7399ba95fd29f139250df8571274dfe6c0 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 10 Jul 2024 17:22:07 +0800 Subject: [PATCH 2/3] also allow timeout for other dialog fns --- R/dialogs.R | 84 +++++++++++++++++++++++++++++------------------- rstudioapi.Rproj | 1 + 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/R/dialogs.R b/R/dialogs.R index bee7ba6..1019095 100644 --- a/R/dialogs.R +++ b/R/dialogs.R @@ -1,25 +1,25 @@ #' Show Dialog Box -#' +#' #' Shows a dialog box with a given title and contents. -#' +#' #' @param title The title to display in the dialog box. -#' +#' #' @param message A character vector with the contents to display in the main #' dialog area. Contents can contain the following HTML tags: "p", "em", #' "strong", "b" and "i". -#' +#' #' @param url An optional URL to display under the \code{message}. -#' -#' @param timeout An optional timeout (in seconds). When set, if the user takes +#' +#' @param timeout A timeout (in seconds). When set, if the user takes #' longer than this timeout to provide a response, the request will be aborted. -#' +#' #' @note The \code{showDialog} function was added in version 1.1.67 of RStudio. -#' +#' #' @examples #' if (rstudioapi::isAvailable()) { #' rstudioapi::showDialog("Example Dialog", "This is an example dialog.") #' } -#' +#' #' @export showDialog <- function(title, message, url = "", timeout = 60) { opts <- options(rstudioapi.remote.timeout = timeout) @@ -30,14 +30,14 @@ showDialog <- function(title, message, url = "", timeout = 60) { #' Updates a Dialog Box -#' +#' #' Updates specific properties from the current dialog box. -#' +#' #' Currently, the only dialog with support for this action is the New #' Connection dialog in which the code preview can be updated through this API. -#' +#' #' \preformatted{ updateDialog(code = "con <- NULL") } -#' +#' #' @param ... Named parameters and values to update a dialog box. #' @note The \code{updateDialog} function was added in version 1.1.67 of #' RStudio. @@ -49,39 +49,57 @@ updateDialog <- function(...) { #' Show Prompt Dialog Box -#' +#' #' Shows a dialog box with a prompt field. -#' -#' +#' +#' #' @param title The title to display in the dialog box. +#' #' @param message A character vector with the contents to display in the main -#' dialog area. +#' dialog area. +#' #' @param default An optional character vector that fills the prompt field with -#' a default value. +#' a default value. +#' +#' @param timeout A timeout (in seconds). When set, if the user takes +#' longer than this timeout to provide a response, the request will be aborted. +#' #' @note The \code{showPrompt} function was added in version 1.1.67 of RStudio. +#' #' @export showPrompt -showPrompt <- function(title, message, default = NULL) { +showPrompt <- function(title, message, default = NULL, timeout = 60) { + opts <- options(rstudioapi.remote.timeout = timeout) + on.exit(options(opts), add = TRUE) callFun("showPrompt", title, message, default) } #' Show Question Dialog Box -#' +#' #' Shows a dialog box asking a question. -#' -#' +#' #' @param title The title to display in the dialog box. +#' #' @param message A character vector with the contents to display in the main -#' dialog area. +#' dialog area. +#' #' @param ok And optional character vector that overrides the caption for the -#' OK button. +#' OK button. +#' #' @param cancel An optional character vector that overrides the caption for -#' the Cancel button. +#' the Cancel button. +#' +#' @param timeout A timeout (in seconds). When set, if the user takes +#' longer than this timeout to provide a response, the request will be aborted. +#' #' @note The \code{showQuestion} function was added in version 1.1.67 of -#' RStudio. +#' RStudio. +#' #' @export showQuestion -showQuestion <- function(title, message, ok = NULL, cancel = NULL) { +showQuestion <- function(title, message, ok = NULL, cancel = NULL, timeout = 60) { + opts <- options(rstudioapi.remote.timeout = timeout) + on.exit(options(opts), add = TRUE) callFun("showQuestion", title, message, ok, cancel) } @@ -91,18 +109,18 @@ showQuestion <- function(title, message, ok = NULL, cancel = NULL) { #' #' Request a secret from the user. If the `keyring` package is installed, it #' will be used to cache requested secrets. -#' -#' +#' +#' #' @param name The name of the secret. -#' +#' #' @param message A character vector with the contents to display in the main #' dialog area. -#' +#' #' @param title The title to display in the dialog box. -#' +#' #' @note The \code{askForSecret} function was added in version 1.1.419 of #' RStudio. -#' +#' #' @export askForSecret <- function( name, diff --git a/rstudioapi.Rproj b/rstudioapi.Rproj index ab963f2..e5c3f7c 100644 --- a/rstudioapi.Rproj +++ b/rstudioapi.Rproj @@ -13,6 +13,7 @@ RnwWeave: Sweave LaTeX: pdfLaTeX AutoAppendNewline: Yes +StripTrailingWhitespace: Yes BuildType: Package PackageUseDevtools: Yes From e63d82ce65acded508a0c1fdeec73b9ebcfeb20f Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 10 Jul 2024 17:25:59 +0800 Subject: [PATCH 3/3] rebuild docs --- man/showDialog.Rd | 2 +- man/showPrompt.Rd | 5 ++++- man/showQuestion.Rd | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/man/showDialog.Rd b/man/showDialog.Rd index 04728c2..efeeaf4 100644 --- a/man/showDialog.Rd +++ b/man/showDialog.Rd @@ -15,7 +15,7 @@ dialog area. Contents can contain the following HTML tags: "p", "em", \item{url}{An optional URL to display under the \code{message}.} -\item{timeout}{An optional timeout (in seconds). When set, if the user takes +\item{timeout}{A timeout (in seconds). When set, if the user takes longer than this timeout to provide a response, the request will be aborted.} } \description{ diff --git a/man/showPrompt.Rd b/man/showPrompt.Rd index 70e4465..762d94c 100644 --- a/man/showPrompt.Rd +++ b/man/showPrompt.Rd @@ -4,7 +4,7 @@ \alias{showPrompt} \title{Show Prompt Dialog Box} \usage{ -showPrompt(title, message, default = NULL) +showPrompt(title, message, default = NULL, timeout = 60) } \arguments{ \item{title}{The title to display in the dialog box.} @@ -14,6 +14,9 @@ dialog area.} \item{default}{An optional character vector that fills the prompt field with a default value.} + +\item{timeout}{A timeout (in seconds). When set, if the user takes +longer than this timeout to provide a response, the request will be aborted.} } \description{ Shows a dialog box with a prompt field. diff --git a/man/showQuestion.Rd b/man/showQuestion.Rd index 6c54f3a..0d10862 100644 --- a/man/showQuestion.Rd +++ b/man/showQuestion.Rd @@ -4,7 +4,7 @@ \alias{showQuestion} \title{Show Question Dialog Box} \usage{ -showQuestion(title, message, ok = NULL, cancel = NULL) +showQuestion(title, message, ok = NULL, cancel = NULL, timeout = 60) } \arguments{ \item{title}{The title to display in the dialog box.} @@ -17,6 +17,9 @@ OK button.} \item{cancel}{An optional character vector that overrides the caption for the Cancel button.} + +\item{timeout}{A timeout (in seconds). When set, if the user takes +longer than this timeout to provide a response, the request will be aborted.} } \description{ Shows a dialog box asking a question.