Filter two tables with crosstalk

后端 未结 2 1996
广开言路
广开言路 2021-02-03 13:34

I am creating a Flexdashboard in R. I want the dashboard to contains both a table and a series of visualizations, that would be filtered through inputs.

As I need to de

2条回答
  •  天涯浪人
    2021-02-03 14:03

    Ah I recently ran into this too, there is another argument to SharedData$new(..., group = )! The group argument seems to do the trick. I found out by accident when I had two dataframes and used the group =.

    If you make a sharedData object, it will include

    • a dataframe
    • a key to select rows by - preferably unique, but not necessarily.
    • a group name

    What I think happens is that crosstalk filters the sharedData by the key - for all sharedData objects in the same group! So as long as two dataframes use the same key, you should be able to filter them together in one group.

    This should work for your example.

    ---
    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)
    ```
    

    I spent some time on this by trying to extract data from plot_ly() using plotly_data() without luck until I figured out the answer. That's why there's some very simple plots with plotly.

提交回复
热议问题