IT박스

Shiny eventReactive 핸들러 내에서 둘 이상의 이벤트 표현식을 수신하는 방법

itboxs 2020. 10. 24. 09:54
반응형

Shiny eventReactive 핸들러 내에서 둘 이상의 이벤트 표현식을 수신하는 방법


내 앱의 다양한 플롯 / 출력에서 ​​사용되는 데이터의 업데이트를 트리거하는 두 가지 다른 이벤트를 원합니다. 하나는 클릭되는 버튼 ( input$spec_button)이고 다른 하나는 클릭 되는 점의 점 ( mainplot.click$click)입니다.

기본적으로 두 가지를 동시에 나열하고 싶지만 코드 작성 방법을 잘 모르겠습니다. 지금 가지고있는 것은 다음과 같습니다.

server.R :

data <- eventReactive({mainplot.click$click | input$spec_button}, {
    if(input$spec_button){
      # get data relevant to the button
    } else {
      # get data relevant to the point clicked
    }
  })

그러나 if-else 절은 작동하지 않습니다.

Error in mainplot.click$click | input$spec_button : operations are possible only for numeric, logical or complex types

-> mainplot.click$click | input$spec_button절에 사용할 수있는 일종의 동작 결합 기능이 있습니까?


나는 이것이 오래되었다는 것을 알고 있지만 같은 질문이 있었다. 나는 마침내 그것을 알아 냈습니다. 중괄호 안에 표현식을 포함하고 단순히 이벤트 / 반응 객체를 나열합니다. 내 (확실하지 않은) 추측은 shiny가이 표현식 블록에 대해 표준 reactive블록 과 동일한 반응 포인터 분석을 수행한다는 것 입니다.

observeEvent({ 
  input$spec_button
  mainplot.click$click
}, { ... } )

또한:

observeEvent(c( 
  input$spec_button,
  mainplot.click$click
), { ... } )

반응 객체를 생성하여이 문제를 해결하고 이벤트 변경 표현에 사용했습니다. 아래:

xxchange <- reactive({
paste(input$filter , input$term)
})

output$mypotput <- eventReactive( xxchange(), {
...
...
...
} )

내가 생각 해낸 해결책은 다음과 같습니다. 기본적으로 빈 reactiveValues데이터 홀더를 만든 다음 두 개의 개별 observeEvent인스턴스를 기반으로 값을 수정 합니다.

  data <- reactiveValues()
  observeEvent(input$spec_button, {
    data$data <- get.focus.spec(input=input, premise=premise, 
                                itemname=input$dropdown.itemname, spec.info=spec.info)
  })
  observeEvent(mainplot.click$click, {
    data$data <- get.focus.spec(input=input, premise=premise, mainplot=mainplot(),
                                mainplot.click_focus=mainplot.click_focus(),
                                spec.info=spec.info)  
  })

참고 URL : https://stackoverflow.com/questions/34731975/how-to-listen-for-more-than-one-event-expression-within-a-shiny-eventreactive-ha

반응형