Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to render DT::datatables in a pdf using rmarkdown?

How can I display DT::datatable objects from a rmarkdown script onto a pdf document? My code so far is breaks down with the following error:

processing file: reportCopy.Rmd
output file: reportCopy.knit.md
Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML.

Including always_allow_html: yes in the YAML header suppresses the error, but nothing appears on the pdf.

I would be grateful for any help. My code is currently:

---
title: "DT"
output: pdf_document
---

### Chart 1
```{r}
DT::datatable(head(mtcars))
```

( I don't know if it matters, but my datatables are in fact created in a shiny application. Ideally, I would have liked to have the prerendered tables simply dumped into the rmarkdown script... but I switched tactic and now try to render the tables directly in the rmarkdown code)

like image 432
hartmut Avatar asked Jun 14 '17 11:06

hartmut


2 Answers

Since knitr v1.13, HTML widgets will be rendered automatically as screenshots taken via the webshot package.

You need to install the webshot package and PhantomJS:

install.packages("webshot")
webshot::install_phantomjs()

(see https://bookdown.org/yihui/bookdown/html-widgets.html)

like image 167
bergant Avatar answered Sep 21 '22 11:09

bergant


You cannot usedatatable in pdf_document (datatable is interactive, pdf is static), only in html_document! The only possibility for PDF is to use the kable or for example pandoc.table

--> if You really wanna get the look of datatable and as You said datatable is created in a shiny application, then You can have a look at the webshot package, which is going to create a screenshot of Your datatable from shiny app which You can use further in pdf as a image.

like image 28
Mal_a Avatar answered Sep 20 '22 11:09

Mal_a