在for循环渲染中打印DT数据表到html

问题描述 投票:0回答:1

我在将四开文档正确渲染为 HTML 时遇到问题 - 它可以渲染,但我的表格没有出现。我使用

DT::datatable()
是因为我希望能够灵活地对行进行排序/过滤 - 如果我使用
kable
进行渲染,则不会出现同样的问题。

我有来自一项大型调查的数据,我想创建一个文档,其中包含作为标题的问题,后面是该问题的摘要表,然后是按每个人群细分对该问题的回答的副标题和摘要。

为了暂时简化,我只是研究如何获取一个问题的副标题和人口统计摘要。

到目前为止,我已经尝试过以下方法:

for (i in 1:length(test_list)){
    cat(sprintf("\n#### %s\n", attributes(test_data[[names(test_list[[i]][1])]])$label))
    if (is.null(test_list[[i]])){
      next}else{
    print(test_list[[i]] |> DT::datatable(rownames = F, escape = F))
      }
}

我也在不使用

print()
的情况下尝试了相同的操作,并尝试使用
htmltools
来获取表格的原始代码 - 这是打印出来的,但我不知道如何获取原始代码来渲染,所以最终结果是标题下的原始代码。 ChatGPT 建议保存每个表,然后打印它,但由于数据集太大,这实际上不是一个选择。

这是示例数据:

test_list <- list(structure(list(Q1 = structure(c(1L, 2L, 3L, NA), levels = c("A", 
"B", "C"), label = "Label 1", class = "factor"), 
    N = c(67L, 28L, 6L, 20L), Mean = c(1, 1, NaN, NaN), SD = c(0, 
    0, NA, NA), `ResponseY` = c(2.99, 7.14, NA, NA), `NA` = c(97.01, 
    92.86, 100, 100)), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(Q2 = structure(c(1L, 2L, NA), levels = c("D", 
"E"), label = "Label 2", class = "factor"), 
    N = c("98", "Insufficient number of responses", 
    "21"), Mean = c(1, NA, NaN), SD = c(0, NA, NA), `ResponseY` = c(4.08, 
    NA, NA), `NA` = c(95.92, NA, 100)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(Q3 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
NA), levels = c("F_", "G", "H", "I", "J", 
"K"), label = "Label 3", class = "factor"), 
    N = c("Insufficient number of responses", 
    "25", "33", "24", "11", "Insufficient number of responses", 
    "23"), Mean = c(NA, 1, 1, NaN, NaN, NA, NaN), SD = c(NA, 
    0, NA, NA, NA, NA, NA), `ResponseY` = c(NA, 12, 3.03, 
    NA, NA, NA, NA), `NA` = c(NA, 88, 96.97, 100, 100, NA, 100
    )), class = "data.frame", row.names = c(NA, -7L)))


test_data <- structure(list(Q1 = structure(c(2, 2, 1, 1, 2, 2), labels = c(A = 1, 
B = 2, C = 3), label = "Label 1", class = c("haven_labelled", 
"vctrs_vctr", "double")), Q2 = structure(c(1, 1, 1, 1, 1, 1), labels = c(D = 1, 
E = 2), label = "Label 2", class = c("haven_labelled", "vctrs_vctr", 
"double")), Q3 = structure(c(3, 3, 2, 2, 4, 4), labels = c(F_ = 1, 
G = 2, H = 3, I = 4, J = 5, K = 6), label = "Label 3", class = c("haven_labelled", 
"vctrs_vctr", "double"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

这是我正在寻找的输出格式的示例:

这是通过以下方式实现的:

cat(sprintf("\n#### %s\n", attributes(test_data[[names(test_list[[1]][1])]])$label))

test_list[[1]] |> DT::datatable(options = list(pageLength = 10), rownames = F, escape = F)

cat(sprintf("\n#### %s\n", attributes(test_data[[names(test_list[[2]][1])]])$label))

test_list[[2]] |> DT::datatable(options = list(pageLength = 10), rownames = F, escape = F)

cat(sprintf("\n#### %s\n", attributes(test_data[[names(test_list[[3]][1])]])$label))

test_list[[3]] |> DT::datatable(options = list(pageLength = 10), rownames = F, escape = F)

我只需要让它在 for 循环中工作。

编辑:

非常幸运,我成功地使用以下代码显示了表格,尽管它也给了我一堆我不想要的文本:

for (i in 1:length(test_list)){
  cat(sprintf("\n#### %s\n", attributes(test_data[[names(test_list[[i]][1])]])$label))
  
  print(htmltools::renderTags(DT::datatable(test_list[[i]])))
}
html r for-loop dt quarto
1个回答
0
投票

弄清楚了,尽管我不知道为什么要这样才能使其发挥作用(如果有人可以解释,我真的很感激!):

首先,在读入数据之前添加

DT::datatable(matrix())
- 不这样做,或者在读入数据后在某处添加它,似乎根本没有帮助。

然后当涉及到实际循环时:

for (i in 1:length(test_list)){
   cat(sprintf("\n#### %s\n", attributes(test_data[[names(test_list[[i]])[1]]])$label))
   print(htmltools::tagList(DT::datatable(test_list[[i]], rownames = F, escape = F)))
}
© www.soinside.com 2019 - 2024. All rights reserved.