0

我有一个带有许多线的绘图图,我使用 add_trace 手动添加。我希望能够通过选择复选框来选择图表上出现的线条。我还希望能够让复选框代表几个变量。例如,选中名为“Choice A”的复选框会返回 varone 和 vartwo 的行。

我觉得我已经尝试了一切,但没有任何运气。我尝试了各种反应函数,但无法让它们工作,因为我的变量在列中,并且每个跟踪选择一个单独的列,所以我没有在下面的代码中包含反应性,因为我无法得到它工作。帮助将不胜感激!

library(shiny)
library(plotly)
library(shinythemes)

 mydataset <-data.frame( date  = c("12/01/2016", "12/02/2016",  "12/03/2016"),
              varone = c(60, 65, 80),
              vartwo = c(90, 45, 100),
              varthree = c(15, 120,201))


 ui <- shinyUI(
  navbarPage(theme=shinytheme("journal"), "My Dashboard",
  tabPanel("Data",
    fluidPage(
      fluidRow(
        column(2, dateRangeInput("daterange1", label = h5("Date Range:"),
                                 start = "2001-01-01",
                                 end   = "2010-12-31"),
               br(),
               checkboxGroupInput("mydataset", label = h5("Variables:"),
                                  c("Choice A" = "varone",
                                    "Choice B" = "vartwo",
                                    "Choice C" = "varthree"))),#close column
        column(10, plotlyOutput("plot"))
      )))))


 server <- function(input, output) { 
 output$plot <- renderPlotly({
 plot_ly(x = mydataset$date, y = mydataset$varone, name = 'varone', type = 'scatter', mode = 'lines') %>%
  add_trace(x = mydataset$date, y = mydataset$vartwo, name = 'vartwo', mode = 'lines+markers')%>%
  add_trace(x = mydataset$date, y = mydataset$varthree, name = 'varthree', mode = 'lines+markers')
  })}

 shinyApp(ui ,server)
4

1 回答 1

0

像这样的东西对你有用吗?

但首先我建议你将你的数据集从宽改成长。对于您的每个选择,您将能够按组拥有它们。

library(reshape2)

mydataset_long <- melt(mydataset, id.vars=c("date"))

ui <- shinyUI(
  navbarPage(theme=shinytheme("journal"), "My Dashboard",
             tabPanel("Data",
                      fluidPage(
                        fluidRow(
                          column(2, dateRangeInput("daterange1", label = h5("Date Range:"),
                                                   start = "2001-01-01",
                                                   end   = "2010-12-31"),
                                 br(),
                                 selectInput("mychoice", "Variables",
                                                    c("Choice A",
                                                      "Choice B",
                                                      "Choice C"))),#close column
                          column(10, plotlyOutput("myPlot"))
                        )))))


server <- function(input, output) { 
  output$myPlot = renderPlotly({

    if(identical(input$mychoice, "Choice A")) {
      mydataset_long_s <- mydataset_long %>% filter(variable == "varone")
      plot_ly(data=mydataset_long_s, x=~as.Date(date), y=~value, line = list(shape="linear"))
    } else {
      if(identical(input$mychoice, "Choice B")) {
        mydataset_long_s <- mydataset_long %>% filter(variable == "vartwo")
      plot_ly(data=mydataset_long_s, x=~as.Date(date), y=~value, line = list(shape="linear"))
    }  else {
        mydataset_long_s <- mydataset_long %>% filter(variable == "varthree")
        plot_ly(data=mydataset_long_s, x=~as.Date(date), y=~value, line = list(shape="linear"))
      }
    } 

})
  }

shinyApp(ui, server)
于 2017-01-23T13:11:06.460 回答