根据checkboxinput为闪亮选择R图表图形的跟踪

时间:2017-01-22 21:24:42

标签: r shiny plotly reactive

我有一个情节图,我用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)

1 个答案:

答案 0 :(得分: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)