简体   繁体   English

R中Shiny DT中表容器的动态数量

[英]Dynamic number of table container in Shiny DT in R

I have problem with my DT in shiny R. suppose we have the following table:我在闪亮的 R 中的 DT 有问题。假设我们有下表:

在此处输入图像描述

I got it using the code:我使用以下代码得到了它:

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)

I would like to be able to dynamically manage the number of containers automatically.我希望能够自动动态管理容器的数量。 While in the element在元素中

lapply (rep (c ('Length', 'Width'), 2), th)

it's simple, by replacing 2 with the passed parameter, I can't manage the fragment completely很简单,通过将2替换为传递的参数,我无法完全管理片段

   tr (
     th (rowspan = 2, 'Name'),
     th (colspan = 2, '2022-06-01'),
     th (colspan = 2, '2022-06-02')
   )

to dynamically add additional elements, ie obtain eg动态添加附加元素,即获取例如

   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'),
   )

any ideas how to get this effect?任何想法如何获得这种效果? Many Thanks!非常感谢!

Like this, if I understand the question:像这样,如果我理解这个问题:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM