是否可以使用RMarkdown以pdf格式生成RTable(FlexTable)? [英] Is it possible to generate the RTable (FlexTable) in pdf with RMarkdown?

查看:111
本文介绍了是否可以使用RMarkdown以pdf格式生成RTable(FlexTable)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,是否可以使用RMarkdown以pdf格式生成RTable(FlexTable)?我可以在html输出中生成它,但不适用于pdf输出.我用谷歌搜索了这个问题,但没有确切的答案.

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.

我的代码:

```{r, echo=FALSE, results='asis'}
library(ReporteRsjars)
library(ReporteRs)
library(rtable)
library(dplyr)
vanilla.table(iris)
```

由于它可以用文字生成,因此我认为pdf格式是可能的.

Since it can generate in word, I assume it is possible for the pdf one.

我尝试了cat(as.html(vanilla.table(iris))),但是它不起作用.

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?

推荐答案

感谢@Yihui

我发现了这个问题.基本上,解决方案是使用webshot函数和knitr::include_graphics截取屏幕截图,以将此png文件插入pdf输出.

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)))))
```

如果您想获得简化的代码,请在Markdown中尝试这段代码.

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')
```

但是这种解决方案有一个小问题: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.

有人能弄清楚如何解决webshot的这个小问题吗?

Can anyone figure out how to solve this minor issue of webshot?

谢谢!

这篇关于是否可以使用RMarkdown以pdf格式生成RTable(FlexTable)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