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

萝らか妹 提交于 2019-12-12 10:48:52

问题


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)


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/44543858/how-to-render-dtdatatables-in-a-pdf-using-rmarkdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!