how to render DT::datatables in a pdf using rmarkd

2019-07-02 09:06发布

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)

2条回答
霸刀☆藐视天下
2楼-- · 2019-07-02 09:30

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)

查看更多
你好瞎i
3楼-- · 2019-07-02 09:30

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.

查看更多
登录 后发表回答