I am trying to replicate the functionality of an Excel pivot table using a Flexdashboard with crosstalk and DT. I need to be able to show both a data table and a summary table, and be able to apply the same filters on both tables at the same time.
The example below works to filter using one field, "manufacturer". What I can't seem to figure out is how to add another filter, like say "continent", to work on both tables.
---
title: "Filter 2 tables With Crosstalk"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
theme: cosmo
---
```{r setup, include=FALSE}
library(dplyr)
library(crosstalk)
car_data <- "manufacturer,model,displ,year,cyl,trans,drv,cty,hwy,fl,class,continent
audi,a4,1.8,1999,4,auto,f,18,29,p,compact,europe
audi,a4,1.8,1999,4,manual,f,21,29,p,compact,europe
audi,a4,2,2008,4,manual,f,20,31,p,compact,europe
audi,a4,2,2008,4,auto,f,21,30,p,compact,europe
audi,a4,2.8,1999,6,auto,f,16,26,p,compact,europe
chevrolet,malibu,2.4,1999,4,auto,f,19,27,r,midsize,america
chevrolet,malibu,2.4,2008,4,auto,f,22,30,r,midsize,america
chevrolet,malibu,3.1,1999,6,auto,f,18,26,r,midsize,america
chevrolet,malibu,3.5,2008,6,auto,f,18,29,r,midsize,america
chevrolet,malibu,3.6,2008,6,auto,f,17,26,r,midsize,america
dodge,caravan 2wd,2.4,1999,4,auto,f,18,24,r,minivan,america
dodge,caravan 2wd,3,1999,6,auto,f,17,24,r,minivan,america
dodge,caravan 2wd,3.3,1999,6,auto,f,16,22,r,minivan,america
dodge,caravan 2wd,3.3,1999,6,auto,f,16,22,r,minivan,america
dodge,caravan 2wd,3.3,2008,6,auto,f,17,24,r,minivan,america"
# Create df1 & df2
mpg_cars <- read.csv(header = TRUE, text = car_data)
summary_mpg <- mpg_cars %>%
group_by(continent,
manufacturer) %>%
summarize(cty = mean(cty),
hwy = mean(hwy),
models = n())
```
Row
------------------------
### Filters
```{r include=TRUE, message=FALSE}
############### This Works! #######################
sd1 <- SharedData$new(mpg_cars,
~manufacturer,
group = "Manufacturer")
sd2 <- SharedData$new(summary_mpg,
~manufacturer,
group = "Manufacturer")
#
filter_select("manufacturer",
"Manufacturer:",
sd1,
~manufacturer)
```
### Shared Summary
```{r include=TRUE, message=FALSE}
DT::datatable(sd2)
```
Row
-----------------------------------------------------------------------
### Shared data
```{r include=TRUE, message=FALSE}
DT::datatable(sd1)
```
I have tried the following, but it did not work right.
############## Do multiple fields work? #####################
# sd1 <- SharedData$new(mpg_cars, group = "individual")
# sd2 <- SharedData$new(summary_mpg, group = "individual")
#
# filter_select(id = "group1",
# sharedData = sd1,
# group = ~manufacturer,
# label = "Mfg. Filter")
#
# filter_select(id = "group2",
# sharedData = sd1,
# group = ~continent,
# label = "Cont. Filter")