Issue with crosstalk and plotly: filter_checkbox not working correctly and ignoring values

101 Views Asked by At

I want to create a graph that shows the market share for three different shops. But I want to be able to select which shops I want to include and to have the market share then added up for the selected shops. In theory, this works with plotly and crosstalk, but for some reason there's a really strange error. When selecting shop 'B' first it works, but then I select shop 'C' as well and the value doesn't change although it should.

Here's my code:

suppressPackageStartupMessages({
  library(data.table)
  library(plotly)
  library(crosstalk)
})

data <- data.table(Month = rep(as.Date("2022-01-01"), 3), 
                   Shop = c("A", "B", "C"),
                   Share = c(0.005, 0.02, 0.4))

data <- SharedData$new(data)

filter <- filter_checkbox("shop", "Select Shop:", data, group = ~Shop)

plot <- plot_ly(
  data, x = ~Month, y = ~Share, type = "bar",
  transforms = list(
    list(
      type = "aggregate",
      group = ~interaction(Month, Shop),
      aggregations = list(list(target = "y", func = "sum", enabled = TRUE))
    )
  )
)

bscols(filter, plot, widths = c(3, 9))

I need the 'transforms' argument in there because I also want the hovertext to show the summarized value. But I'm open to suggestions as to how this could also be solved in another way. Thanks!

0

There are 0 best solutions below