I am just curious that is it possible to generate the RTable (FlexTable) in pdf with RMarkdown? I can generate it in html output but it is not working for pdf output. I google this question but there is no exact answer for it.
My code:
```{r, echo=FALSE, results='asis'}
library(ReporteRsjars)
library(ReporteRs)
library(rtable)
library(dplyr)
vanilla.table(iris)
```
Since it can generate in word, I assume it is possible for the pdf one.
I have tried cat(as.html(vanilla.table(iris)))
but it is not working.
Can I friendly ask if you have any idea of it?
This is not meant to be an answer, but only a pointer to the possible direction to solve this issue. In general, HTML output in R code chunks of an R Markdown document won't work for PDF output, just because HTML and LaTeX are totally different. However, there is an indirect way to get there, which is to take a screenshot of the HTML output and insert an image instead. This approach is used in knitr to deal with HTML widgets when the output format is not HTML. You can find the technical details in https://github.com/yihui/knitr/blob/master/R/plot.R (see the
html_screenshot()
function).The basic idea is you save the HTML output as an
*.html
file, take a screenshot using the webshot package (which requires PhantomJS), and return the image to knitr. It should not be too difficult to generalize the idea to any HTML output, but I have not thought about it very carefully. This does not mean you cannot implement it by yourself, though. Below is a sketch I typed out of my mind and there is certainly a lot of details to improve:Thanks for @Yihui,
I figured out this issue. Basically, the solution is to take the screenshot by
webshot
function andknitr::include_graphics
to insert this png file into the pdf output.Please try this piece of code in your markdown:
If you want to get the simplified code, please try this piece of code in Markdown.
But there is a tiny issue of this solution: the solution of the PNG picture is not very high and I don't know why there are flows between each column. Also, some single lines are printed in double lines.
Can anyone figure out how to solve this minor issue of
webshot
?Thank you!