# NOT RUN {
if (interactive()) {
library(shiny)
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
# Add a CSS class for red text colour
inlineCSS(list(.red = "background: red")),
actionButton("btn", "Click me"),
p(id = "element", "Watch what happens to me")
),
server = function(input, output) {
observeEvent(input$btn, {
# Change the following line for more examples
toggleClass("element", "red")
})
}
)
}
# }
# NOT RUN {
# The shinyjs function call in the above app can be replaced by
# any of the following examples to produce similar Shiny apps
toggleClass(class = "red", id = "element")
addClass("element", "red")
removeClass("element", "red")
# }
# NOT RUN {
## toggleClass can be given an optional `condition` argument, which
## determines if to add or remove the class
if (interactive()) {
shinyApp(
ui = fluidPage(
useShinyjs(),
inlineCSS(list(.red = "background: red")),
checkboxInput("checkbox", "Make it red"),
p(id = "element", "Watch what happens to me")
),
server = function(input, output) {
observe({
toggleClass(id = "element", class = "red",
condition = input$checkbox)
})
}
)
}
# }
Run the code above in your browser using DataLab