Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Last active June 26, 2020 01:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcheng5/a1bb9aa3e975c92ce581dd4d255b6ab4 to your computer and use it in GitHub Desktop.
Save jcheng5/a1bb9aa3e975c92ce581dd4d255b6ab4 to your computer and use it in GitHub Desktop.
Using a more recent version of jQuery with Shiny

To opt into a more recent version of jQuery, you can add an htmlDependency object pointing to your desired version, somewhere (anywhere) in your UI:

htmltools::htmlDependency("jquery", "3.3.1",
  src = c(href = "https://code.jquery.com/"),
  script = "jquery-3.3.1.min.js")

This example will serve jQuery 3.3.1 from the jQuery CDN. If your app needs to work with clients that won't be able to connect to the wider internet, you'll need to download the jquery-3.3.1.min.js file, put it in an app subdirectory (say, jquery), and point to the directory using the src argument.

htmltools::htmlDependency("jquery", "3.3.1",
  src = here::here("jquery"),
  script = "jquery-3.3.1.min.js")

Note that Shiny is only tested with the version of jQuery that it ships with, and we cannot guarantee that problems will not arise by upgrading, especially to a different major version. However, please do report any issues you run into with newer jQuery versions at https://github.com/rstudio/shiny/issues and we will try to address them.

library(shiny)
ui <- fluidPage(
htmltools::htmlDependency("jquery", "3.3.1",
src = c(href = "https://code.jquery.com/"),
script = "jquery-3.3.1.min.js"),
numericInput("n", "n", 5),
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
plot(head(cars, input$n))
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment