闪亮DT中桌子容器的动态数量

发布于 2025-02-04 15:51:51 字数 1128 浏览 2 评论 0原文

我在Shiny 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'),
   )

任何想法如何获得这种效果?非常感谢!

I have problem with my DT in shiny R.
suppose we have the following table:

enter image description here

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

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

to dynamically add additional elements, i.e. obtain e.g.

   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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2025-02-11 15:51:51

这样,如果我理解一个问题:

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文