Introduction

I’m not a big fan of the default options for printing dataframes as specified in the YAML header (e.g., default, paged, tibble, kable) of Rmarkdown. Here I hope to:

  1. Customize the aesthetics of data table printing from Rmarkdown to HTML.
  2. Generate a function that bundles these preferences.
  3. Pass this custom function to ‘df_print’ in the YAML header of Rmarkdown so that all tables successively knitted under this function call will retain my preferred aesthetics.

We are going to tweak the aesthetics of some fake data with the kableExtra package.

and awaaay we go!

Let’s work with mtcars from base R

dat <- mtcars
str(dat)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
print(dat)
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

That’s fairly ugly, but it gets the job done.

Base table

Let’s see what we can improve on using kableExtra. We will use the almighty pipe for these bits of code. Here’s the most stripped down version similar to what you might encounter when you specify df_print: kable. Here I am using indices to limit the output to all columns but only the first 10 rows. Cells are now left-aligned and output is limited to two decimal places.

dat[1:10, ] %>% kbl(digits = 2, align = "l") %>% kable_styling()  #max decimals to print is two places
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Hover & Condense

This one is a bit too squashed.

dat[1:10, ] %>% kbl(digits = 2, align = "l") %>% kable_paper("hover", full_width = F)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Kable Classic I

Change font, font size, add caption

dat[1:10, ] %>% kbl(digits = 2, align = "l", caption = "Here's a caption!") %>% kable_classic(full_width = F, 
    html_font = "Helvetica", font_size = 14)  #change font and font size
Here’s a caption!
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Kable Classic II

dat[1:10, ] %>% kbl(digits = 2, align = "l", caption = "Here's a caption!") %>% kable_classic_2(full_width = F, 
    html_font = "Helvetica", font_size = 14)  #condensed
Here’s a caption!
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
dat[1:10, ] %>% kbl(digits = 2, align = "l", caption = "Larger tables better streched full width") %>% 
    kable_classic_2(full_width = T, html_font = "Helvetica", font_size = 14)  #full width
Larger tables better streched full width
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Bootstrap options applied

dat[1:10, ] %>% kbl(digits = 2, align = "l", caption = "Here's a nice version") %>% 
    kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
        html_font = "Helvetica", font_size = 14)
Here’s a nice version
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Positioning

left

best for smaller tables not stretched to full width

dat[1:10, ] %>% kbl(digits = 2, align = "l", caption = "I'm on the left side") %>% 
    kable_styling(bootstrap_options = "striped", full_width = F, position = "left", 
        html_font = "Helvetica", font_size = 14)
I’m on the left side
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Float figure and wrap text

dat[1:10, ] %>% kbl(digits = 2, align = "l", caption = "I'm on the right side now") %>% 
    kable_styling(bootstrap_options = "striped", full_width = F, position = "float_right", 
        html_font = "Helvetica", font_size = 14)  #wrap text
I’m on the right side now
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Here’s some bullshit to test out textwrapping from a paper we just published in the Journal of Neurolinguistics. Word meanings are often suffused with sensory, motor, and affective features. For many of us, a word such as beach evokes a diverse range of pleasant associations including blue skies (visual),gritty sand (tactile), crashing waves (auditory), and the distinctive smell of sunscreen (olfactory).Aristotle argued for a hierarchy of the senses where vision and audition eclipse the lesser modalities of odor, taste, and touch. A direct test of Aristotle’s premise was recently made possible with the establishment of the Lancaster Sensorimotor Norms (2019), a crowdsourced database cataloging sensorimotor salience for nearly 40,000 English words. Neurosynth, a metanalytic database of functional magnetic resonance imaging studies, can potentially confirm if Aristotle’s sensory hierarchy is reflected in functional activation within the human brain. We correlated sensory salience of English words as assessed by subjective ratings of vision, audition, olfaction, touch, and gustation (Lancaster Ratings) with volumes of cortical activation for each of these respective sensory modalities (Neurosynth). English word ratings reflected the following sensory hierarchy: vision > audition > haptic > olfaction gustation. This linguistic hierarchy nearly perfectly correlated with voxel counts of functional activation maps by each sensory modality (Pearson r = .99). These findings are grossly consistent with Aristotle’s hierarchy of the senses. We discuss implications and counterevidence from other natural languages.

