Shiny: calculate cumsum based on dygraphs' RangeSelector

前端 未结 2 1785
慢半拍i
慢半拍i 2021-01-25 04:47

I\'m building a shiny app where I want to plot a dataset with one of the variables being a cumulative sum of another variable. The latter needs to be re-calculated every time th

2条回答
  •  别那么骄傲
    2021-01-25 05:16

    This should do the job, I think it is cleaner to have separate outputs for your dashboard

    library(xts)
    library(shiny)
    library(shinydashboard)
    library(dygraphs)
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        dygraphOutput('plot1'),
        textOutput("cumsum1")
      )
    )
    
    server <- function(input, output, session) {
    
      m_df <- data.frame(date=as.Date(zoo::as.yearmon(time(mdeaths))), Y=as.matrix(mdeaths))
    
      output$plot1 <- renderDygraph({
        input_xts <- xts(select(m_df, -date), order.by = m_df$date)
    
        dygraph(input_xts) %>% 
          dyRangeSelector()
      })
    
      output$cumsum1 <- renderText({
        req(input$plot1_date_window)
        subdata <- cumsum(m_df$Y[m_df$date >= as.Date(input$plot1_date_window[1]) & m_df$date <= as.Date(input$plot1_date_window[2])])
        subdata
      })
    
    }
    
    shinyApp(ui, server)
    

提交回复
热议问题