Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

r - Get the event which is fired in Shiny?

I would like to know in the example below which event is fired in the multiple ObserveEvent().

ui <- fluidPage(
  numericInput("a", "a", 0),
  textInput("b", "b")
)

server <- function(input, output, session) {
  observeEvent({
    input$a
    input$b
  },{

    # If only input$a is fired, I want to know that is input$a

  })
}

shinyApp(ui, server)

Or the only solution is to have two ObserveEvent() like the second link ? like that ?

ui <- fluidPage(
  numericInput("a", "a", 0),
  textInput("b", "b")
)

server <- function(input, output, session) {
  observeEvent({
    input$a
  },{

    my_function_or_reactive_function(input,1)

  })

  observeEvent({
    input$b
  },{

    my_function_or_reactive_function(input,2)

  })

}

shinyApp(ui, server)

link:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use shiny's JS event shiny:inputchanged to check which input changed:

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

server <- function(input, output, session) {
  output$changedInputs <- renderText({
    paste("Outside observer: Latest input fired:", paste(input$changed, collapse = ", "))
  })
  
  observeEvent({
    c(input$a,
      input$b)
  }, {
    req(input$changed)
    if (input$changed == "a") {
      output$aFired <- renderText("Inside observer: input$a was fired")
    } else if (input$changed == "b") {
      output$aFired <- renderText("Inside observer: input$b was fired")
    } else if (input$changed == "c") {
      output$aFired <- renderText("Inside observer: input$c was fired")
    }
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)

Result:

Edit - request from @TristanTran using renderUI:

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$script(
      "$(document).on('shiny:inputchanged', function(event) {
          if (event.name != 'changed') {
            Shiny.setInputValue('changed', event.name);
          }
        });"
    )
  ),
  uiOutput("serverside"),
  textOutput("changedInputs"),
  textOutput("aFired")
)

server <- function(input, output, session) {
  output$changedInputs <- renderText({
    paste("Outside observer: Latest input fired:", paste(input$changed, collapse = ", "))
  })
  
  output$serverside <- renderUI({
    tagList(
      numericInput("a", "a", 0),
      textInput("b", "b"),
      textInput("c", "c")
    )
  })
  
  observeEvent({
    c(input$a,
      input$b)
  }, {
    req(input$changed)
    if (input$changed == "a") {
      output$aFired <- renderText("Inside observer: input$a was fired")
    } else if (input$changed == "b") {
      output$aFired <- renderText("Inside observer: input$b was fired")
    } else if (input$changed == "c") {
      output$aFired <- renderText("Inside observer: input$c was fired")
    }
  }, ignoreInit = TRUE)
}

shinyApp(ui, server)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...