Search code examples
rshinyr-markdowndtflexdashboard

searchPanes extension in Rmarkdown


I am trying to get the searchPanes extension from DT to work in an rmarkdown file (https://rstudio.github.io/DT/extensions.html). I can get it to work with datatable but not with renderDataTable and reactive statement. Attached is a working example that one tab shows it working and the other tab showing the issue. Any advice/recommendation is appreciate.

My code:

---
title: "Filter_example"
output: flexdashboard::flex_dashboard
runtime: shiny
date: "`r Sys.Date()`"
---

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

library(DT)
library(tidyverse)
```

# Sidebar {.sidebar data-width="200"}

```{r sidebar, echo=FALSE}
      selectizeInput('color', label ='color', choices = diamonds$color , 
               multiple = TRUE,
                    options = list(maxItems = 5,
                                 placeholder = 'Search Here...'))   

```

# Diamonds Are Forever 
```{r echo=FALSE}
diamonds %>% 
  datatable(
    rownames = F,
    extensions = c("SearchPanes", "Select", "Buttons"),
    options = list(
      language = list(searchPanes = list(collapse = "Filter Rows")),
       scrollX= "30vw",
       scrollY = "45vh",
      dom = "Blfrtip",
      buttons = list("searchPanes")
      ),
    selection = 'none'
    )
```

# Diamonds Are Forever renderDataTable
```{r echo=FALSE}
diamonds_dd <- reactive({
            if (is.null(input$color)){
                        diamonds
                    } 
      else if(!is.null(input$color)) {
    diamonds %>% 
    filter(color %in% input$color) 
}
}) 


DT::renderDataTable(diamonds_dd(),extensions = c("SearchPanes", "Select", "Buttons"),
    options = list(
       scrollX= "30vw",
       scrollY = "45vh",
      dom = "Blfrtip",
      buttons = list("searchPanes"),
      columnDefs = list(
        list(searchPanes = list(show = FALSE), targets = 3:5),
        list(searchPanes = list(controls = FALSE), targets = 0:2),
        list(className = "dt-center", targets = 0:5)
        )
      ),
    selection = 'none'
    )
```

Solution

  • You need server = FALSE.

    DT::renderDataTable(
        diamonds_dd(), extensions = c("SearchPanes", "Select", "Buttons"),
        options = list(
           scrollX= "30vw",
           scrollY = "45vh",
           dom = "Blfrtip",
           buttons = list("searchPanes"),
           columnDefs = list(
             list(searchPanes = list(show = FALSE), targets = 3:5),
             list(searchPanes = list(controls = FALSE), targets = 0:2),
             list(className = "dt-center", targets = 0:5)
             )
         ),
        selection = 'none',
        server = FALSE
    )
    

    enter image description here

    However, this is not recommended for a large dataset like diamonds. It sends all rows to users which is unnecessary and takes a long time.