How can I reduce row height in DT datatables
Asked Answered
D

2

18

I would like to be able to get 'slimmer' rows when rendering a DT datatable (ie decrease that height)

options(digits.secs=6)
library(data.table)
d = data.table(x = 1:10,time = as.POSIXct('2015-03-23 12:00:00.123'))
library(DT)
datatable(d)

enter image description here

Dissimilitude answered 7/2, 2017 at 20:31 Comment(2)
You can use datatable(d, class="compact")Verso
@Verso the only thing it appears to be doing is removing the nice highlighting.Dissimilitude
A
21

If you add the pageLength= attribute you can set how many rows to show initially. And by adjusting the lengthMenu= c() you can also control the sizes of offered in the drop down, You can also turn search on or off with searching =FALSE

   library(DT)
    datatable(d, options=list(
       pageLength = 3,
       lengthMenu = c(2, 12, 18),
       searching= FALSE))%>%

   formatStyle( 0, target= 'row',color = 'black', backgroundColor = 'yellow', fontWeight ='bold', lineHeight='70%')

And by using the helper functions you can set the style just as you would in traditional CSS on a webpage. Notice the last one, line-height should adjust the row height.

Edited: I moved all the code together for you to see how it works. Sorry I was not clearer up front. The %>%is necessary as is devtools::install_github("rstudio/DT") version of DT.

Angelitaangell answered 11/2, 2017 at 2:36 Comment(5)
sounds interesting, but was anybody able to reproduce that? =)Monovalent
Sorry, I cut and pasted WITHOUT the import part...options=list() around the arguments. I have used it and it works. If you cannot get this to work, be sure you installed the devtools::install_github("rstudio/DT") version.Angelitaangell
you can include any camelCase modified CSS style for the type this way...Angelitaangell
@Angelitaangell Is it possible to adust the height of the header row and filter row similarly?Fairish
@Fairish that is a very good question, I have not tried it before. Sorry about that one!Angelitaangell
S
4

I found the above answer didn't work. My simpler solution found via https://rstudio.github.io/DT/010-style.html is to use:

DT::datatable(df) %>%
DT::formatStyle(names(df),lineHeight='80%') 
Sexy answered 26/4, 2020 at 14:44 Comment(1)
The only difference in your solution is the use of pipes and the calling of DT using : . The formatStyle() is accomplished the same way. With properly called leaflet the :: is not needed, but scoping can be an issue depending on the order in which you call packages from your library.Angelitaangell

© 2022 - 2024 — McMap. All rights reserved.