有光泽的:根据输入值添加/删除时间序列到dygraphs

问题描述 投票:1回答:1

我正在构建一个闪亮的应用程序,它将在dygraphs中显示一个基本数据集,然后提供一个选项,在选择复选框输入时添加新的时间序列。但是,正如我现在编写的那样,我“陷入”原始数据集并且无法添加/删除新内容。任何提示如何解决这个问题都非常受欢迎,谢谢。

library(shinydashboard)
library(dygraphs)
library(dplyr)


ui <-dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    checkboxGroupInput(inputId = 'options',
                       label = 'Choose your plot(s)',
                       choices = list("mdeaths" = 1,
                                      "ldeaths" = 2)
    ),

    uiOutput("Ui1")
  )
)

server <- function(input, output, session) {

  output$Ui1 <- renderUI({


    output$plot1 <- renderDygraph({

      final_ts <- ldeaths
      p <- dygraph(final_ts, main = 'Main plot') %>% 
        dygraphs::dyRangeSelector()

      if(1 %in% input$options) {

        final_ts <- cbind(final_ts, mdeaths)
        p <- p %>% 
          dySeries('mdeaths', 'Male Deaths')

      } else if(2 %in% input$options) {

        final_ts <- cbind(final_ts, fdeaths)
        p <- p %>% 
          dySeries('fdeaths', 'Female Deaths')

      } 

      p

    })

    dygraphOutput('plot1')
  })


}

shinyApp(ui, server)
shiny shinydashboard dygraphs
1个回答
1
投票

我建议根据用户选择动态过滤数据,而不是动态添加/删除绘图中的痕迹:

library(shinydashboard)
library(shinyjs)
library(dygraphs)
library(dplyr)

lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    selectizeInput(
      inputId = "options",
      label = "Choose your trace(s)",
      choices = colnames(lungDeaths),
      selected = colnames(lungDeaths)[1],
      multiple = TRUE,
      options = list('plugins' = list('remove_button'))
    ),
    uiOutput("Ui1")
  )
)

server <- function(input, output, session) {
  output$Ui1 <- renderUI({
    filteredLungDeaths <- reactive({
      lungDeaths[, input$options]
    })

    output$plot1 <- renderDygraph({

      p <- dygraph(filteredLungDeaths(), main = 'Main plot') %>%
        dygraphs::dyRangeSelector()

      if('mdeaths' %in% colnames(filteredLungDeaths())){
        p <- dySeries(p, 'mdeaths', 'Male Deaths')
      }

      if('fdeaths' %in% colnames(filteredLungDeaths())){
        p <- dySeries(p, 'fdeaths', 'Female Deaths')
      }

      p

    })

    dygraphOutput('plot1')
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.