0

我目前正在开发一款没有光泽的 R-flexdashboard 产品。目标是有一个情节,不同公司的不同产品的价格演变。我在顶部有一个情节线图和 2 个串扰过滤器,可让我按公司和/或产品进行过滤。这部分工作正常。

但是,如果我几天没有价格或者当时没有产品,我希望停止生产线。为此,我知道我必须在我的数据集中创建价格为 NA 或 NaN 的行,然后设置 plotly 参数“connectgaps = FALSE”。这也没有问题。

我遇到的问题是,如果我有给定产品的 NA/NaN,该产品将出现在我的情节图例中,即使它不应该因为过滤器并且不会消失。

这是一个示例,我选择了公司 A,只显示了他们的 2 个产品。但是,产品 B2 和 C1 出现在图例中......

问题

这是一些代码来说明我的问题。


    ---
    title: "Stack example"
    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("10.11.2021", "10.11.2021", "10.11.2021", "10.11.2021", "10.11.2021", "10.11.2021", "11.11.2021", "11.11.2021", 
                    "11.11.2021", "11.11.2021", "11.11.2021", "11.11.2021", "12.11.2021", "12.11.2021", "12.11.2021", "12.11.2021", 
                    "12.11.2021", "12.11.2021", "13.11.2021", "13.11.2021", "13.11.2021", "13.11.2021", "13.11.2021", "13.11.2021", 
                    "14.11.2021", "14.11.2021", "14.11.2021", "14.11.2021", "14.11.2021", "14.11.2021", "15.11.2021", "15.11.2021", 
                    "15.11.2021", "15.11.2021", "15.11.2021", "15.11.2021"),
        company = c("A", "A", "B", "B", "B", "C", "A", "A", "B", "B", "B", "C", "A", "A", "B", "B", "B", "C", "A", "A", "B", "B", 
                    "B", "C", "A", "A", "B", "B", "B", "C", "A", "A", "B", "B", "B", "C"),
        product = c("A1", "A2", "B1", "B2", "B3", "C1", "A1", "A2", "B1", "B2", "B3", "C1", "A1", "A2", "B1", "B2", "B3", "C1",
                    "A1", "A2", "B1", "B2", "B3", "C1", "A1", "A2", "B1", "B2", "B3", "C1", "A1", "A2", "B1", "B2", "B3", "C1"),
        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)
      ) %>% 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 %>% 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)) %>%
       layout(hovermode = 'x',
              title = list(text  = 'Market prices'),
              xaxis = list(title = 'Date'),
              yaxis = list(title = 'Price',
                           range = list(0, 60),
                           autorange  = FALSE,
                           fixedrange = FALSE))

我不知道如何解决这个问题,因为这对我来说似乎是一个错误。我考虑尝试添加一些 html/javascript 来隐藏图例中不应在每次使用 2 个过滤器之一时显示的元素,但我不知道从哪里开始,不知道这种交互性是否在单个 .html 文件上可行,目前不知道任何 javascript。

这个想法是在伪代码中执行类似 -->

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.

任何建议将不胜感激。

谢谢您的帮助 !

4

0 回答 0