R DT datatable font size change

依然范特西╮ 提交于 2020-12-13 07:11:51

问题


I want to change the font size of the entire data table., The default size is too large for what I want to display. I am trying to change the font-size of variable names and column content with formatStyle as follows

library(DT)
library(magrittr)
iris %>% 
     datatable() %>% 
     formatStyle(columns = colnames(.), `font-size` = '25%')

But, what is rendered is the same font size regardless of the changes. Tried different formats for font-size such as 'large, small' '150%, 50%' and '10px,20px,30px'


回答1:


Was able to change the font-size for header and the columns by updating the CSS. The table header with JS api and the column content font size with the formatStyle as follows. However, the header and the footer font size stayed the same. I would like to change header/footer/body (entire font for the table) in one swoop. Is that possible?

datatable(head(iris, 20), options = list(
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'font-size': '5px', 'background-color': '#c2d1f0', 'color': '#fff'});",
    "}"))) %>%  formatStyle(columns = colnames(.$x$data), `font-size` = '12px')

Attempted to update CSS for columns with the following command without success

"$(this.api().columns().data()).css({'font-size': '5px'});"

"$(this.api().table().footer()).css({'font-size': '10px});"

"$(this.api().tables().body()).css({'font-size': '10px'});"



回答2:


datatable(head(iris, 20), options = list(
  initComplete = JS("
                        function(settings, json) {
                          $(this.api().table().header()).css({
                          'font-size': '12px',
                          });
                        }
                    ")
)) %>%
  formatStyle(columns = colnames(.$x$data), `font-size` = "12px")



回答3:


This question has already been addressed here.

To accomplish what you are trying to do:

library(DT)
library(magrittr)
iris %>% 
  datatable() %>% 
  DT::formatStyle(columns = colnames(iris), fontSize = '25%')


来源:https://stackoverflow.com/questions/53655781/r-dt-datatable-font-size-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!