Can I pass parameters to a shiny app via URL?

I have a shiny app
https://merrittr.shinyapps.io/hydroshiny/

that starts off with 2 default parameters and then below alows the user to change these parameters via a side panel. Ideally for this application I would like to link to this app from a map that would have an href and a link that would provide the app the input$z and input$y

https://merrittr.shinyapps.io/hydroshiny/?y=05EF001&z=WaterLevel

is that possible?

output$lineplot <- renderPlot({
        subds <- subset(skdat, ID == input$z)
        subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H:%M:%OS")
        ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) +
        geom_line()
})

My other motivation is to be able to get rid of that sidebar so the graph is bigger

You'd have to update the input inside the app when initializes. You would use the session$clientData$url_search variable to get the query parameters.

This is a simple reproducible example, that would work with this type of url https://merrittr.shinyapps.io/example-app/?bins=1

library(shiny)

ui <- fluidPage(

    titlePanel("Old Faithful Geyser Data"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        mainPanel(
           plotOutput("distPlot")
        )
    )
)

server <- function(input, output, session) {
    # Here you read the URL parameter from session$clientData$url_search
    observe({
        query <- parseQueryString(session$clientData$url_search)
        if (!is.null(query[['bins']])) {
            updateSliderInput(session, "bins", value = query[['bins']])
        }
    })
    
    output$distPlot <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

You can modify this to make it work with a reactive value for example and get rid of the controls.

4 Likes

HA! that is great! thanks again Andres!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.