英文:
Return a series of DT::datatables within a list in knitted HTML document
问题
我有一个返回一系列表格的函数。我想要将它们全部作为DT::datatable返回。然而,当它们在一个列表中时,我无法让R将这些表格返回。它们会出现在RMarkdown文件中,但在已编织的HTML文件中却没有出现。是否可能让这些表格出现在HTML文档中?
---
title: "未命名"
output: html_document
---
myfunc <- function(dataset){
return_list <- list()
mytab <- DT::datatable(dataset)
return_list$mytab <- mytab
return(return_list)
}
myfunc(mtcars)
表格出现在RMarkdown文件中:
但在已编织的HTML文件中没有出现:
<details>
<summary>英文:</summary>
I have a function that returns a series of tables. I want to return them all as a DT::datatable. However, I cannot get R to return these tables when they are in a list. They appear in the RMarkdown file, but not in the knitted HTML file. Is it possible to get the tables to appear in the HTML doc?
title: "Untitled"
output: html_document
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
myfunc <- function(dataset){
return_list <- list()
mytab <- DT::datatable(dataset)
return_list$mytab <- mytab
return(return_list)
}
myfunc(mtcars)
The table appears in the RMarkdown file:
But does not appear in the knitted HTML file:
答案1
得分: 1
如果您提前知道键,只需使用键调用它:
myfunc(mtcars)$mytab
如果您计划创建一个更长的列表,并希望打印列表中的所有表格,请使用{htmltools}
中的tagList
:
htmltools::tagList(myfunc(mtcars))
英文:
There's two ways to do this:
If you know the keys in advance, just call it with the key
myfunc(mtcars)$mytab
If you are planning to make a longer list and want to print all of the tables in the list, use tagList
from {htmltools}
htmltools::tagList(myfunc(mtcars))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论