[英]Dynamic number of table container in Shiny DT in R
我在闪亮的 R 中的 DT 有问题。假设我们有下表:
我使用以下代码得到了它:
sketch = htmltools::withTags(
table(
class = 'display',
thead(
tr(
th(rowspan = 2, 'Name'),
th(colspan = 2, '2022-06-01'),
th(colspan = 2, '2022-06-02')
),
tr(
lapply(rep(c('Length', 'Width'), 2), th)
)
)
)
)
datatable(df, container = sketch, rownames = FALSE)
我希望能够自动动态管理容器的数量。 在元素中
lapply (rep (c ('Length', 'Width'), 2), th)
很简单,通过将2替换为传递的参数,我无法完全管理片段
tr (
th (rowspan = 2, 'Name'),
th (colspan = 2, '2022-06-01'),
th (colspan = 2, '2022-06-02')
)
动态添加附加元素,即获取例如
tr (
th (rowspan = 2, 'Name'),
th (colspan = 2, '2022-06-01'),
th (colspan = 2, '2022-06-02'),
th (colspan = 2, '2022-06-03'),
th (colspan = 2, '2022-06-04'),
)
任何想法如何获得这种效果? 非常感谢!
像这样,如果我理解这个问题:
library(htmltools)
dates <- as.Date("2022-06-01") + 0:3
withTags(
tr(
th(rowspan = 2, 'Name'),
lapply(dates, function(d) th(colspan = 2, d))
)
)
# <tr>
# <th rowspan="2">Name</th>
# <th colspan="2">2022-06-01</th>
# <th colspan="2">2022-06-02</th>
# <th colspan="2">2022-06-03</th>
# <th colspan="2">2022-06-04</th>
# </tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.