Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature requested: Get the event which is fired in ObserveEvent #2514

Open
philibe opened this issue Jun 27, 2019 · 2 comments
Open

Feature requested: Get the event which is fired in ObserveEvent #2514

philibe opened this issue Jun 27, 2019 · 2 comments

Comments

@philibe
Copy link

philibe commented Jun 27, 2019

Hello,

From my question below in stackoverflow I was surprised that the functionality of getting the event which is fired in ObserveEvent() apparently doesn't exist by default in observeEvent().

I've looked for "event fired" in the issues and the Shiny docs but I didn't found it :)

Have a good day.

Here is the copy-paste of the stackoverflow workaround by ismirsehregal

ui <- fluidPage(
  tags$head(
    tags$script(
      "$(document).on('shiny:inputchanged', function(event) {
          if (event.name != 'changed') {
            Shiny.setInputValue('changed', event.name);
          }
        });"
    )
  ),
  numericInput("a_1", "a_1", 0),
  textInput("a_2", "a_2"),
  textInput("c", "c"),
  textInput("d", "d"),

  p("changedInputs:"), textOutput("changedInputs"), br(),
  p("aFired:"), textOutput("aFired")
)

server <- function(input, output, session) {
  output$changedInputs <- renderText({
    paste("Outside observer: Latest input fired:", paste(input$changed, collapse = ", "))
  })

  myText <- reactiveVal()

  observeEvent(eventExpr = {
    lapply(grep(pattern = "^a_+[[:digit:]]$|^c$", x = isolate({names(input)}), value = TRUE), function(x){input[[x]]})
  }, handlerExpr = {
    req(input$changed)
    if (input$changed == "a_1") {
      myText("Inside observer: input$a_1 was fired")
    } else  if (input$changed == "a_2") {
      myText("Inside observer: input$a_2 was fired")
    } else {
      myText(paste("Inside observer:", input$changed, "was fired"))
    }
  }, ignoreInit = TRUE)

  output$aFired <- renderText({myText()})

}

shinyApp(ui, server)
@ismirsehregal
Copy link
Contributor

Here is a simplified example showing the mentioned workaround (based on the JS event shiny:inputchanged) to receive the input names which triggered observeEvent(). The SO example might be a little difficult to get into.

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$script(
      "$(document).on('shiny:inputchanged', function(event) {
          if (event.name != 'changed') {
            Shiny.setInputValue('changed', event.name);
          }
        });"
    )
  ),
  sliderInput("a", "input$a", 0, 10, 5),
  sliderInput("b", "input$b", 0, 10, 5),
  sliderInput("c", "input$c", 0, 10, 5),
  textOutput("printChangedInputs"),
  textOutput("printTrigger")
)

server <- function(input, output, session) {
  observeEventTrigger <-  reactiveVal()
  
  observeEvent(c(input$a, input$b), {
    observeEventTrigger(req(input$changed))
    cat("My execution was triggered by input:", observeEventTrigger(), "\n")
  }, ignoreInit = TRUE)
  
  output$printChangedInputs <- renderText({
    paste("Latest input used:", input$changed)
  })
  
  output$printTrigger <- renderText({
    paste("Latest observeEvent trigger:", observeEventTrigger())
  })
}

shinyApp(ui, server)

@daattali
Copy link
Contributor

Closely related to #1846

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants