Paul Eagle
Paul Eagle

Reputation: 161

Automatically test if shiny crashes and retrieve error?

I have an app like this:

library(shiny)

ui <- fluidPage(
    mainPanel(
        textOutput("Query_String")
    )
)

server <- function(input, output, session) {
    observeEvent(session$clientData$url_search,{
        Query <- session$clientData$url_search
        output$Query_String <- renderText(Query)
        # Long list of operations dependant on the parameters passed in the URL
    })
}

shinyApp(ui = ui, server = server)

That takes an URL query as parameter. I have a list with around ~5000 entries for all possible queries that should be accepted by the app and I can run the app iterating through the queries by calling the app via something like this:

runApp(
  appDir = "R",
  port = 3838,
  launch.browser = function(appUrl) {
    url <- paste0(appUrl, "/?query")
    invisible(.Call("rs_shinyviewer", url, getwd(), "browser", NULL, PACKAGE = "(embedding)"))
  },
  host = "0.0.0.0"
)

Now my question:
How can I catch which queries may make the app crash?
I have tried wrapping the logic inside the server with a big tryCatch() but that apparently doesnt do anything, neither does wrapping the runApp() with tryCatch() (although this makes sense to me).
Ideas?

Upvotes: 1

Views: 508

Answers (1)

David Mu&#241;oz Tord
David Mu&#241;oz Tord

Reputation: 365

To test if shiny app crashes on launch

testthat::test_that("server launches",{
local_edition(3)
skip_on_cran()
skip_on_travis()
skip_on_appveyor() 
app <- shinytest::ShinyDriver$new(here::here()) #open shiny app

a <- data.frame(app$getDebugLog()) #check debug log for error

a <- a %>%  
  filter(!grepl('Warning: ', message)) %>% filter(level == "ERROR") 

if (length(grep("Error in", a)) == 0) 
  alive = TRUE
 else 
  alive = FALSE 
expect_true(alive) })

Upvotes: 1

Related Questions