- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
sjPlot 包 ( http://www.strengejacke.de/sjPlot ) 具有 tab_model() 函数,可以为许多模型类型创建漂亮的 html 表格。我正在尝试在 Overleaf 中使用这些表格,但我不确定如何“轻松”将它们转换为 Latex 而不会丢失一些格式等。
我非常老套的方法如下(发布在 https://github.com/Rapporter/pander/issues/298 中)。
我们将不胜感激任何改进此方面的帮助。
下面是一个可重现的例子:
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)] <- ""
现在我们在“干净”的数据框中有了 html 表,我们可以使用 kable() 在终端中查看它:
knitr::kable(tables2, format = "pipe")
通过最后的 kable() 调用,我们可以创建下面的 latex 代码,这是对初始表的合理近似……尽管缺少一些重要的东西(粗体 p 值、VD 顶行……)
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 代码:
\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}
这是 latex 的最终结果:
当然,这是一个玩具示例,模型更复杂,格式问题有点堆积......
最佳答案
如果对某人(以及我 future 的自己)有用,我创建了一个 Github repo具有获取 sjPlot::tab_model()
html 输出并构建其 tex(和 pdf)版本的函数。因此,使用上表:
# Load html2pdf.R function
source("R/html2pdf.R")
# Create tex and pdf
html2pdf(filename = "temp.html", page_width = 13, build_pdf = TRUE, silent = TRUE)
最终结果是:
这似乎也适用于更复杂的表格。
感谢 tjebo,现在您可以将它作为一个包安装并在 Linux 和 Mac 上运行:remotes::install_github("gorkang/html2latex")
关于r - 如何将 sjPlot 包中的 html sjtable 转换为 Latex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63053465/
sjPlot 包 ( http://www.strengejacke.de/sjPlot ) 具有 tab_model() 函数,可以为许多模型类型创建漂亮的 html 表格。我正在尝试在 Overl
sjPlot 包 ( http://www.strengejacke.de/sjPlot ) 具有 tab_model() 函数,可以为许多模型类型创建漂亮的 html 表格。我正在尝试在 Overl
我是一名优秀的程序员,十分优秀!