Freeze header

dat[1:10, ] %>% kbl(digits = 2, align = "l") %>% kable_styling(fixed_thead = T)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Format header row

dat[1:10, ] %>% kbl(digits = 2, align = "l") %>% kable_paper("striped", full_width = T, 
    html_font = "Helvetica", font_size = 14) %>% row_spec(0, color = "white", background = "#84BA8C")
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4

Add x-y scrollbars

No need for indices. Just print the whole thing.

dat %>% kbl(digits = 2, align = "l") %>% kable_paper("striped", full_width = T, fixed_thead = T, 
    html_font = "Helvetica", font_size = 14) %>% row_spec(0, color = "white", background = "#84BA8C") %>% 
    scroll_box(width = "800px", height = "300px")
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.42 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.34 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.20 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.61 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.83 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.46 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.44 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.85 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.94 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.14 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.51 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.17 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.77 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.60 1 1 4 2

Putting it all together

dat %>% kbl(digits = 2, align = "l") %>% kable_styling(fixed_thead = T, position = "float_right") %>% 
    kable_paper("striped", full_width = T, fixed_thead = T, html_font = "Helvetica", 
        font_size = 12) %>% row_spec(0, color = "white", background = "#84BA8C") %>% 
    scroll_box(width = "850px", height = "300px")
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.42 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.34 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.20 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.61 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.83 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.46 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.44 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.85 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.94 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.14 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.51 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.17 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.77 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.60 1 1 4 2

Test driving print functions

df.out1

df.out1 <- function(x) {
    x <- x %>% kbl(digits = 2, align = "l") %>% kable_styling(fixed_thead = T, position = "float_right") %>% 
        kable_paper("striped", full_width = T, fixed_thead = T, html_font = "Helvetica", 
            font_size = 12) %>% row_spec(0, color = "white", background = "#84BA8C") %>% 
        scroll_box(width = "600px", height = "300px")
    return(x)
}
df.out1(dat)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.42 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.34 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.20 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.61 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.83 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.46 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.44 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.85 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.94 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.14 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.51 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.17 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.77 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.60 1 1 4 2

df.out2

df.out2 <- function(x) {
    x <- x %>% kbl(digits = 2, align = "l") %>% kable_styling(fixed_thead = T, position = "float_right") %>% 
        kable_paper("striped", full_width = T, fixed_thead = T, html_font = "Helvetica", 
            font_size = 13) %>% row_spec(0, color = "white", background = "#5b705f") %>% 
        scroll_box(width = "600px", height = "300px")
    return(x)
}
df.out2(dat)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.42 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.34 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.20 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.61 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.83 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.46 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.44 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.85 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.94 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.14 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.51 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.17 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.77 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.60 1 1 4 2

df.out3

df.out3 <- function(x) {
    x <- x %>% kbl(digits = 2, align = "l", booktabs = T) %>% kable_styling(fixed_thead = T, 
        position = "float_right") %>% kable_paper("striped", full_width = T, fixed_thead = T, 
        html_font = "Helvetica", font_size = 11) %>% row_spec(0, color = "yellow", 
        background = "#5b705f", font_size = 12) %>% scroll_box(width = "700px", height = "300px")
    return(x)
}
df.out3(dat)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.42 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.34 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.20 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.61 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.83 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.46 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.44 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.85 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.94 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.14 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.51 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.17 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.77 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.60 1 1 4 2

Apply global dataframe print option

Delete the df_print line from the YAML header, then paste this print function in the code block right after the YAML header (after the libraries). Now your dataframes and tables will all automatically print in this glorious style.

print.me <- function(x, ...) {
    x %>% kbl(digits = 2, align = "l", booktabs = T) %>% kable_styling(fixed_thead = T, 
        position = "float_left") %>% kable_paper("striped", full_width = T, fixed_thead = T, 
        html_font = "Helvetica", font_size = 11) %>% row_spec(0, color = "white", 
        background = "#5b705f", font_size = 12) %>% scroll_box(width = "700px", height = "300px") %>% 
        asis_output()
}

registerS3method("knit_print", "data.frame", print.me)
# registerS3method(genname, class, method, envir = parent.frame())