Search code examples
rshinyshiny-server

R shiny server disconnect with url parameters


I am running a shiny server on AWS, and attempting to set url query parameters from user input, and allow forwarding of a url to others. The simplified example below works to retrieve values from checkbox and slider inputs, and push them to the url string when the submit button is clicked e.g. url:port?param1=1,2,3,4&param2=0,100

I can also initially go directly to url:port?param1=1,2,3,4&param2=0,100 (simulating a user forwarding the url to others), however, once some time has passed, this link returns a "disconnected from server" server grey screen, and the logs state "Error getting worker: Error: The application exited during initialization.".

It seems the server sleeps and will only resume if I first go to the base url, and then after that, visit the url containing the parameters. is there any way around this to share urls in this way and bypass first needing the visit the base url?

# Load required libraries.
library(shiny)

# Shiny ui.
ui <- shinyUI(
  fillPage(

    # Add filter controls.
    absolutePanel(
       style='overflow-y:scroll; border-bottom: 1px solid #CCC; padding: 10px; background: #F5F5F5;',
       fixed = TRUE,
       draggable = FALSE, 
       top = 0,
       right = 0,
       bottom=0,
       left=0,
       width = '100%',
       actionButton('submit', 'Submit'),
       checkboxGroupInput('input1', 'Input 1', c(1,2,3,4), c(1,2,3,4)),
       sliderInput('input2', 'Input 2', min=0, max=100, value=c(0,100), step=10),
       verbatimTextOutput("output1", TRUE),
       verbatimTextOutput("output2", TRUE)
    )
  )
)

# Shiny server.
server <- function(input, output, session){

  # Observer to read url query string.
  observe({
    query <- parseQueryString(session$clientData$url_search)
    param1 <- query$param1
    param2 <- query$param2
    output$output1 <- renderText(as.character(param1))
    output$output2 <- renderText(as.character(param2))
  })

  # Observer for button click.
  observeEvent(input$submit, {
    param1 <- paste(input$input1, collapse=',')
    param2 <- paste(input$input2, collapse=',')
    string <- paste0('?param1=', param1, '&param2=', param2)
    updateQueryString(string, mode = 'push')
  })
}

# Run app.
shinyApp(ui, server)

Solution

  • Turns out the issue was not what I thought, posting the link elsewhere had converted & to the HTML ampersand, invalidating the query string and causing the app to fail.