首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shiny:根据输入值将时间序列添加/删除到dygraphs

shiny:根据输入值将时间序列添加/删除到dygraphs
EN

Stack Overflow用户
提问于 2019-04-23 22:15:16
回答 1查看 369关注 0票数 1

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-23 22:48:20

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55813276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档