Shiny 闪亮:根据输入值向动态图添加/删除时间序列
我正在构建一个闪亮的应用程序,它将在基本数据集中显示Shiny 闪亮:根据输入值向动态图添加/删除时间序列,shiny,shinydashboard,dygraphs,Shiny,Shinydashboard,Dygraphs,我正在构建一个闪亮的应用程序,它将在基本数据集中显示dygraphs,然后在选中复选框输入时提供添加新时间序列的选项。但是,正如我现在编写的那样,我被原始数据集“卡住”,无法添加/删除新内容。任何关于如何解决此问题的提示都非常欢迎,谢谢 library(shinydashboard) library(dygraphs) library(dplyr) ui <-dashboardPage( dashboardHeader(), dashboardSidebar(), dash
dygraphs
,然后在选中复选框输入时提供添加新时间序列的选项。但是,正如我现在编写的那样,我被原始数据集“卡住”,无法添加/删除新内容。任何关于如何解决此问题的提示都非常欢迎,谢谢
library(shinydashboard)
library(dygraphs)
library(dplyr)
ui <-dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
checkboxGroupInput(inputId = 'options',
label = 'Choose your plot(s)',
choices = list("mdeaths" = 1,
"ldeaths" = 2)
),
uiOutput("Ui1")
)
)
server <- function(input, output, session) {
output$Ui1 <- renderUI({
output$plot1 <- renderDygraph({
final_ts <- ldeaths
p <- dygraph(final_ts, main = 'Main plot') %>%
dygraphs::dyRangeSelector()
if(1 %in% input$options) {
final_ts <- cbind(final_ts, mdeaths)
p <- p %>%
dySeries('mdeaths', 'Male Deaths')
} else if(2 %in% input$options) {
final_ts <- cbind(final_ts, fdeaths)
p <- p %>%
dySeries('fdeaths', 'Female Deaths')
}
p
})
dygraphOutput('plot1')
})
}
shinyApp(ui, server)
库(ShinydaShashboard)
图书馆(动态图)
图书馆(dplyr)
ui我建议根据用户选择动态过滤数据,而不是从绘图中动态添加/删除跟踪:
library(shinydashboard)
library(shinyjs)
library(dygraphs)
library(dplyr)
lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs(),
selectizeInput(
inputId = "options",
label = "Choose your trace(s)",
choices = colnames(lungDeaths),
selected = colnames(lungDeaths)[1],
multiple = TRUE,
options = list('plugins' = list('remove_button'))
),
uiOutput("Ui1")
)
)
server <- function(input, output, session) {
output$Ui1 <- renderUI({
filteredLungDeaths <- reactive({
lungDeaths[, input$options]
})
output$plot1 <- renderDygraph({
p <- dygraph(filteredLungDeaths(), main = 'Main plot') %>%
dygraphs::dyRangeSelector()
if('mdeaths' %in% colnames(filteredLungDeaths())){
p <- dySeries(p, 'mdeaths', 'Male Deaths')
}
if('fdeaths' %in% colnames(filteredLungDeaths())){
p <- dySeries(p, 'fdeaths', 'Female Deaths')
}
p
})
dygraphOutput('plot1')
})
}
shinyApp(ui, server)
库(ShinydaShashboard)
图书馆(shinyjs)
图书馆(动态图)
图书馆(dplyr)
Lungdeath刚刚玩了一番,意识到这个解决方案的效果不如我在原始问题中建议的那样好,因为我需要为每个系列指定dySeries选项(例如,分配不同的y轴比例、线颜色等),所以我需要更多地控制和理解所选择的内容。有什么方法可以让我的原始代码工作吗?我编辑了我的答案来设置序列标签,就像您在示例代码中所做的那样。您仍然需要将dygraph的数据
参数与用户选择同步。