我有一个带有许多线的绘图图,我使用 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)