shinyjs Package
JavaScript operations in Shiny without writing JS.
Setup
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(), # Required!
# ... rest of UI
)
Show/Hide
# UI
div(id = "myDiv", "Content to show/hide")
actionButton("toggle", "Toggle")
# Server
observeEvent(input$toggle, {
toggle("myDiv")
})
# Other options
show("myDiv")
hide("myDiv")
toggle("myDiv", anim = TRUE, animType = "fade")
Enable/Disable
# UI
textInput("text", "Input"),
actionButton("btn", "Submit")
# Server
observe({
if (nchar(input$text) > 0) {
enable("btn")
} else {
disable("btn")
}
})
# Toggle
toggleState("btn", condition = nchar(input$text) > 0)
CSS Classes
addClass("myDiv", "highlight")
removeClass("myDiv", "highlight")
toggleClass("myDiv", "highlight")
HTML Content
html("myDiv", "New content")
html("myDiv", "<b>Bold</b>", add = TRUE)
Reset
# Reset input to default
reset("text")
reset("form") # Reset all inputs in container
Run JavaScript
# Run custom JS
runjs("alert('Hello!')")
runjs("$('#myDiv').css('color', 'red')")
# Extended functions
extendShinyjs(text = "shinyjs.hello = function() { alert('Hi!'); }")
# Then call: js$hello()
Delay
delay(1000, show("myDiv")) # Show after 1 second