Let's say I have the following data:
library(highcharter)
year <- c(2017, 2018, 2019, 2020, 2021)
black <- c(12, 15, 23, 16, 11)
asian <- c(3, 12, 63, 46, 21)
white <- c(5, 45, 44, 44, 4)
hispanic <- c(23, 25, 44, 16, 112)
male <- c(33, 33, 62, 66, 102)
female <- c(57, 15, 46, 46, 172)
df <- data.frame(year, black, asian, white, hispanic, male, female)
I want to be able to create a chart with highcharter that can update the data series displayed via user input (i.e. a dropdown with the options "show race" and "show gender", click of a button(s) etc.)
For example, the chart would default to this view:
highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(opposite = TRUE, gridLineWidth = .3, categories = df$year) %>%
hc_add_series(name="White",data = df$white) %>%
hc_add_series(name="Black",data = df$black) %>%
hc_add_series(name="Asian",data = df$asian) %>%
hc_add_series(name="Hispanic",data = df$hispanic)
But on user input, the chart would update or re-draw to this chart:
highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(opposite = TRUE, gridLineWidth = .3, categories = df$year) %>%
hc_add_series(name="female",data = df$female) %>%
hc_add_series(name="male",data = df$male)
I know this is easy enough to do in Shiny or highcharts in javascript - I'm looking for a solution in pure R / RMarkdown. In other words, adding user selection options via dropdown or buttons to change the data displayed in a highcharter chart. Is this possible?