Home  >  Q&A  >  body text

Returns a sequence of DT::datatables in a list of knitted HTML documents

I have a function that returns a series of tables. I want to return them all as DT::datatable. However, I cannot get R to return these tables when they are in a list. They appear in RMarkdown files but not in knitted HTML files. Is it possible to have a table appear in an HTML document?

---
title: "Untitled"
output: html_document
---
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 will not appear in knitted HTML files:

P粉510127741P粉510127741399 days ago960

reply all(1)I'll reply

  • P粉872182023

    P粉8721820232023-09-16 11:59:51

    There are two ways to do this:

    If you know the key in advance, just call it with the key

    myfunc(mtcars)$mytab

    If you plan to make a longer list and want to print all tables in the list, use tagList from {htmltools}

    htmltools::tagList(myfunc(mtcars))

    reply
    0
  • Cancelreply