Rmarkdown中的水平滚动

时间:2017-08-23 21:19:13

标签: r knitr r-markdown

我有一个包含许多列的宽表,我希望能够在R-markdown中以水平滚动整齐地显示。我已阅读文档并尝试以下

```{css}
pre code, pre, code {
  overflow-x: scroll !important;

}
```

以及在代码块中设置options(width = 1000)。两种方法都失败了

kable(mat))显示彼此重叠的行名称和列名称,但我想通过x-overflow

来避免这种情况。

任何建议都将不胜感激。这是完整的数据集:

> dput(mat)
structure(list(`Jul 2016` = c(0.108624566189299, 1.76516237793691, 
0.24654630490587, 0.336005341665282, 1.00201353799076, 1.36940398901225, 
3.66411692441857, 0.927558916609414, 0.691046979743987, 0.450146262999624, 
0.917686009737563, 0.140224124770612, 0.843074107500625, 1.05518907118113
), `Aug 2016` = c(0.495219146367162, 0.391441250685602, 1.54749462052876, 
0.173654376994818, 0.633756110444665, 0.631372984033078, 0.18316258961296, 
0.182683125603944, 3.05005361690094, 3.94560365916646, 1.23740167125376, 
0.104842279106379, 1.19900375879825, 0.885420372667623), `Sep 2016` = c(0.233641386257531, 
0.0033428730592414, 0.750113554504904, 3.29350166751032, 0.321632610633969, 
0.147915420588106, 0.588389918208122, 0.448350375518203, 0.11338227847591, 
0.183331309817731, 0.503199412953109, 0.171937711299566, 1.95316866590622, 
0.104802044216859), `Oct 2016` = c(0.739911493734554, 1.11888734619589, 
0.84596463195504, 1.11839738455811, 0.217166714049797, 3.77456121684468, 
2.30912737349484, 0.587557188700885, 1.47151514614042, 2.22869711170409, 
0.922314693177951, 0.471901882905513, 0.534924456384033, 0.0313225598150482
), `Nov 2016` = c(1.85388414180818, 2.55487188870523, 0.0862376162762327, 
0.346423422452062, 1.51655351548436, 2.49406717891309, 2.30299281056283, 
0.263112519867718, 0.00580294150859118, 0.185066258069128, 0.0903926742270416, 
2.37662858317111, 3.73288818241513, 0.0573666100390255), `Dec 2016` = c(0.313190717250109, 
0.307465369347483, 0.239482162054628, 1.01155032901168, 2.39428096810792, 
1.94335110663416, 1.13592004678668, 1.42108202717247, 0.710517601505195, 
1.61591860465599, 0.455696674063802, 1.07137738536568, 0.621122478507459, 
0.618124924134463), `Jan 2017` = c(0.649377639405429, 1.83437980516794, 
0.297817176467259, 0.858038477557612, 1.49970549750212, 0.308495274279267, 
0.0211296189621441, 0.492535644210875, 0.161597543573384, 0.676304758060724, 
1.76788817649841, 0.550365904346108, 0.995464661116612, 1.13094686947322
), `Feb 2017` = c(0.198544207034127, 0.774353488306415, 0.138672931585461, 
0.420741511508822, 5.6814887264918, 1.73189786999466, 2.79930100191574, 
0.340402969624847, 1.39306524879292, 0.21201393196603, 0.0833668480627239, 
1.18146268087895, 0.418718740344048, 1.19787603798778), `Mar 2017` = c(2.09939065147947, 
2.97548833472614, 2.11208846593711, 0.485648008773732, 1.05899326592208, 
5.68802214045531, 0.78282427131654, 1.79497248670998, 0.941131940139789, 
0.00790881412103772, 2.83165551371601, 0.424321911297739, 1.32597267344118, 
0.846728059421194), `Apr 2017` = c(0.274038907606155, 0.609476356767118, 
1.03544399231197, 0.152570991776884, 0.339995595626533, 0.119376386050135, 
2.69392074818227, 0.813008557963085, 0.115333137568086, 2.11701883921835, 
2.56948147142742, 0.689078038092703, 0.70200188826294, 1.77652035201243
), `May 2017` = c(1.27068436700375, 0.0606693171930701, 0.223359284922481, 
1.24367935671927, 0.0387959590379592, 0.080388783942908, 0.265470865182579, 
0.0740235898768298, 4.56730645625964, 0.232464682413259, 2.11742714593623, 
2.0478821661256, 1.24304682384701, 0.660282246768475), `Jun 2017` = c(0.962065315763366, 
0.927825206740443, 0.235446563770117, 1.92433465077522, 0.629580335691571, 
0.343002805951983, 0.424926831386983, 0.280481974128634, 0.329299085657779, 
2.41983439671075, 0.570362926460803, 1.85204407244592, 0.603411518032043, 
0.924501490833573), `Jul 2017` = c(1.60951108251061, 1.03197320811326, 
0.98342213775487, 0.493952827528119, 0.278958274768225, 0.228817364528904, 
0.287614235659573, 0.324104877188802, 0.394792452454567, 2.14486608711631, 
0.206482742447406, 2.98590822501068, 0.0209106279733035, 1.14526769700916
), `Aug 2017` = c(2.55301682452176, 0.280766402836889, 1.13620263706417, 
0.976574084679496, 1.12200299925627, 2.1722190325201, 0.0616904203336536, 
2.1899062039933, 1.16009358405591, 1.51088082985876, 0.735464695799065, 
0.0305269896052778, 3.94500710607922, 0.940294581629341)), .Names = c("Jul 2016", 
"Aug 2016", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", 
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017", 
"Aug 2017"), row.names = c("Jul 2016", "Aug 2016", "Sep 2016", 
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", 
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017"), class = "data.frame")

2 个答案:

答案 0 :(得分:3)

你试过DataTables吗? R数据对象(矩阵或数据框)可以在HTML页面上显示为表格,DataTables在表格中提供过滤,分页,排序和许多其他功能。

答案 1 :(得分:3)

您可以查看我的enter image description here包裹。

我认为我在上一个CRAN版本中添加的scroll_box函数是您可能会觉得有用的东西。

kable(dt, "html") %>% kable_styling("striped") %>% scroll_box(width = "100%")