Skip to content

Instantly share code, notes, and snippets.

@pepijn-devries
Created June 30, 2017 20:12
Show Gist options
  • Save pepijn-devries/fda485982361c2c00bdf031bb5751ef6 to your computer and use it in GitHub Desktop.
Save pepijn-devries/fda485982361c2c00bdf031bb5751ef6 to your computer and use it in GitHub Desktop.
Trick-event-server
# This part of the code runs on a Shiny server and interacts with the
# user interface shown at the client's side.
# For details and tutorials go to http://shiny.rstudio.com
server <- shinyServer(function(input, output, server) {
# Make these character strings 'reactive'. That way
# the output text is automatically update when these
# values are changed.
values <- reactiveValues(state = c("Interface1: Waiting for clicks",
"Interface2: Waiting for clicks"))
# Try to count the number of clicks on each interface. Initialize the counter
interface <- c(-1, -1)
# observer for clicks on any of the custom interface elements:
observeEvent(input$customInterface, {
# This event observer will always respond to clicks on the second
# interface, as it's value is negated with each click.
# We just need to make sure that we take the absolute value of
# any negated values:
i <- abs(input$customInterface)
# count the number of clicks and reset once they have been clicked twice:
interface[i] <<- (interface[i] + 1)%%2
if (interface[i] == 0) values$state[i] <- sprintf("Interface%i: click me once, shame on you!", i)
if (interface[i] == 1) values$state[i] <- sprintf("Interface%i: click me twice, shame on me!", i)
})
# render the output text:
output$outputText <- renderText(paste0(c(values$state[1],
values$state[2]), collapse = "\n"))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment