0

Как добавить кнопкой данные на график?

Пример данных в 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

Данные такого же типа и структуры хочу добавить с помощью кнопки

Пример кода:

ui <- fluidPage(
titlePanel("Пример"),
sidebarLayout(
sidebarPanel(
actionButton("shux",
               label = "Добавить")
    ),
mainPanel(
  highchartOutput("hcontainer1", height = "800px")
         )
            )
             )
server = function(input, output, session) {

   output$hcontainer1 <- renderHighchart({
     hc <- highchart() %>%

     hc_add_series( name = 'Значения',
                 data = DF1,
                 hcaes(x= Date, y = Value),
                 type = 'line')

    })}
 shinyApp(ui = ui, server = server)

Пытался добавить вот так:

 observe({
  input$shux
  hc_add_series( name = 'Значения 2',
                 data = DF2,
                 hcaes(x= Date, y = Value),
                 type = 'line')
})

Но не получилось

1 ответ 1

0
library(highcharter)
library(shiny)

DF1 <- data.frame(
    Date = seq.Date(as.Date("2018-01-01"), as.Date("2018-02-01"), length.out = 15),
    Value = sample(1:100, size = 15, replace = TRUE)
)

DF2 <- data.frame(
    Date = seq.Date(as.Date("2018-01-01"), as.Date("2018-02-01"), length.out = 15),
    Value = sample(1:100, size = 15, replace = TRUE)
)

ui <- fluidPage(
    titlePanel("Пример"),
    sidebarLayout(
        sidebarPanel(
            actionButton("shux",
                         label = "Добавить")
        ),
        mainPanel(
            highchartOutput("hcontainer1", height = "800px")
        )
    )
)

server = function(input, output, session) {

    output$hcontainer1 <- renderHighchart({
        hc <- highchart() %>%
            hc_add_series( name = 'Значения',
                           data = DF1,
                           hcaes(x= Date, y = Value),
                           type = 'line')

        if (input$shux) {
            hc <- hc %>%
                hc_add_series( name = 'Значения 2',
                               data = DF2,
                               hcaes(x= Date, y = Value),
                               type = 'line')
        }

        hc
    })
}

shinyApp(ui = ui, server = server)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.