shiny

R语言DT包在报告中的应用

2017-12-20  本文已影响0人  飘舞的鼻涕

在使用 Rmarkdown 或 Shiny 写报告时总会遇到 table 展示的困扰
有幸了解到益辉大神的DT包,如厕后的畅快感又一次降临了

有任何建议或疑问,请加 QQ: 1595218767 ,共同探讨学习
如R/python代码编程作图等方面需要帮忙,欢迎来店咨询 之恒科技, 挥动热情的小爪爪期待你哦

函数参数简介

datatable(data, options = list(), 
          class = "display", 
          callback = JS("return table;"), 
          rownames, 
          colnames, 
          container, 
          caption = NULL, 
          filter = c("none", "bottom","top"),
          escape = TRUE, 
          style = "default", 
          width = NULL, 
          height = NULL, 
          elementId = NULL, 
          fillContainer = getOption("DT.fillContainer", NULL), 
          autoHideNavigation = getOption("DT.autoHideNavigation",NULL), 
          selection = c("multiple", "single", "none"), 
          extensions = list(), 
          plugins = NULL)

各项参数功能实例

示例数据准备

library(DT)
m = cbind(matrix(rnorm(60, 1e5, 1e6), 20), runif(20), rnorm(20, 100))
dim(m)
m[, 1:3] = round(m[, 1:3])
m[, 4:5] = round(m[, 4:5], 7)
colnames(m) = head(LETTERS, ncol(m))
head(m)
           A        B       C         D         E
[1,] 1447159  -288199 -790697 0.8517458 100.68543
[2,] -204256  -260374 1783257 0.4864743 101.26310
[3,]  140063  -698646 -316450 0.5513788  99.95794
[4,]  170153   250854  -21752 0.3009611 101.35218
[5,] -899676 -1146587 -947206 0.4223725 102.43825
[6,]  279729  -377690 1411022 0.5134331  98.57725

列调整

货币/百分比/小数点 调整

sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      #th(rowspan = 2, 'Species'),
      th(colspan = 3, 'A'),
      th(colspan = 2, 'D'),
      th(colspan = 3, 'E')
    ),
    tr(
      lapply(c(1:3,1:2,1:3), th)
    )
  )
))
#print(sketch)

datatable(head(m[,c(1,1,1,4,4,5,5,5)],3),
          container = sketch, # 设置表头格式
          class='stripe', # 鼠标悬停,整行突出显示
          caption = 'Table 1: Format Columns') %>% # 标题
  formatCurrency(1:3, c('\U20AC','$','\U00A5'),# 货币形式
                 digits = c(1,0,2)) %>%
  formatPercentage(5, 2) %>% # 百分比形式
  formatRound('E', 3) %>% # 小数点位数
  formatRound(c(7,8), c(2,1)) # 小数点位数
cols.png

时间格式调整

x = strptime('2015-07-23 22:05:21', 
             '%Y-%m-%d %H:%M:%S', tz = 'EST')
(x = x + seq(1, 1e6, length.out = 10))
m = c('toDateString', 'toLocaleDateString', 
      'toLocaleString', 'toUTCString')
d = as.data.frame(setNames(lapply(m, function(.) x), m))
str(d)
datatable(d, options = list(pageLength = 5, dom = 'tip')) %>%
  formatDate(1:ncol(d), m)
date1.png

单元格调整

字体/背景调整

datatable(iris[c(1:2,55:56,120:121),],
          caption = ('Table 3 : This is formats for cells.'),
          options = list(pageLength = 5),#展示的行数
          #rownames=FALSE,
          #colnames = c(LETTERS[1:5])) %>%
          colnames = c('ID'=1,'A'=2,'B'=3,
                       'C'=4,'D'=5,'E'=6)) %>%
  formatStyle('A',  # column.name == 'xxx'
              color = styleInterval(c(5.1, 6), # <=5.1,w;<=6,b;else,r
                                    c('white', 'blue', 'red')),  # 字体颜色
              backgroundColor = 'skyblue', #背景颜色 
              fontWeight = styleInterval(5, c('normal', 'bold'))) %>% # 字体粗细
  formatStyle(
    'B', # column.name == 'xxx'
    color = styleInterval(3.4, c('blue','red')),
    backgroundColor = styleInterval(c(2.8,3.4),
                                    c('lightgray', 'lightyellow','lightpink'))
  ) %>% 
  formatStyle(
    'C', # column.name == 'xxx'
    background = styleColorBar(iris$Petal.Length, 
                               'steelblue'),
    backgroundSize = '100% 70%',#(width,height)
    backgroundRepeat = 'no-repeat',
    backgroundPosition = 'center'
  ) %>%
  formatStyle(
    4, # column 4
    color = styleEqual(1.5, c('red')) # value==1.5,fontColor=='red'
  ) %>% 
  formatStyle(
    'D',
    transform = 'rotateX(30deg) rotateY(5deg) rotateZ(10deg)',
    backgroundColor = styleEqual(
      unique(iris$Species), 
      c('lightblue', 'lightgreen', 'lightpink')
    )) %>%
  formatStyle(
    5,
    backgroundColor = styleEqual(
      unique(iris$Species), 
      c('lightblue', 'lightgreen', 'lightpink')
    ))
cells.png

References:

上一篇下一篇

猜你喜欢

热点阅读