通过串扰过滤两个表

Pro*_*eus 2 r datatables shiny flexdashboard

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

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

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

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

例如,假设我有:

  1. 数据框一:包含原始数据

    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 =结构(c(2L,2L,2L,1L,1L),.Label = c(“ benz”,“ bmw”),class =“ factor”),id =结构(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”)

  2. Datafrane 2:包含汇总数据

    df2 <-structure(list(car = structure(c(1L,2L,1L,2L),.Label = c(“ benz”,

    • “ bmw”),类=“ factor”),所有者=结构(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),类=“ data.frame”)

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

我可以创建两个不同的对象:

library(crosstalk)
shared_df1 <- SharedData$new(df1)
shared_df2 <- SharedData$new(df2)
Run Code Online (Sandbox Code Playgroud)

然后:

filter_select("owner", "Car owner:", shared_df1, ~ owner)
filter_select("owner", "Car owner:", shared_df2, ~ owner)
Run Code Online (Sandbox Code Playgroud)

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

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

Lod*_*ert 7

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

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

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

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

这应该适合您的示例。

---
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)
```
Run Code Online (Sandbox Code Playgroud)

我试图从数据中提取花费在这一段时间plot_ly()使用plotly_data(),直到我想出答案没有运气。这就是为什么有一些非常简单的地块。