Is it possible to generate the RTable (FlexTable) in pdf with RMarkdown?

狂风中的少年 提交于 2019-12-12 10:10:58

问题


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?


回答1:


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:

insert_screenshot = function(x) {
  if (!inherits(x, c('html', 'shiny.tag'))) return()
  htmltools::save_html(x, 'temp.html')
  res = webshot::webshot('temp.html', 'my-screenshot.png')
  knitr::include_graphics(res)
}



回答2:


Thanks for @Yihui,

I figured out this issue. Basically, the solution is to take the screenshot by webshot function and knitr::include_graphics to insert this png file into the pdf output.

Please try this piece of code in your markdown:

```{r TableJiena, out.width = "700px", out.length = "400px"}

insert_screenshot = function(x) {
  if (!inherits(x, c('html', 'shiny.tag'))) return()
  htmltools::save_html(x, 'temp.html')
  res = webshot::webshot('temp.html', 'my-screenshot.png')
  knitr::include_graphics(res)
}
insert_screenshot(htmltools::HTML(as.html(vanilla.table(head(iris)))))
```

If you want to get the simplified code, please try this piece of code in Markdown.

```{r TableJiena, out.width = "700px", out.length = "400px"}
webshot::webshot(htmltools::HTML(as.html(vanilla.table(head(iris)))), 'my-screenshot.png')
knitr::include_graphics('my-screenshot.png')
```

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!



来源:https://stackoverflow.com/questions/40516169/is-it-possible-to-generate-the-rtable-flextable-in-pdf-with-rmarkdown

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