闪亮:单击按钮将新系列添加到Highcharts绘图

闪亮:单击按钮将新系列添加到Highcharts绘图,r,highcharts,shiny,R,Highcharts,Shiny,如何在Highcharts制作的绘图中添加新系列?我想通过点击名为“shux”的按钮来实现这一点,该按钮是在Shiny中创建的 示例数据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) ) 我想向绘图中添加类似的数据DF2(带有2列的数据框) 我的代码: library(shiny)

如何在Highcharts制作的绘图中添加新系列?我想通过点击名为“shux”的按钮来实现这一点,该按钮是在Shiny中创建的

示例数据
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)
) 
我想向绘图中添加类似的数据
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)

但它不起作用。我应该使用什么方法?

类似这样的操作,请注意,当您使用
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)
库(闪亮)
图书馆(高级特许)
DF1
observe({
  input$shux
  hc_add_series(
    name = 'second',
    data = DF2,
    hcaes(x= Date, y = Value),
    type = 'line'
  )
})
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)