根据复选框输入为闪亮选择 R 绘图图的迹线



我有一个绘图图,其中包含我用add_trace手动添加的许多行。我希望能够通过选中复选框来选择图表上显示的线条。我还希望能够让复选框表示几个变量。例如,选中名为"选择 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)
这样

的东西对你有用吗?

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

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)

相关内容

  • 没有找到相关文章

最新更新