/
using-crosstalk.Rmd
85 lines (69 loc) · 2.43 KB
/
using-crosstalk.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: "Using Crosstalk"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{using-crosstalk}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
The `rsc_table()`, `rsc_grid()`, `rsc_search()`, and `rsc_filter()` widgets use
`crosstalk` to facilitate inter-widget communication. In simple cases, passing
identical results from the `content()` tibble is sufficient:
```{r, eval=FALSE}
library(connectwidgets)
library(dplyr)
library(purrr)
library(htmltools)
client <- connect()
all_content <- client %>% content()
some_content <- slice_sample(all_content, prop = .1)
tagList(
rsc_cols(rsc_search(some_content), rsc_filter(some_content)),
rsc_table(some_content)
)
```
For more complicated layouts, you may want to pass your own `crosstalk` objects to the components.
## Example: Grids grouped by owner
As a publisher, you want to:
- display each content owner's username with a level 4 heading
- display an `rsc_grid()` of each content owner's items
- display one search and filter widget that works across all the card grids
`crosstalk::SharedData$new()` takes two optional parameters that make this possible:
- `keys`
- The `content()` tibble contains a globally unique identifier (GUID) for each piece of content, which you can [use as a key](https://rstudio.github.io/crosstalk/using.html#keys) when creating your `SharedData` object (e.g `key = ~ guid`).
- `group`
- Specifying a [group name](https://rstudio.github.io/crosstalk/using.html#grouping)
when creating the `SharedData` object gives you control over which widgets are
grouped together (e.g. `group = "xfilter"`).
Since the `some_content` tibble contains all the GUIDs, passing it to the Search
and Filter components will enable you to search across each of the subset tibbles
created by `dplyr::group_nest`:
```{r, eval=FALSE}
some_content_xfilter <- crosstalk::SharedData$new(
some_content,
key = ~ guid,
group = "xfilter"
)
rsc_content_shared <-
some_content %>%
group_nest(owner_username, .key = "content_df", keep = TRUE)
div(
h3("Grouped Grids by Owner", class = "text-center"),
rsc_cols(rsc_search(some_content_xfilter), rsc_filter(some_content_xfilter)),
purrr::pmap(
rsc_content_shared,
{
~ tagList(
h4(..1),
rsc_grid(crosstalk::SharedData$new(..2, key = ~ guid, group = "xfilter"))
)
}
)
)
```