2

I'm using checkbox filters from crosstalk to filter entries in a table made with reactable.

I need a simple way to get the checkboxes to show the number of corresponding entries next to them.

Here's a minimal example of a table with checkbox filters:

Example.Rmd

---
title: "Filtering with crosstalk"
---

```{r}
library(reactable)
library(crosstalk)

data <- SharedData$new(iris)

filter_checkbox("species", "Species", data, ~Species)

reactable(data)
```

It spits out this: cross-talk checkboxes with the iris dataset

I need the checkboxes to show the number of corresponding entries next to them, like this:

enter image description here

What is the simplest way to do this?

Ulrik Lyngs
  • 98
  • 1
  • 5

1 Answers1

2

You can create the desired category label as a column to the dataset itself, then pass the data to SharedData$new() and create filter_checkbox based on that newly created column. Then hide this newly created column in reactable using colDef(show = FALSE).

---
title: "Filtering with crosstalk"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE)
```

```{r}
# preparing the label into the data itself

library(dplyr)

iris_df <- iris %>% 
  group_by(Species) %>% 
  mutate(
    Species_label = paste0(Species, " (",n(), ")")
  ) %>% ungroup()
```

```{r}
library(reactable)
library(crosstalk)

data <- SharedData$new(iris_df)

filter_checkbox("species", "Species", data, ~Species_label)

reactable(data,
          columns = list(
            Species_label = colDef(show = FALSE)
          ))
```

reactable with custom filter label


shafee
  • 15,566
  • 3
  • 19
  • 47