блестящий: добавить / удалить временные ряды на графиках при вводе значений - PullRequest
1 голос
/ 23 апреля 2019

Я создаю блестящее приложение, которое будет отображать в 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)

1 Ответ

1 голос
/ 23 апреля 2019

Я бы предложил динамически фильтровать данные на основе выбора пользователя, а не динамически добавлять / удалять следы с графика:

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)
...