Search code examples
rr-markdowndtxaringan

R DT table with buttons does not fit page in Xaringan HTML5 presentation


I have a dataframe df that I am trying to output as an interactive table in an Xaringan HTML5 presentation. The code works fine, it's just that after adding buttons the table no longer completely fits the Chrome browser page as shown in the screen grab below.

enter image description here

How can I fix this?

Sample data (df):

structure(list(City = c("HOLLYWOOD", "PLANTATION", "Davie", "HOLLYWOOD", 
"PLANTATION", "HOLLYWOOD", "PLANTATION", "Davie", "HOLLYWOOD", 
"PLANTATION"), Zipcode = c("33024", "33317", "33314", "33024", 
"33317", "33024", "33317", "33314", "33024", 
"33317"), Date = structure(c(18996, 18633, 19011, 19012, 19016, 18996, 18633, 19011, 19012, 19016
), class = "Date"), Year = c(2022, 2021, 2022, 2022, 2022, 2022, 2021, 2022, 2022, 2022), Month = c(1, 
1, 1, 1, 1, 1, 
1, 1, 1, 1), Day = c(4, 6, 19, 20, 24, 4, 6, 19, 20, 24), SR = c("SR-22-001", "SR-22-002", 
"SR-22-003", "SR-22-004", "SR-22-006", "SR-22-001", "SR-22-002", 
"SR-22-003", "SR-22-004", "SR-22-006"), Permit = c("06-SE-2433290", 
"06-SE-2444371", "06-SM-2448351", "06-SM-2448625", NA, "06-SE-2433290", 
"06-SE-2444371", "06-SM-2448351", "06-SM-2448625", NA), `Owner/Agent` = c("Pardo, G A & Elaine Nu-Black Septic Co", 
"Alshine Mondesir A Tip Top Septic", "Charlotte Ingmire Mr. C's Pumbling & Septic Inc.", 
"SRP Sub LLC Statewide Septic Cont Inc", "John Nelson Mr. C's Pumbling & Septic Inc.", "Pardo, G A & Elaine Nu-Black Septic Co", 
"Alshine Mondesir A Tip Top Septic", "Charlotte Ingmire Mr. C's Pumbling & Septic Inc.", 
"SRP Sub LLC Statewide Septic Cont Inc", "John Nelson Mr. C's Pumbling & Septic Inc."
), Address = c("1111 Harding St Hollywood, FL 33024", "5555 W Broward Blvd Plantation, 33317", 
"1111 SW 74 Ave Davie, 33314", "2222 Thomas Street Hollywood, FL 33024", 
"333 Bryan Blvd Plantation, 33317", "12345 Harding St Hollywood, FL 33024", "34556 W Broward Blvd Plantation, 33317", 
"55656 SW 74 Ave Davie, 33314", "3333 Thomas Street Hollywood, FL 33024", 
"316 Bryan Blvd Plantation, 33317")), sfc_columns = c("x", "y"
), class = "data.frame", row.names = c(NA, -5L)) 

Code:

---
title: "Ed Edd n Eddy
subtitle: ""
author: "Ed"
institute: "Gravy"
date: "`r format(Sys.Date(),'%e de %B, %Y')`"
output:
  xaringan::moon_reader:
    css: xaringan-themer.css
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
editor_options: 
  chunk_output_type: console
---

<style>
div.remark-slide-content {
  padding: 1em; /*default is 1em 4em*/
}
.dataTables_wrapper {
  font-size: .5em;
}
</style>

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(knitr)
library(tidyverse)
library(sf)
library(DT)
library(xaringanthemer)
```
---
# Interactive Table
```{r, Data Table}
datatable(df,
          extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
  ))  %>%
  formatStyle(names(df),
                              background = 'lightblue', angle = -90,
                              backgroundSize = '98% 88%',
                              backgroundRepeat = 'no-repeat',
                              backgroundPosition = 'center')

```

Solution

  • One option is to add a page length.

    datatable(df,
              extensions = 'Buttons', options = list(
                pageLength = 5,    
                dom = 'Bfrtip',
                buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))) %>%
      formatStyle(names(df),
                  background = 'lightblue', angle = -90,
                  backgroundSize = '98% 88%',
                  backgroundRepeat = 'no-repeat',
                  backgroundPosition = 'center')
    

    enter image description here