本文介绍了获取在 Shiny 中触发的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在下面的例子中哪个事件在多个 ObserveEvent() 中被触发.

ui 

或者唯一的解决方案是像第二个链接那样有两个 ObserveEvent() ?像那样吗?

ui 

链接:

  • :

    编辑 - 使用 renderUI 来自 @TristanTran 的请求:

    图书馆(闪亮)ui <-流体页面(标签$头(标签$脚本("$(document).on('shiny:inputchanged', function(event) {如果(事件名称!='改变'){Shiny.setInputValue('changed', event.name);}});")),uiOutput(服务器端"),textOutput("changedInputs"),文本输出(aFired"))服务器 <- 功能(输入,输出,会话){output$changedInputs <- renderText({粘贴(外部观察者:最新输入被触发:",粘贴(输入$更改,折叠=,"))})输出$serverside <- renderUI({标签列表(numericInput("a", "a", 0),textInput("b", "b"),textInput("c", "c"))})观察事件({c(输入$a,输入$b)}, {请求(输入$改变)if (input$changed == "a") {output$aFired <- renderText(内部观察者:input$a 被解雇了")} else if (input$changed == "b") {output$aFired <- renderText(内部观察者:input$b 被解雇了")} else if (input$changed == "c") {output$aFired <- renderText(内部观察者:input$c 被解雇了")}}, ignoreInit = TRUE)}闪亮应用程序(用户界面,服务器)

    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:

    解决方案

    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)
    


    :

    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)
    

    这篇关于获取在 Shiny 中触发的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:21