Plotly button (updatemenus) not resetting when using crosstalk filter_select in R

102 Views Asked by At

I have some time series data for several cities in log and linear scale. I want to be able to filter the city using crosstalk filter_select. I also want to be able to toggle between log and linear scales which i have added using plotly updatemenus button option. Log should be the active option. This all works fine, however, the problem occurs when I am in linear mode (via button) and then select a new city via filter, the data is displayed in log but the button doesn't switch back to log - it stays in "linear". How can I reset the button to "log" when I select a new city via the filter? (My output is an html doc written in R Markdown).

Here is some reproducible code:

library(plotly)
library(crosstalk)

#sample data
data <- data.frame(
  date = rep(as.Date(c("2023-01-01", "2023-01-02", "2023-01-03")), times = 3),
  city = rep(c("A", "B", "C"), each = 3),
  value = c(100, 150, 200, 120, 180, 250, 90, 140, 190),
  value_log = c(2, 2.18, 2.3, 2.08, 2.26, 2.4, 1.95, 2.15, 2.28)
)

data <- SharedData$new(data)

plot <- plot_ly(data) |> 
  add_trace(x = ~date, y = ~value_log, type = "scatter", mode = "lines") %>%
  add_trace(x = ~date, y = ~value, type = "scatter", mode = "lines", visible = FALSE) %>%
  layout(updatemenus = list(list(
    x = 1,
    y = 1.1,
    active = 0,
    showactive = TRUE, 
    buttons= list(
      list(label = 'Log',
           method = 'update',
           args = list(list(visible = c(T,F)),
                       list(yaxis = list(type = 'Log',
                                         title = "Log scale"))
           )),
      list(label = 'Linear',
           method = 'update',
           args = list(list(visible = c(F,T)),
                       list(yaxis = list(type = 'Linear',
                                         title = "Linear scale"))
           ))))))

#filter
filter = bscols(
  filter_select("id", "City:", data, ~city, multiple = F),
  plot,
  widths = c(12,12)
)

filter

I have also added some javascript to remove the "all" selection, so maybe there is an option to include something here? I am not good with javascript...

$(document).ready(function remove_all_option() {
  document.getElementById("id").getElementsByClassName("selectized")[0].selectize.removeOption("");
});

Thank you for your help!

0

There are 0 best solutions below