R 如何将html sjtable从sjPlot包转换为latex

R 如何将html sjtable从sjPlot包转换为latex,r,latex,R,Latex,sjPlot包()具有tab_model()函数,可以为许多模型类型创建漂亮的html表。我正试图在背面使用这些表,但我不知道如何“轻松”地将它们转换为latex而不丢失一些格式等等 我的方法非常粗糙,如下所示 如有任何改进,我们将不胜感激 下面是一个可复制的示例: library(lme4) library(sjPlot) library(XML) library(RCurl) library(rlist) library(janitor) library(dplyr) library(kni

sjPlot包()具有tab_model()函数,可以为许多模型类型创建漂亮的html表。我正试图在背面使用这些表,但我不知道如何“轻松”地将它们转换为latex而不丢失一些格式等等

我的方法非常粗糙,如下所示

如有任何改进,我们将不胜感激

下面是一个可复制的示例:

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

现在,我们可以读取.html文件并对其进行清理:

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)] <- ""
通过最后的kable()调用,我们可以创建下面的latex代码,这是与初始表的合理近似。。。尽管缺少一些重要内容(粗体p值、顶行VD…)

乳胶代码:

\begin{table}

\caption{\label{tab:}Means and Standard Deviations of Scores on Baseline Measures}
\centering
\begin{tabular}[t]{lccc}
\toprule
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 &  & \\
\bottomrule
\end{tabular}
\end{table}
这就是最终结果:


当然,这是一个玩具示例,对于更复杂的模型,格式问题有点堆积…

我认为不可能直接从sJPlot生成LaTeX结果。 在提供直接乳胶输出的同时,stargazer包可能是一个合适的替代品:

library(lme4)
library(stargazer)

# This is a terrible model
model = lmer(mpg ~ cyl * disp + (1|vs), mtcars)
stargazer(model, title="Regression Results", align=TRUE)
对于以下结果:


如果对某人有用(对我未来的自己也有用),我创建了一个函数,该函数获取
sjPlot::tab_model()
html输出,并构建它的tex(和pdf)版本。因此,使用上表:

#加载html2pdf.R函数
来源(“R/html2pdf.R”)
#创建tex和pdf
html2pdf(filename=“temp.html”,页面宽度=13,构建pdf=TRUE,无提示=TRUE)
最终结果是:

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


感谢tjebo,现在您可以将其作为软件包安装并在Linux和Mac上运行:
remotes::install\u github(“gorkang/html2latex”)

感谢您的回复!stargazer还可以,但是sjPlot有很多有趣的特性我想保留。正如您所说,不可能使用sjPlot直接创建latex输出,。。。但正如他们所说,有志者事竟成:)
\begin{table}

\caption{\label{tab:}Means and Standard Deviations of Scores on Baseline Measures}
\centering
\begin{tabular}[t]{lccc}
\toprule
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 &  & \\
\bottomrule
\end{tabular}
\end{table}
library(lme4)
library(stargazer)

# This is a terrible model
model = lmer(mpg ~ cyl * disp + (1|vs), mtcars)
stargazer(model, title="Regression Results", align=TRUE)