【问题标题】:shiny: add/remove time-series to dygraphs upon input values闪亮:根据输入值向 dygraphs 添加/删除时间序列
【发布时间】:2019-04-23 23:22:03
【问题描述】:

我正在构建一个闪亮的应用程序,它将在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】:

    我建议根据用户选择动态过滤数据,而不是从图中动态添加/删除跟踪:

    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)
    

    【讨论】:

    • 刚刚试玩了一下,发现这个解决方案的效果不如我原来的问题中建议的那样好,因为我需要指定 dySeries 选项(例如,分配不同的 y 轴刻度,线条颜色等),所以我需要更多地控制和理解所选择的内容。有什么方法可以让我的原始代码工作?
    • 我编辑了答案以设置系列标签,就像您在示例代码中所做的那样。您仍然需要将 dygraph 的 data 参数同步到用户选择。
    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 2021-04-18
    • 2016-12-18
    • 2017-05-11
    • 2017-05-05
    • 2013-10-07
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多