我正在R中创建一个Flexdashboard。我希望该仪表板既包含一个表又包含一系列可视化效果,这些可视化效果可以通过输入进行过滤。

由于我需要在本地交付仪表板(没有在后台运行服务器),因此我无法使用Shiny,因此我依赖串扰。

我知道相声包在前端提供的功能有限。例如,文档说您不能聚合SharedData对象。

但是,我不清楚是否可以使用相同的输入来过滤两个不同的数据帧

例如,假设我有:

  • 数据框一:包含原始数据

    df1 “Mark”),类别=“factor”),hp = c(250,120,250,100,110),
    car = structure(c(2L,2L,2L,1L,1L),.Label = c(“benz”,
    “bmw”),class =“factor”),id = structure(1:5,.Label = c(“car1”,
    “car2”,“car3”,“car4”,“car5”),class =“factor”)),.names = c(“owner”,
    “hp”,“car”,“id”),row.names = c(NA,-5L),class =“data.frame”)
  • Datafrane二:包含聚合数据

    df2
  • “bmw”),class =“factor”),owner = structure(c(1L,1L,2L,2L
  • ),.Label = c(“John”,“Mark”),class =“factor”),freq = c(0L,
  • 1L,2L,2L)),.names = c(“car”,“owner”,“freq”),row.names = c(NA,
  • -4L),class =“data.frame”)

  • 这两个数据框包含具有相同值的列-car和所有者。以及其他列。

    我可以创建两个不同的对象:
    library(crosstalk)
    shared_df1 <- SharedData$new(df1)
    shared_df2 <- SharedData$new(df2)
    

    然后:
    filter_select("owner", "Car owner:", shared_df1, ~ owner)
    filter_select("owner", "Car owner:", shared_df2, ~ owner)
    

    但是,这意味着用户将需要两次填充实质上相同的输入。另外,如果表很大,这会使使用仪表板所需的内存大小增加一倍。

    是否可以解决串扰中的此问题?

    最佳答案

    嗯,我最近也遇到了这个问题,SharedData$new(..., group = )还有另一个参数!组参数似乎可以解决问题。当我有两个数据框并使用group =时,我偶然发现了它。

    如果您创建一个sharedData对象,它将包含

  • 数据帧
  • 一个选择行的键-最好是唯一的,但不一定。
  • 群组名称

  • 我认为发生的情况是串扰通过键过滤了sharedData-对于同一组中的所有sharedData对象!因此,只要两个数据帧使用相同的 key ,您就应该能够将它们一起过滤到一个组中。

    这应该适合您的示例。
    ---
    title: "blabla"
    output:
       flexdashboard::flex_dashboard:
       orientation: rows
       social: menu
       source_code: embed
       theme: cerulean
    ---
    
    ```{r}
    library(plotly)
    library(crosstalk)
    library(tidyverse)
    ```
    
    ```{r Make dataset}
    df1 <- structure(list(owner = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("John", "Mark"), class = "factor"), hp = c(250, 120, 250, 100, 110), car = structure(c(2L, 2L, 2L, 1L, 1L), .Label = c("benz", "bmw"), class = "factor"), id = structure(1:5, .Label = c("car1", "car2", "car3", "car4", "car5"), class = "factor")), .Names = c("owner", "hp", "car", "id"), row.names = c(NA, -5L), class = "data.frame")
    
    df2 <- structure(list(car = structure(c(1L, 2L, 1L, 2L), .Label = c("benz",
    "bmw"), class = "factor"), owner = structure(c(1L, 1L, 2L, 2L
    ), .Label = c("John", "Mark"), class = "factor"), freq = c(0L,
    1L, 2L, 2L)), .Names = c("car", "owner", "freq"), row.names = c(NA,
    -4L), class = "data.frame")
    ```
    
    #
    
    ##
    
    ### Filters
    
    ```{r}
    library(crosstalk)
    # Notice the 'group = ' argument - this does the trick!
    shared_df1 <- SharedData$new(df1, ~owner, group = "Choose owner")
    shared_df2 <- SharedData$new(df2, ~owner, group = "Choose owner")
    
    filter_select("owner", "Car owner:", shared_df1, ~owner)
    # You don't need this second filter now
    # filter_select("owner", "Car owner:", shared_df2, ~ owner)
    ```
    
    ### Plot1 with plotly
    
    ```{r}
    plot_ly(shared_df1, x = ~id, y = ~hp, color = ~owner) %>% add_markers() %>% highlight("plotly_click")
    ```
    
    ### Plots with plotly
    
    ```{r}
    plot_ly(shared_df2, x = ~owner, y = ~freq, color = ~car) %>% group_by(owner) %>% add_bars()
    ```
    
    ##
    
    ### Dataframe 1
    
    ```{r}
    DT::datatable(shared_df1)
    ```
    
    ### Dataframe 2
    
    ```{r}
    DT::datatable(shared_df2)
    ```
    

    我花了一些时间尝试通过使用plot_ly()plotly_data()提取数据而没有运气,直到我找出答案。这就是为什么存在一些带有plotly的非常简单的图的原因。

    关于r - 过滤带有串扰的两个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48581598/

    10-12 16:32