Adding JS to UI
As onclick
is a valid HTML attribute for the <button> tag, we can use it within Shiny’s actionButton to execute arbitrary JavaScript code when the button is clicked.
onclick
is supported for all HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>
library(shiny)
ui <- fluidPage(
actionButton(
"unbind",
"Unbind inputs",
onclick = "Shiny.unbindAll();"
),
actionButton(
"bind",
"Bind inputs",
onclick = "Shiny.bindAll(document);"
),
lapply(1:3, function(i) {
textInput(paste0("text_", i), paste("Text", i))
}),
lapply(1:3, function(i) {
uiOutput(paste0("val_", i))
})
)
server <- function(input, output, session) {
lapply(1:3, function(i) {
output[[paste0("val_", i)]] <- renderPrint({
input[[paste0("text_", i)]]
})
})
}
shinyApp(ui, server)