yihui
遇到一个新问题,当在 flexdashboard 里面把 DT 结合 crosstalk 一起使用时,设置scrollY = FALSE
理论上也是起作用的,但是要是刚好使用的数据长得比较奇形怪状,那个幺蛾子就又跑出来作妖了。下面一共有三种情况:
Table A:只使用 DT 包,scrollY = FALSE
是起作用的。
Table B:DT + crosstalk,由于数据中某一列的值特别长,如“医院名称”为“哈尔滨医科大学附属第三医院(哈尔滨医科大学附属肿瘤医院)”,scrollY = FALSE
不起作用,本来应该展示十行数据的,只展示了一行。
Table C:在 Table B 的基础上,将“医院名称”那列的宽度改为200px,scrollY = FALSE
依然不起作用,本来应该展示十行数据的,展示了两行。
代码如下
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
```
```{css, echo=FALSE}
/*去除边距设置 */
.container-fluid.crosstalk-bscols {
margin-left: inherit;
margin-right: inherit;
}
```
# TRUE
## Row {data-height=800}
### Table A
```{r}
table <- data.frame(
"xxxx号" = rep('10000000000', 20),
"是否欺诈" = rep('是', 20),
"事故者姓名" = rep('坂田银时', 20),
"事故者证件号" = rep('232102190000000000', 20),
"医院名称" = rep('哈尔滨医科大学附属第三医院(哈尔滨医科大学附属肿瘤医院)', 20),
"住院天数" = rep(10, 20),
"门诊账单金额" = rep(1000, 20),
"住院账单金额" = rep(10000, 20),
"xxxx时间" = rep('2019/4/4', 20),
"xxxxx时间" = rep('2019/4/16 13:55:55', 20),
"医院所属省份" = rep('黑龙江省', 20),
"医院所属城市" = rep('哈尔滨市', 20),
"啊啊啊啊啊" = rep('娘口三三', 20),
"哦哦哦哦哦" = rep('哦哦哦哦哦哦哦哦哦', 20),
"嗷嗷嗷嗷嗷嗷" = rep('嗷嗷嗷嗷嗷嗷', 20),
"嗯嗯嗯嗯嗯嗯嗯" = rep('嗯嗯嗯', 20),
"鹅鹅鹅鹅鹅" = rep('鹅鹅鹅鹅鹅', 20),
"icd10代码" = rep('S60', 20),
"icd10名称" = rep('腕和手浅表损伤,未特指', 20)
)
DT::datatable(table,
width = '100%',
options = list(pageLength = 10,
scrollY = FALSE))
```
# FALSE1
## Row {data-height=800}
### Table B
```{r}
library(crosstalk)
shared_table1 <- SharedData$new(table)
bscols(
widths = c(2, 10),
list(
filter_checkbox("是否欺诈", "是否欺诈", shared_table1, ~ `是否欺诈`, inline = TRUE),
filter_slider("住院账单金额", "住院账单金额", shared_table1, ~ `住院账单金额`, width = "100%"),
filter_slider("门诊账单金额", "门诊账单金额", shared_table1, ~ `门诊账单金额`, width = "100%"),
filter_select("医院所属省份", "医院所属省份", shared_table1, ~ `医院所属省份`)
),
DT::datatable(
table,
width = '100%',
options = list(
pageLength = 10,
scrollY = FALSE
)
)
)
```
# FALSE2
## Row {data-height=800}
### Table C
```{r}
library(crosstalk)
shared_table1 <- SharedData$new(table)
bscols(
widths = c(2, 10),
list(
filter_checkbox("是否欺诈", "是否欺诈", shared_table1, ~ `是否欺诈`, inline = TRUE),
filter_slider("住院账单金额", "住院账单金额", shared_table1, ~ `住院账单金额`, width = "100%"),
filter_slider("门诊账单金额", "门诊账单金额", shared_table1, ~ `门诊账单金额`, width = "100%"),
filter_select("医院所属省份", "医院所属省份", shared_table1, ~ `医院所属省份`)
),
DT::datatable(
table,
width = '100%',
options = list(
pageLength = 10,
scrollY = FALSE,
autoWidth = TRUE, # 单独设置“医院名称”那列的列宽
columnDefs = list(list(width = '200px', targets = c(5)))
)
)
)
```
效果如下
Table A
Table B
Table C