Search code examples
rshinyr-highcharter

Arguments are not named in hc_add_series


I'm trying to create a dashboard where a state can be selected and the graph is updated by that selection, but I get this error:

'Warning: Error in : 'df', 'hcaes(x = date, y = injured)' arguments are not named in hc_add_series [No stack trace available]'

library(tidyverse)
library(shiny)
library(shinydashboard)
library(highcharter)

ui <- dashboardPage(dashboardHeader(title = 'Test Dashboard'),
                    dashboardSidebar(),
                    dashboardBody(fluidPage(selectInput('select',
                                                        label = 'States',
                                                        choices = unique(opts),
                                                        selected = 'Alabama'),
                                            box(title = "Stock",
                                                status = "primary",
                                                solidHeader = TRUE,
                                                collapsible = TRUE,
                                                highchartOutput('plot')))))

server <- function(input,
                   output) {
  output$plot <- renderHighchart({
    df <- reactive({
      df <- massShooting2018.order %>%
        filter(state %in% input$select) %>%
        group_by(date) %>%
        summarise(
          dead = sum(dead),
          injured = (sum(injured)),
          total = sum(total)
        )
    })


    highchart(type = "stock") %>%
      hc_chart("line",
        name = "base",
        hcaes(x = date)
      ) %>%
      hc_add_series(df,
        name = "Total",
        type = "line",
        hcaes(
          x = date,
          y = total
        )
      ) %>%
      hc_add_series(df,
        name = "Dead",
        type = "line",
        hcaes(
          x = date,
          y = dead
        )
      ) %>%
      hc_add_series(df,
        name = "Injured",
        type = "line",
        hcaes(
          x = date,
          y = injured
        )
      ) %>%
      hc_tooltip(
        crosshairs = TRUE,
        shared = TRUE,
        borderWidth = 2,
        table = TRUE
      )
  })
}

shinyApp(ui, server)

DataSource

using the previous dataset:

massShooting2018 <- read.csv('shootings_2018.csv')

massShooting2018.clean <- massShooting2018 %>%
  clean_names() %>%
  mutate(date = dmy(date))

massShooting2018.order <- massShooting2018.clean %>%
  group_by(date, state) %>%
  summarise(dead = sum(dead),
            injured = sum(injured),
            total = sum(total),
            description, .groups = 'drop')

opts <- massShooting2018.order %>%
  sample_frac(1) %>%
  select(state) %>%
  arrange(state)

Solution

  • The call of df after reactive function should be df():

    library(tidyverse)
    library(shiny)
    library(shinydashboard)
    library(highcharter)
    library(janitor)
    library(lubridate)
    
    massShooting2018 <- read.csv('shootings_2018.csv')
    
    massShooting2018.clean <- massShooting2018 %>%
      clean_names() %>%
      mutate(date = dmy(date))
    
    massShooting2018.order <- massShooting2018.clean %>%
      group_by(date, state) %>%
      summarise(dead = sum(dead),
                injured = sum(injured),
                total = sum(total),
                description, .groups = 'drop')
    
    opts <- massShooting2018.order %>%
      sample_frac(1) %>%
      select(state) %>%
      arrange(state)
    
    
    
    ui <- dashboardPage(dashboardHeader(title = 'Test Dashboard'),
                        dashboardSidebar(),
                        dashboardBody(fluidPage(selectInput('select',
                                                            label = 'States',
                                                            choices = unique(opts),
                                                            selected = 'Alabama'),
                                                box(title = "Stock",
                                                    status = "primary",
                                                    solidHeader = TRUE,
                                                    collapsible = TRUE,
                                                    highchartOutput('plot')))))
    
    server <- function(input, 
                       output) 
    {
      output$plot <- renderHighchart({
        
        df <- reactive({df <- massShooting2018.order %>%
          filter(state %in% input$select) %>%
          group_by(date) %>%
          summarise(dead = sum(dead),
                    injured = (sum(injured)),
                    total = sum(total))})
        
        
        highchart(type = 'stock') %>%
          hc_chart('line',
                   name = 'base',
                   hcaes(x = date)) %>%
          hc_add_series(df(),
                        name = 'Total',
                        type = 'line',
                        hcaes(x = date,
                              y = total)) %>%
          hc_add_series(df(),
                        name = 'Dead',
                        type = 'line',
                        hcaes(x = date,
                              y = dead)) %>%
          hc_add_series(df(),
                        name = 'Injured',
                        type = 'line',
                        hcaes(x = date,
                              y = injured)) %>%
          hc_tooltip(crosshairs = TRUE,
                     shared = TRUE, 
                     borderWidth = 2,
                     table = TRUE)})
      
    }
    
    shinyApp(ui, server)
    

    enter image description here