Skip to content

Commit

Permalink
Unhardcode timeout in callRemote(), callFun(), showDialog(). Fixes rs…
Browse files Browse the repository at this point in the history
…tudio#291.

Revised function signatures:
showDialog(title, message, url = "", timeout = 10)
callFun(fname, ...)
callRemote(call, frame, timeout = 10)

Original function signatures:
showDialog(title, message, url = "")
callFun(fname, ...)
callRemote(call, frame)

To avoid adding arguments to callFun(), I make an object of list(...) and check whether it has a `timeout` element, then callRemote() with two or three arguments as needed.
  • Loading branch information
jonovik committed Jul 4, 2024
1 parent a985492 commit c1c45f2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
17 changes: 13 additions & 4 deletions R/code.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ getVersion <- function() {
#'
#'
#' @param fname name of the RStudio function to call.
#' @param ... Other arguments passed on to the function
#' @param ... Other arguments passed on to the function.
#' In particular, a `timeout` argument (in seconds) can be specified, by keyword only, for a remote call if running as a job (see `?isJob`).
#' @examples
#'
#' if (rstudioapi::isAvailable()) {
Expand All @@ -90,9 +91,18 @@ getVersion <- function() {
#'
#' @export callFun
callFun <- function(fname, ...) {
args <- list(...)

if (isJob())
return(callRemote(sys.call(), parent.frame()))
if (isJob()) {
if (is.null(args$timeout)) {
return(callRemote(sys.call(),
parent.frame()))
} else {
return(callRemote(sys.call(),
parent.frame(),
timeout = args$timeout))
}
}

verifyAvailable()

Expand All @@ -103,7 +113,6 @@ callFun <- function(fname, ...) {

# drop arguments that aren't accepted by RStudio
# (ensure backwards-compatibility with older versions of RStudio)
args <- list(...)
if (!"..." %in% names(formals(f)))
if (length(args) > length(formals(f)))
length(args) <- length(formals(f))
Expand Down
5 changes: 3 additions & 2 deletions R/dialogs.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#' 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 Timeout (in seconds) passed to `callFun()` if running as a job (see `?isJob`).
#' @note The \code{showDialog} function was added in version 1.1.67 of RStudio.
#' @export showDialog
showDialog <- function(title, message, url = "") {
callFun("showDialog", title, message, url)
showDialog <- function(title, message, url = "", timeout = 10) {
callFun("showDialog", title, message, url, timeout = timeout)
}


Expand Down
16 changes: 14 additions & 2 deletions R/remote.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ isWorkbenchJob <- function() {
identical(Sys.getenv("RSTUDIO_WORKBENCH_JOB"), "1")
}

callRemote <- function(call, frame) {
#' Make remote call.
#'
#' This function facilitates making a remote call in an RStudio job by serializing
#' the call and its arguments, sending them to the remote environment, and waiting
#' for the response.
#'
#' @param call An unevaluated function call captured by `sys.call()`, representing
#' the original function invocation to be executed remotely.
#' @param frame The environment in which to evaluate the arguments of the `call`.
#' This is typically the parent frame captured by `parent.frame()` in the calling function.
#' @param timeout Timeout (in seconds) if running as a job (see `?isJob`).
#' @return The response from the remote execution of the function call.
callRemote <- function(call, frame, timeout = 10) {

# check for active request / response
requestFile <- Sys.getenv("RSTUDIOAPI_IPC_REQUESTS_FILE", unset = NA)
Expand Down Expand Up @@ -80,7 +92,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
Expand Down
3 changes: 2 additions & 1 deletion man/callFun.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/callRemote.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/showDialog.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c1c45f2

Please sign in to comment.