如何将 sjPlot 包中的 html sjtable 转换为 Latex [英] How to convert an html sjtable from the sjPlot package to latex

查看:13
本文介绍了如何将 sjPlot 包中的 html sjtable 转换为 Latex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sjPlot 包 (

现在我们可以读取 .html 文件并稍微清理一下:

tables <- list.clean(readHTMLTable("~/Downloads/temp.html"), fun = is.null, recursive = FALSE)表 2 = 表 [[1]] %>% 看门人::row_to_names(row_number = 1)表2 <- as.matrix(tables2) %>% as_tibble()tables2[is.na(tables2)] <- """

所以现在我们有一个干净"的 html 表.dataframe,我们可以使用 kable() 在终端中查看:

knitr::kable(tables2, format = "pipe")

通过这个最终的 kable() 调用,我们可以创建下面的乳胶代码,这是对初始表的合理近似......虽然缺少一些重要的东西(粗体 p 值,VD 顶行......)

kable(表2,格式=乳胶",booktabs = TRUE,col.names = 名称(tables2),align = c("l", rep("c", length(names(tables2)) - 1)),标题=基线测量得分的平均值和标准偏差";)

乳胶代码:

egin{table}caption{label{tab:}基线测量得分的均值和标准差}定心egin{表格}[t]{lccc}	oprule预测器和估计和CI &\midrule(拦截) &49.04 &39.23 – 58.85 &1.144e-22\汽缸&-3.41 &-5.05 – -1.76 &5.058e-05\显示&-0.15 &-0.22 – -0.07 &2.748e-04\cyl * disp &0.02 &0.01 – 0.03 &1.354e-03\N与&2&&\addlinespace观察与32&&\边际 R2/条件 R2 &0.809/NA &&\底部规则end{表格}茶几}

这是乳胶的最终结果:

当然,这是一个玩具示例,模型越复杂,格式问题就越多……

解决方案

如果对某人有用(以及对我未来的自己),我创建了一个

这似乎也适用于更复杂的表格.

感谢 tjebo,现在您可以将它作为一个包安装并在 Linux 和 Mac 上运行:remotes::install_github("gorkang/html2latex")

The sjPlot package (http://www.strengejacke.de/sjPlot) has the tab_model() function to create beautiful html tables for lots of model types. I am trying to use those tables in Overleaf, but I am not sure how to "easily" convert them to latex without losing some of the formatting, etc.

My very hacky approach has been the following (posted it in https://github.com/Rapporter/pander/issues/298).

Any help improving this would be appreciated.

Below, a reproducible example:

library(lme4)
library(sjPlot)
library(XML)
library(RCurl)
library(rlist)
library(janitor)
library(dplyr)
library(knitr)

# This is a terrible model
model = lmer(mpg ~ cyl * disp + (1|vs), mtcars)

# We save the sjPlot table to an .html file (see the table below)
sjPlot::tab_model(
  model,
  show.r2 = TRUE,
  show.icc = FALSE,
  show.re.var = FALSE,
  p.style = "scientific",
  emph.p = TRUE,
  file = "Downloads/temp.html")

Now we can read the .html file and clean it up a bit:

tables <- list.clean(readHTMLTable("~/Downloads/temp.html"), fun = is.null, recursive = FALSE)
tables2 = tables[[1]] %>% janitor::row_to_names(row_number = 1)
tables2 <- as.matrix(tables2) %>% as_tibble()
tables2[is.na(tables2)] <- ""

So now we have the html table in a "clean" dataframe, and we can use kable() to see it in the terminal:

knitr::kable(tables2, format = "pipe")

With this final kable() call we can create the latex code below, which is a reasonable approximation to the initial table... although some important things are missing (bold p values, VD top row...)

kable(
  tables2,
  format = "latex",
  booktabs = TRUE,
  col.names = names(tables2),
  align = c("l", rep("c", length(names(tables2)) - 1)),
  caption = "Means and Standard Deviations of Scores on Baseline Measures"
)

Latex code:

egin{table}

caption{label{tab:}Means and Standard Deviations of Scores on Baseline Measures}
centering
egin{tabular}[t]{lccc}
	oprule
Predictors & Estimates & CI & p\
midrule
(Intercept) & 49.04 & 39.23 – 58.85 & 1.144e-22\
cyl & -3.41 & -5.05 – -1.76 & 5.058e-05\
disp & -0.15 & -0.22 – -0.07 & 2.748e-04\
cyl * disp & 0.02 & 0.01 – 0.03 & 1.354e-03\
N vs & 2 &  & \
addlinespace
Observations & 32 &  & \
Marginal R2 / Conditional R2 & 0.809 / NA &  & \
ottomrule
end{tabular}
end{table}

This is the latex end result:

Of course, this is a toy example, with more complex models, the formatting issues pile up a bit...

解决方案

In case is useful to someone (and for my future self), I created a Github repo with a function that gets a sjPlot::tab_model() html output and builds tex (and pdf) versions of it. So, using the table above:

# Load html2pdf.R function
source("R/html2pdf.R")
    
# Create tex and pdf
html2pdf(filename = "temp.html", page_width = 13, build_pdf = TRUE, silent = TRUE)

The end result is:

This seems to work for more complex tables too.

Thanks to tjebo, now you can install this as a package and works on Linux and Mac: remotes::install_github("gorkang/html2latex")

这篇关于如何将 sjPlot 包中的 html sjtable 转换为 Latex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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