如何在Highcharts制作的情节中添加新系列?我想通过点击Shiny中创建的名为“shux”的按钮来实现。
示例数据DF1
:
DF1 <- data.frame(
Date = c(as.Date("2017-12-01"), as.Date("2017-12-06"), as.Date("2017-12-11")),
Value = c(95,97,96)
)
结构:
Date Value
1 2017-12-01 95
2 2017-12-06 97
3 2017-12-11 96
我想在图中添加类似的数据DF2
(带有2列的数据框)。
我的代码:
library(shiny)
library(highcharter)
ui <- fluidPage(
titlePanel("Example"),
sidebarLayout(
sidebarPanel(
actionButton("shux", label = "Add")
),
mainPanel(
highchartOutput("hcontainer1", height = "800px")
)
)
)
server = function(input, output, session) {
output$hcontainer1 <- renderHighchart({
hc <- highchart() %>%
hc_add_series(
name = 'first',
data = DF1,
hcaes(x= Date, y = Value),
type = 'line'
)
})
}
shinyApp(ui = ui, server = server)
我试过了:
observe({
input$shux
hc_add_series(
name = 'second',
data = DF2,
hcaes(x= Date, y = Value),
type = 'line'
)
})
但它没有用。我应该使用什么方法?
答案 0 :(得分:1)
这样的事情请注意,当您使用renderHighchart
时,图表将被重新绘制。
library(shiny)
library(highcharter)
DF1 <- data.frame(
Date = c(as.Date("2017-12-01"), as.Date("2017-12-06"), as.Date("2017-12-11")),
Value = c(95,97,96)
)
DF2 <- data.frame(
Date = c(as.Date("2017-12-01"), as.Date("2017-12-06"), as.Date("2017-12-11")),
Value = c(75,77,76)
)
ui <- fluidPage(
titlePanel("Example"),
sidebarLayout(
sidebarPanel(
actionButton("shux",label = "Add")
),
mainPanel(
highchartOutput("hcontainer1", height = "800px")
)
)
)
server = function(input, output, session) {
hc <- reactive({
highchart() %>% hc_add_series(name = 'first',id = "1",data = DF1,hcaes(x= Date, y = Value),type = 'line')
})
hcadd <- eventReactive(input$shux,{
hc() %>% hc_add_series(name = 'second',id = "2",data = DF2,hcaes(x= Date, y = Value),type = 'line')
})
output$hcontainer1 <- renderHighchart({
if(input$shux == 0){
return(hc())
}
hcadd()
})
}
shinyApp(ui = ui, server = server)