Search code examples
rshinyeval

R Shiny, shinyapps.io printing error messages for R codes


I was trying to create a Shiny app that takes code blocks and runs the code then gives the output for that code block.

To do that I take a textInput, then with the following, tried to provide outputs for users.

Question1 <- reactive({
    eval(parse(text=input$Question1_code))
})

output$Question1_output <- renderText({
    input$Run_Code
    isolate(paste(Question1()))
})

Now, my problem is when I run this on locally, the output I get for wrong statements/codes e.g.

seq(1,10,-2)

is wrong sign in 'by' argument. (which is what I would like to see for a wrong statement)

However, when I run it on shinyapps.io, I get the following error message,

An error has occurred. Check your logs or contact the app author for clarification.

How can I print the same error message that I am getting locally (wrong sign in 'by' argument) on shinyapps.io too?


Solution

  • use shinyCatch from spsComps

    Example for your case:

    library(shiny)
    library(spsComps)
    ui <- fluidPage(
      actionButton("a", "blocking"),
      actionButton("b", "no blocking"),
    )
    
    server <- function(input, output, session) {
        observeEvent(input$a, {
          spsComps::shinyCatch({
              seq(1,10,-2)
          },
          # blocking recommended
          blocking_level = "error",
          prefix = "My-project" #change console prefix if you don't want "SPS"
          )
          # some other following actions will NOT  be run 
          print("other actions")
        })
        
        # if you dont want to block
        observeEvent(input$b, {
            spsComps::shinyCatch({
                seq(1,10,-2)
            }, prefix = "My-project")
            # some other following actions will run 
            print("other actions")
        })
    }
    
    shinyApp(ui, server)
    

    or try more demos here