问:R Shiny Reactivity,2个事件中的1个

时间:2018-08-06 16:48:44

标签: r shiny shiny-reactivity

我有更新闪亮的代码,但是我想将事件处理更改为在2个事件中的1个发生而不是在两个事件都发生时发生。现在,用户必须更新两个下拉菜单以更新图形(input $ company和input $ peer)。如果其中任何一个事件发生,我都希望该图能够正常工作。我想我可以用observeEvent以外的其他方式来做这件事,但是我对此并不陌生。感谢您的帮助。

Server.R

 server <- function(input, output) {

  observeEvent({
    input$company
    input$peer},{

  company_Bal <- data %>%
    select(Company, Date, End_Bal, Product) %>%
    filter(Product == "xxx", Company %in% input$company) %>%
    group_by(Date) %>%
    summarise(End_Bal = sum(End_Bal, na.rm = TRUE)) %>%
    collect()

  Peer_Bal <- data %>%
    select(Company, Date, End_Bal, Product) %>%
    filter(Product == "xxx", Company %in% input$peer) %>%
    group_by(Date) %>%
    summarise(End_Bal = sum(End_Bal, na.rm = TRUE)) %>%
    collect()


  output$hist <- renderPlotly({plot_ly(mode = 'lines')%>%
      add_lines(
        data =  company_Bal, 
        x = ~Date, 
        y = ~End_Bal,
        marker = list(
          size = 6, 
          color = 'rgba(31,119,180,0.0)',
          line = list(
            color = 'rgb(31,119,180)',
            width = 2
            )

          ),
        name = list(input$company), showlegend = TRUE) %>%
      add_lines(
        data =  Peer_Bal,
        x = ~Date,
        y = ~End_Bal,
        mode = 'lines',
        marker = list(
          size = 6, 
          color = 'rgba(31,119,180,0.0)',
          line = list(
            color = 'rgb(31,119,180)',
            width = 2
          )

        ),
        name = list(input$peer),showlegend = TRUE) %>%
      layout(xaxis = list(title = "", ticks= company_Bal$Date), showlegend = T, legend = list(orientation = "h", xanchor = "center",x = 0.5))

  })
  })

0 个答案:

没有答案