Skip to content

Instantly share code, notes, and snippets.

@ddbs
Created May 13, 2020 12:54
Show Gist options
  • Save ddbs/966cbfde38b810450904fcf340eee8b8 to your computer and use it in GitHub Desktop.
Save ddbs/966cbfde38b810450904fcf340eee8b8 to your computer and use it in GitHub Desktop.
shiny two reactive buttons on the same app
## Execute multiple buttons
foo <- function() {
message("one")
Sys.sleep(0.5)
message("two")
}
bar <- function() {
message("alpha")
Sys.sleep(0.5)
message("beta")
}
runApp(shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
h3("foo button"),
actionButton("btn_foo","Click me for foo"),
textOutput("text_foo"),
h3("bar button"),
actionButton("btn_bar","Click me for bar"),
textOutput("text_bar")
),
server = function(input,output, session) {
#foo
observeEvent(input$btn_foo, {
withCallingHandlers({
shinyjs::html("text_foo", "")
foo()
},
message = function(m) {
shinyjs::html(id = "text_foo", html = m$message, add = TRUE)
})
})
#bar
observeEvent(input$btn_bar, {
withCallingHandlers({
shinyjs::html("text_bar", "")
bar()
},
message = function(m) {
shinyjs::html(id = "text_bar", html = m$message, add = TRUE)
})
})
}
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment