Skip to content

Commit

Permalink
another attempt to close rstudio#249, using withCallingHandlers() ins…
Browse files Browse the repository at this point in the history
…tead of modifying tryCatch()

also closes rstudio#217 if everyone agrees with this approach
  • Loading branch information
yihui committed Oct 16, 2013
1 parent f4a4af0 commit 6af7de5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 2 additions & 3 deletions R/imageutils.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ plotPNG <- function(func, filename=tempfile(fileext='.png'),
}

pngfun(filename=filename, width=width, height=height, res=res, ...)
tryCatch(
func(),
finally=dev.off())
dv <- dev.cur()
tryCatch(shinyCallingHandlers(func()), finally = dev.off(dv))

filename
}
2 changes: 1 addition & 1 deletion R/reactives.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ Observable <- setRefClass(
on.exit(.running <<- wasRunning)

ctx$run(function() {
result <- withVisible(try(.func(), silent=FALSE))
result <- withVisible(try(shinyCallingHandlers(.func()), silent=FALSE))
.visible <<- result$visible
.value <<- result$value
})
Expand Down
9 changes: 3 additions & 6 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ShinySession <- setRefClass(

obs <- observe({

value <- try(func(), silent=FALSE)
value <- try(shinyCallingHandlers(func()), silent=FALSE)

.invalidatedOutputErrors$remove(name)
.invalidatedOutputValues$remove(name)
Expand Down Expand Up @@ -1383,15 +1383,12 @@ runApp <- function(appDir=getwd(),

.globals$retval <- NULL
.globals$stopped <- FALSE
tryCatch(
tryCatch(shinyCallingHandlers(
while (!.globals$stopped) {
serviceApp()
Sys.sleep(0.001)
},
finally = {
timerCallbacks$clear()
}
)
), finally = timerCallbacks$clear())

return(.globals$retval)
}
Expand Down
10 changes: 10 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ parseQueryString <- function(str) {
setNames(as.list(values), keys)
}

# decide what to do in case of errors; it is customizable using the shiny.error
# option (e.g. we can set options(shiny.error = recover))
shinyCallingHandlers <- function(expr) {
withCallingHandlers(expr, error = function(e) {
handle <- getOption('shiny.error')
if (is.null(handle) || !is.function(handle)) return()
if (length(formals(handle)) > 0) handle(e) else handle()
})
}

#' Print message for deprecated functions in Shiny
#'
#' To disable these messages, use \code{options(shiny.deprecation.messages=FALSE)}.
Expand Down

0 comments on commit 6af7de5

Please sign in to comment.