简体   繁体   English

如何让 tab_model 在 Rmarkdown PDF 中正确显示?

[英]How to I get tab_model to display properly in Rmarkdown PDF?

I realize this is probably quite simple, but I have looked and can't find an example of how to use sjPlot::tab_model in an Rmarkdown document sent to PDF.我意识到这可能很简单,但我已经查看并找不到如何在发送到 PDF 的 Rmarkdown 文档中使用 sjPlot::tab_model 的示例。

Here is reproducible code:这是可重现的代码:

---
title: "test"
author: "Me"
date: "2020/12/6"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(sjPlot)

A table一张桌子

tab_model(lm(mpg~cyl, data=mtcars))

And what I get looks something like this:我得到的看起来像这样:

A table tab_model(lm(mpg~cyl, data=mtcars)) mpg Predictors Estimates CI p (Intercept) 37.88 33.65 – 42.12 <0.001 cyl -2.88 -3.53 – -2.22 <0.001 Observations 32 R2 / R2 adjusted表格 tab_model(lm(mpg~cyl, data=mtcars)) mpg 预测器估计 CI p(拦截) 37.88 33.65 – 42.12 <0.001 cyl -2.88 -3.53 – -2.22 <0.001 观察值 32 R2 / R2 调整

I have tried to save the html within the tab_model function and then read it into the Rmd doc Include HTML files in R Markdown file?我试图将 html 保存在tab_model function 中,然后将其读入 Rmd doc 在 R Markdown 文件中包含 HTML 文件? . .

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(sjPlot)
```

```{r plot}

tab_model(lm(mpg~cyl, data=mtcars),file="test.html")

```


```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```

But I get the same problem, which makes me think there is some option for formatting html tables in Rmd, but I can't seem to find a solution.但我遇到了同样的问题,这让我觉得有一些选项可以在 Rmd 中格式化 html 表,但我似乎找不到解决方案。

There is a related post here: https://stackoverflow.com/a/33182503/9096420 , but tab_model doesn't work well with the code there, because it requires a data frame.这里有一个相关的帖子: https://stackoverflow.com/a/33182503/9096420 ,但是 tab_model 不能很好地处理那里的代码,因为它需要一个数据框。

I think there is a bit of a hack that works, but isn't ideal.我认为有一些技巧可行,但并不理想。 You can save the output as a png and then include it in the Rmd doc https://stackoverflow.com/a/25167279/9096420 .您可以将 output 保存为 png,然后将其包含在 Rmd 文档https://stackoverflow.com/a/25167279/9096420中。

---
title: "Untitled"
author: "Dylan Gomes"
date: "12/7/2020"
output: pdf_document
---
![Caption for the picture.](test.png) ## this file 'test.png' needs to be in the working directory.

在此处输入图像描述

This is a well known issue, and the creator of the package (Daniel) responded here: https://github.com/strengejacke/sjPlot/issues/712这是一个众所周知的问题,package 的创建者(丹尼尔)在这里回复: https://github.com/strengejacke/sjPlot/issues/712

In the meantime, I ended up creating a couple functions to help out.与此同时,我最终创建了几个函数来提供帮助。

Please see the Github repo https://github.com/gorkang/html2latex/ .请参阅 Github 回购https://github.com/gorkang/html2latex/

In the repo you can find an example.Rmd file with step-by-step instructions.在 repo 中,您可以找到带有分步说明的 example.Rmd 文件。 It works in a Ubuntu 20.04 system.它在 Ubuntu 20.04 系统中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM