0

我有在更新闪亮时有效的代码,但我想将事件处理更改为在 2 个事件中的 1 个发生时发生,而不是在两个事件发生时发生。现在,用户必须更新两个下拉菜单以更新图表(input$company 和 input$peer)。如果其中任何一个事件发生,我希望情节能够奏效。我想我可以用另一种方式来做这个,而不是observeEvent,但我是新手。谢谢您的帮助。

服务器.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))

  })
  })
4

0 回答 0