execute_safely: Execute an expression safely in server

Description Usage Arguments Value Examples

View source: R/execute_safely.R

Description

Execute an expression without generating an error, instead display the error to the user in an alert.

Usage

1
2
3
4
5
6
7
8
execute_safely(
  expr,
  title = "Error",
  message = "An error occured, detail below:",
  include_error = TRUE,
  error_return = NULL,
  session = shiny::getDefaultReactiveDomain()
)

Arguments

expr

Expression to evaluate

title

Title to display in the alert in case of error.

message

Message to display below title.

include_error

Include the error message generated by R.

error_return

Value to return in case of error.

session

Shiny session.

Value

Result of expr if no error, otherwise the value of error_return (NULL by default to use req in other reactive context).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  tags$h2("Execute code safely in server"),
  fileInput(
    inputId = "file",
    label = "Try to import something else than a text file (Excel for example)"
  ),
  verbatimTextOutput(outputId = "file_value")
)

server <- function(input, output, session) {

  options(warn = 2) # turns warnings into errors
  onStop(function() {
    options(warn = 0)
  })

  r <- reactive({
    req(input$file)
    execute_safely(
      read.csv(input$file$datapath)
    )
  })

  output$file_value <- renderPrint({
    head(r())
  })

}

if (interactive())
  shinyApp(ui, server)

shinyWidgets documentation built on Sept. 17, 2021, 5:06 p.m.