Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reduce row height in DT datatables

Tags:

r

dt

shiny

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

like image 951
statquant Avatar asked Feb 07 '17 20:02

statquant


People also ask

How do I restrict the height of the DataTable?

The width and height of the datatable are determined by the container element. A scroller is appended to the table body if there are more rows to display. For example, you can restrict the height to 300px by applying CSS styling to the container element. <div style="height: 300px;"> <!-- lightning-datatable goes here --> </div>

Is it possible to change the height of rows in data table?

The Data Table control currently seems not to support changing the height of rows. I couldn't find any (advanced) property for row height. Documentation also says it's not available yet.

Is it possible to resize the row size of a DataTable?

Resizing Data row size is not supported within Data Table control in PowerApps currently, I afraid that there is no way to achieve your needs. More details about capabilities that are supported or not supported within Data Table, please check the following article:

What determines the width and height of the DataTable?

The width and height of the datatable are determined by the container element. A scroller is appended to the table body if there are more rows to display. For example, you can restrict the height to 300px by applying CSS styling to the container element.


2 Answers

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.

like image 153
sconfluentus Avatar answered Sep 21 '22 00:09

sconfluentus


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%') 
like image 37
smartse Avatar answered Sep 18 '22 00:09

smartse