title: R Flexdashboard Plotly Filter legend problem
body: <p>I'm currently working on a R-flexdashboard product without shiny. The goal is to have on a plot, the price evolution for different products of different companies. I have a plotly line plot and 2 crosstalks filters at the top that allow me to filter by company and/or product. This part works fine.</p>
  <p>However, I want to have the lines discontinued if I don't have the prices for a few day or if a product was not available at the time. To do this, I understood that I have to create lines in my data set where the price is NA or NaN and then set the plotly parameter &quot;connectgaps = FALSE&quot;. This also works without problem.</p>
  <p>The problem I encounter is that if I have a NA/NaN for a given product, this product will appear in the legend of my plot even though it shouldn't because of the filters and won't go away.</p>
  <p>Here is an example where I select the company A and only their 2 products are displayed. However, the products B2 and C1 appear in the legend...</p>
  <p><a href="https://i.stack.imgur.com/qF7xh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qF7xh.png" alt="Problem" /></a></p>
  <p>Here is some code to illustrate my problem.</p>
  <pre><code>
      ---
      title: &quot;Stack example&quot;
      output: 
        flexdashboard::flex_dashboard:
          orientation: columns
          vertical_layout: fill
      ---
      
      Overview {data-orientation=columns}
      =======================================================================
      
      column{.tabset}
      -----------------------------------------------------------------------
      
      ### Example
      
      ```{r}
      library(flexdashboard)
      library(tidyverse)
      library(lubridate)
      library(plotly)
      library(crosstalk)
      library(RColorBrewer)
      
      my_table = 
        tibble(
          date    = c(&quot;10.11.2021&quot;, &quot;10.11.2021&quot;, &quot;10.11.2021&quot;, &quot;10.11.2021&quot;, &quot;10.11.2021&quot;, &quot;10.11.2021&quot;, &quot;11.11.2021&quot;, &quot;11.11.2021&quot;, 
                      &quot;11.11.2021&quot;, &quot;11.11.2021&quot;, &quot;11.11.2021&quot;, &quot;11.11.2021&quot;, &quot;12.11.2021&quot;, &quot;12.11.2021&quot;, &quot;12.11.2021&quot;, &quot;12.11.2021&quot;, 
                      &quot;12.11.2021&quot;, &quot;12.11.2021&quot;, &quot;13.11.2021&quot;, &quot;13.11.2021&quot;, &quot;13.11.2021&quot;, &quot;13.11.2021&quot;, &quot;13.11.2021&quot;, &quot;13.11.2021&quot;, 
                      &quot;14.11.2021&quot;, &quot;14.11.2021&quot;, &quot;14.11.2021&quot;, &quot;14.11.2021&quot;, &quot;14.11.2021&quot;, &quot;14.11.2021&quot;, &quot;15.11.2021&quot;, &quot;15.11.2021&quot;, 
                      &quot;15.11.2021&quot;, &quot;15.11.2021&quot;, &quot;15.11.2021&quot;, &quot;15.11.2021&quot;),
          company = c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, 
                      &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;),
          product = c(&quot;A1&quot;, &quot;A2&quot;, &quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;, &quot;C1&quot;, &quot;A1&quot;, &quot;A2&quot;, &quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;, &quot;C1&quot;, &quot;A1&quot;, &quot;A2&quot;, &quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;, &quot;C1&quot;,
                      &quot;A1&quot;, &quot;A2&quot;, &quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;, &quot;C1&quot;, &quot;A1&quot;, &quot;A2&quot;, &quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;, &quot;C1&quot;, &quot;A1&quot;, &quot;A2&quot;, &quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;, &quot;C1&quot;),
          price   = c(5, 10, 15, 20, 25, 45, 25, 10, 15, 20, 25, 45, 15, 15, 20, 25, 30, 50, 15, 15, 20, NaN, 30, NaN, 10, 10, 15, 20,
                      25, 45, 10, 10, 15, 20, 25, 45)
        ) %&gt;% mutate(date = lubridate::dmy(date))
      
      
      a_palette = brewer.pal(9, 'Blues')[c(4,6)]
      b_palette = brewer.pal(9, 'Reds')[c(4,6,8)]
      c_palette = brewer.pal(9, 'Greens')[5]
      
      pal = c(a_palette, b_palette, c_palette)
      pal = setNames(pal, unique(my_table$product))
      
      my_table  = my_table %&gt;% arrange(date, product)
      share = SharedData$new(my_table)
      
      # -------------------------------------------------------------------------------------------------
      
      bscols(widths = c(6,6),
        filter_select(id         = 'company',
                      label      = 'Company',
                      sharedData = share,
                      group      = ~company,
                      multiple   = TRUE),
        filter_select(id         = 'product',
                      label      = 'Product',
                      sharedData = share,
                      group      = ~product,
                      multiple   = TRUE))
      
      plot_ly(share, 
              x           =  ~date, 
              y           =  ~price, 
              type        =  'scatter', 
              mode        =  'lines+markers',
              color       =  ~product,
              colors      =  pal,
              height      =  525,
              connectgaps =  FALSE,
              hoverlabel  =  list(namelength = - 1)) %&gt;%
         layout(hovermode = 'x',
                title = list(text  = 'Market prices'),
                xaxis = list(title = 'Date'),
                yaxis = list(title = 'Price',
                             range = list(0, 60),
                             autorange  = FALSE,
                             fixedrange = FALSE))
  
  </code></pre>
  <p>I have no idea how to fix this as it seems to be a bug to me. I considered trying to add some html/javascript to hide the elements in the legend that shouldn't be displayed every time one of the 2 filters is used but I have no idea where to start, don't know if this kind of interactivity is doable on a single .html file, and don't know any javascript at the moment.</p>
  <p>The idea would be to do in pseudocode something like --&gt;</p>
  <blockquote>
  <pre><code>When updating the filter values: 
     for (i in legends):
        if (string(legend.text) exists in company_filter.text)
           do nothing
        else
           set.value of some html/css attribute in order not to display this part of the legend.
  </code></pre>
  </blockquote>
  <p>Any advice would be highly appreciated.</p>
  <p>Thank you for your help !</p>