3

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?

DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81
  • 1
    Is this for an Rmarkdown? – Johan Rosa May 08 '23 at 21:42
  • 1
    Since R can't talk to itself without an interpreter, you can't have the graph change without something other than R. (That's how Shiny works.) Since HC is based on `htmlwidgets`, you could use the tools extended with that library to make this is happen. Is that something you're interested in? – Kat May 08 '23 at 22:44
  • 1
    I thought more about this. You wanted to avoid any JS and if you're willing to forgo the dropdown, you could add the two remaining series (the female and male columns) with `visible = F`. Then you could use the legend as your selection tool, any combination of series selections can be made. (All you have to do is click on the legend entry to show/hide the series.) – Kat May 09 '23 at 12:11
  • Hey Kat! that's a good idea. I should try that. But yeah HTMLwidgets , as far as I've seen, only offer very limited interactivity, like say a pop-up on hover, etc. I haven't seen any filtering abilities with it. I'm currently using highcharter via Rmarkdown so may look into your suggestion to show/hide series.. – DiamondJoe12 May 09 '23 at 15:25
  • this is a good idea but I don't think its possible without shiny - the reason being that there has to be some sort backend to render the plot dynamically; in this case it would have t be some sort of javascript and not R. – Ahdee May 13 '23 at 23:36
  • Would you consider plotly dropdown as an option? – M-- May 14 '23 at 04:34
  • If this is an acceptable approach, I can post an answer: https://i.stack.imgur.com/KY1rQ.png – M-- May 14 '23 at 05:10
  • Interested in plotly drop-down, but I’m using highcharter so a solution using that package is preferable .. – DiamondJoe12 May 16 '23 at 20:11
  • Your example is intriguing though… I’m curious how to implement these drop downs! – DiamondJoe12 May 16 '23 at 20:13

0 Answers0