Select 基于闪亮复选框输入的 R 绘图跟踪
Select trace(s) for R plotly graph based on checkboxinput for shiny
我用 add_trace 手动添加了很多线条的绘图。我希望能够通过 select 复选框 select 哪些行出现在图表上。我还希望能够让复选框代表几个变量。例如,选中名为 "Choice A" returns 的复选框 varone 和 vartwo 的行。
我觉得我已经尝试了一切,但没有任何运气。我尝试了各种反应函数,但无法让它们工作,因为我的变量在列中,并且每个跟踪 selects 一个单独的列,所以我没有在下面的代码中包含反应性,因为我可以让它工作。帮助将不胜感激!
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)
我用 add_trace 手动添加了很多线条的绘图。我希望能够通过 select 复选框 select 哪些行出现在图表上。我还希望能够让复选框代表几个变量。例如,选中名为 "Choice A" returns 的复选框 varone 和 vartwo 的行。
我觉得我已经尝试了一切,但没有任何运气。我尝试了各种反应函数,但无法让它们工作,因为我的变量在列中,并且每个跟踪 selects 一个单独的列,所以我没有在下面的代码中包含反应性,因为我可以让它工作。帮助将不胜感激!
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)