Add the parameters of the shiny app to the URL | R-Bloggers

Add the parameters of the shiny app to the URL | R-Bloggers

3 minutes, 32 seconds Read

[This article was first published on pacha.dev/blog, and kindly contributed to R-bloggers]. (You can report problems here about the content on this page)


Do you want to share your content on R-bloggers? Click here if you have a blog, or here If you don’t.

Shiny makes it possible to use all R to visualize information, regardless of whether it is a refined statistical model or a simple plot. One of the functions that the non-out-of-the-box offers is to add the selected parameters to the URL, such as this:

https://pacha.dev/palmerpenguinsshiny?inputs&species="Adelie"&island="Torgersen"

To have this type of URL, I will demonstrate a simple case with wing. Let’s start making a project:

golem::create_golem("palmerpenguinsshiny")

Open R/run_app.R and change enableBookmarking = NULL Unpleasant enableBookmarking = "url".

Open R/app_server.R and add these lines at the end of app_server():

# Bookmarking ----

observe({
    # Trigger this observer every time an input changes
    # strip shiny related URL parameters
    shiny::reactiveValuesToList(input)
    setBookmarkExclude(c(
        "parameter_not_in_url"
    ))
    session$doBookmark()
})

onBookmarked(function(url) {
    updateQueryString(url)
})

Now we have to add content to the app. Let us make an app with which the user can filter by type and island to obtain a plot of the body mass distribution.

Here is a shortcut to simplify things and use the pipe operator:

usethis::use_pipe()
devtools::document()

Make a copy of the data we need:

penguins_sib <- palmerpenguins::penguins[, c("species", "island", "body_mass_g")]
usethis::use_data(penguins_sib)

Now let’s add content to app_server()like this:

#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @import ggplot2
#' @importFrom dplyr filter
#'
#' @noRd
app_server <- function(input, output, session) {
  # Main plot ----

  # Filter by species and island, then show the distribution of body_mass_g
  output$main_plot <- renderPlot({
    req(input$species, input$island)
    penguins_sib %>%
      filter(
        species %in% input$species,
        island %in% input$island
      ) %>%
      ggplot(aes(x = body_mass_g)) +
      geom_histogram(bins = input$bins, fill = input$fill, color = "black") +
      labs(
        title = "Distribution of Body Mass (g)",
        x = "Body Mass (g)",
        y = "Count"
      ) +
      theme_minimal(base_size = 13)
  })

  # Bookmarking ----

  observe({
    # Trigger this observer every time an input changes
    # strip shiny related URL parameters
    shiny::reactiveValuesToList(input)
    setBookmarkExclude(c(
      "fill"
    ))
    session$doBookmark()
  })

  onBookmarked(function(url) {
    updateQueryString(url)
  })
}

Spend the server logic on the onion side:

#' The application User-Interface
#'
#' @param request Internal parameter for `{shiny}`.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # Your application UI logic
    
    # Filters

    sidebarLayout(
      sidebarPanel(
        selectInput("species", "Select Species:", choices = unique(penguins$species), multiple = TRUE),
        selectInput("island", "Select Island:", choices = unique(penguins$island), multiple = TRUE),
        selectInput("fill", "Select Fill Color:", choices = c("#3d809d", "#d04e66", "#365158"),
          multiple = FALSE, selected = "#3d809d"),
        sliderInput("bins", "Number of Bins:", min = 1, max = 50, value = 30)
      ),
      mainPanel(
        plotOutput("main_plot")
      )
    )
  )
}

#' Add external Resources to the Application
#'
#' This function is internally used to add external
#' resources inside the Shiny application.
#'
#' @import shiny
#' @importFrom golem add_resource_path activate_js favicon bundle_resources
#' @noRd
golem_add_external_resources <- function() {
  add_resource_path(
    "www",
    app_sys("app/www")
  )

  tags$head(
    favicon(),
    bundle_resources(
      path = app_sys("app/www"),
      app_title = "palmerpenguins"
    )
    # Add here other external resources
    # for example, you can add shinyalert::useShinyalert()
  )
}

Run now:

devtools::load_all()
run_app()

You would see your app run locally and if you select the two required fields (species and island), there will be a rendered plot and a URL of the shape:

http://127.0.0.1:6736/?_inputs_&species=%22Adelie%22&island=%22Biscoe%22&bins=30

Note that the filling color is not in the URL, but that is deliberately 🙂

The full code is here: https://github.com/pachadotdev/palmerpenguinsshiny.

Don’t forget to add a license to your app (eg open dev/01_start.R To select the MIT license or use Apache usethis::use_apache_license()).

If this source was useful for you, consider donating here: https://buymeacoffee.com/pacha.


#Add #parameters #shiny #app #URL #RBloggers

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *