Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing font size in R DataTables (DT)

Tags:

Have been trying to change the font size of all text in the tables generated by DT. However, I could only figure out how to change the size of the records using formatStyle(names(datCalc), fontSize = '12px'). The column headers and buttons have text of the same size. Using R Markdown in RStudio.

like image 467
tmesis Avatar asked May 21 '17 19:05

tmesis


People also ask

How to increase the font size of a plot in R?

We can create a plot with default font sizes as follows: Figure 1: Base R Plot with Default Font Sizes. Now, if we want to increase certain font sizes, we can use the cex arguments of the plot function. Have a look at the following examples… We can increase the labels of our plot axes with the cex.lab argument:

What is R DataTables?

The R package DT provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables.

What is DTDT in R?

DT: An R interface to the DataTables library. The R package DT provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables.

How to increase the font size of the main title?

The font size of the main title can be increased with the cex.main argument: Figure 4: Base R Plot with Increased Font Size of Main Title. The font size of the subtitle is getting larger by specifying a larger value for the cex.sub argument:


2 Answers

I think you almost got there. I solved it by explicitly telling DT::formatStyle() which columns I wanted. I first tried using the names() or colnames() approach, as you did. For some reason this didn't work:

iris %>%
  DT::datatable() %>%
  DT::formatStyle(columns = colnames(.), fontSize = '50%')

However, we know the iris dataset has 5 columns, so I just did this:

iris %>%
  DT::datatable() %>%
  DT::formatStyle(columns = c(1, 2, 3, 4, 5), fontSize = '50%')

In this case, I use font-size = 50%, but you can also specify font-size = 12pt as you did. You can also supply logical vectors like c(T, F, F, F, T) to the columns argument, and the formatting will apply to those columns for which you have stated TRUE.

like image 53
Juan Carlos Villaseñor Derbez Avatar answered Sep 19 '22 12:09

Juan Carlos Villaseñor Derbez


Adding CSS through a javascript table header call seems to do the trick (i.e 'this.api().table().header()' ).

datatable(..., options=list(
  initComplete = JS(
        "function(settings, json) {",
        "$(this.api().table().header()).css({'font-size': '50%'});",
        "}")))
  )

Citation: Section 4.3 @ https://rstudio.github.io/DT/options.html

like image 23
sabeepa Avatar answered Sep 16 '22 12:09

sabeepa