I'm having trouble correctly rendering my quarto document to HTML - it renders, but my tables aren't appearing. I'm using DT::datatable() because I want to have the flexibility to sort/filter rows - there isn't the same problem if I render using kable.
I have data from a large survey and I want to create a document that contains the question as a heading, followed by summary tables for that question, and then subheadings and summaries breaking down responses to that question by each demographic.
To simplify for now, I'm just looking at how to get the subheadings and demographic summaries for one question.
So far I've tried the following:
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))
}
}
I also tried the same without using print(), and attempted to use htmltools to get the raw code for the tables - this printed but I couldn't figure out how to get that raw code to render so just ended up with raw code under headings. ChatGPT recommended saving each table and then printing that but since it's such a large dataset this isn't really an option.
Here is the example data:
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"))
This is an example of the output format I'm looking for:
Which was achieved with:
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)
I just need to get it working in the for loop.
EDIT:
Through sheer luck I've managed to get the tables to appear with the following code, though it's also giving me a bunch of text that I don't want:
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]])))
}

Figured it out, though I've no idea why this is what it takes to make it work (if anyone could explain I'd really appreciate it!):
First, adding
DT::datatable(matrix())before reading in the data - without doing this, or adding it somewhere after reading in the data, doesn't seem to help at all.Then when it comes to the actual loop: