1

我正在尝试更改闪亮应用程序中数据表的页脚。我可以使用以下代码复制我在应用程序中遇到的错误:

dt_test <- tibble(cntry = c("A","A","B"),
                  city = c("X","Y","Z"),
                  sales = c(1000,1500,500),
                  score = c(1.1234,5.1234,2.1234))
footer <- sapply(dt_test, function(x) ifelse((is.numeric(x)), sum(x), ""))

sketch <- htmltools::withTags(table(
  tableHeader(dt_test),
  tableFooter(footer)
  ))
sketch

R 显示了这一点:

Error in writeImpl(text) : 
Text to be written must be a length-one character vector

但是,如果我直接将页脚的定义作为参数,它会起作用:

sketch <- htmltools::withTags(table(
  tableHeader(dt_test),
  tableFooter(sapply(dt_test, function(x) ifelse( (is.numeric(x)),sum(x), "" ))
  )))

不幸的是,我不能使用这种方法,因为聚合包含很多业务逻辑,并且在单独的函数中执行。我在这里做错了什么?

4

1 回答 1

0

问题是footer标签的名称,即<footer>标签。因此,在footer内部使用时,htmltools::withTags您实际上是传递tags$footer(这是一个函数) tableFooter而不是存储在矢量页脚中的内容。这就是您收到错误消息的原因,也是您在直接传递定义时代码有效的原因。

这表示有两种选择可以使您的代码正常工作:

选项 1:使用htmltools::tags$...而不是htmltools::withTags

library(tibble)
library(DT)

sketch <- htmltools::tags$table(
  tableHeader(dt_test),
  tableFooter(footer)
)

选项 2:重命名变量

footer1 <- footer

sketch <- htmltools::withTags(table(
  tableHeader(dt_test),
  tableFooter(footer1)
))
于 2021-10-28T10:12:41.463 回答