我正在尝试使用带有串扰和 DT 的 Flexdashboard 来复制 Excel 数据透视表的功能。我需要能够同时显示数据表和汇总表,并且能够同时在两个表上应用相同的过滤器。
下面的示例使用一个字段“制造商”进行过滤。我似乎无法弄清楚如何添加另一个过滤器,比如说“大陆”,以在两个表上工作。
---
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)
```
我尝试了以下方法,但效果不佳。
############## 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")