Interactive chart with r2d3 and shiny app

202 Views Asked by At

I am trying to add an interactive bar chart to my Shiny app, using r2d3 package. I have a dataset like this dummy sample containing date, id and motion column just for reference:

df <- data.frame(stringsAsFactors=FALSE,
          date = c("2019-08-06", "2019-08-07", "2019-08-08", "2019-08-09",
                   "2019-08-10", "2019-08-06", "2019-08-07", "2019-08-08",
                   "2019-08-09", "2019-08-10"),
   id = c("18100410-1", "18100410-1", "18100410-1", "18100410-1",
                   "18100410-1", "18100496-1", "18100496-1", "18100496-1",
                   "18100496-1", "18100496-1"),
       useage = c(16.43, 15.78, 14.43, 15.68, 15.5, 17.08, 0, 0, 14.78, 14.57)
) %>% 
  mutate(date = readr::parse_date(date, format = "%Y-%m-%d"))

My aim is to have an app that user can select each id from the right menu and then we have a bar chart shows usage hours per day as a bar chart ( here is dummy example).

enter image description here

I have tried this for my bar plot chart section, but obviously, I am missing something here. Any help would be greatly appreciated


bar_graphD3=reactive({
  grouped <- ifelse(input$id != "ALL", expr(date), expr(id), expr(usage))

  data <- sel_data() %>%
    group_by(!!grouped) %>%
    collect(usage) %>%
    mutate(
      y = n,
      x = !!grouped
    ) %>%
    select(x, y)

  data <- data %>%
    mutate(label = x)

  r2d3(data, "bar_plot_sample.js")
})
0

There are 0 best solutions below