Search code examples
rshinyr-highcharter

Plotting two columns using highchart in R shiny returns argument object is missing with no value


I tried to make interactive map using R shiny which will show plot of male and female citizens in some cities. Data frame sample is shown below.

df1 <- read.table(header = TRUE, text = "city,year,male,female,long,lat
                  A,2017,1038,876,35.54331,139.12333
                  A,2018,1281,911,35.54331,139.12333
                  B,2017,832,517,35.14189,140.664113
                  B,2018,914,589,35.14189,140.664113", sep = ",")
df2 <- df1

The interactive map is built by using leaflet package and if the city marker is clicked, a plot which built by highchart will be shown.

output$chart <- renderHighchart({

  df2 <- df1[df1$city == click_marker(),]
  hchart() %>%
    hc_add_series(df2, "column", hcaes(x = year, y = male, group = city, name = "Male")) %>%
    hc_add_series(df2, "column", hcaes(x = year, y = female, group = city, name = "Female")) %>%
    hc_xAxis(title = list(text = "Year")) %>%
    hc_yAxis(title = list(text = "Amount (Thousands)"))
})

highchartOutput('chart')

I expect the output is a plot that show amount of male and female in the city for each year given but the output that I got is "argument object is missing, with no default."


Solution

  • What if you change hchart to highchart and the names to lowercase? Apparently they have to be found in the data.frame.

    highchart() %>%
      hc_add_series(df2, "column", hcaes(x = year, y = male, group = city, name = "male")) %>%
      hc_add_series(df2, "column", hcaes(x = year, y = female, group = city, name = "female")) %>%
      hc_xAxis(title = list(text = "Year")) %>%
      hc_yAxis(title = list(text = "Amount (Thousands)"))