Is it possible to generate the RTable (FlexTable)

2019-05-22 19:56发布

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?

2条回答
smile是对你的礼貌
2楼-- · 2019-05-22 20:12

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)
}
查看更多
祖国的老花朵
3楼-- · 2019-05-22 20:29

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!

查看更多
登录 后发表回答