Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

execute_safely

Execute an expression safely in server


Description

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

Usage

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

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

Custom Inputs Widgets for Shiny

v0.6.0
GPL-3
Authors
Victor Perrier [aut, cre, cph], Fanny Meyer [aut], David Granjon [aut], Ian Fellows [ctb] (Methods for mutating vertical tabs & updateMultiInput), Wil Davis [ctb] (numericRangeInput function), Spencer Matthews [ctb] (autoNumeric methods), JavaScript and CSS libraries authors [ctb, cph] (All authors are listed in LICENSE.md)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.