texreg在Rmarkdown/knitr中
简单问题:我可以使用texreg在Rmarkdown/knitr中,r,knitr,r-markdown,texreg,R,Knitr,R Markdown,Texreg,简单问题:我可以使用knitr在Rmarkdown中的markdown(非LaTex)文档中使用texreg 我以为我可以,但似乎我错了?我可以将LaTex代码包含在Rmarkdown中,用于等式,但对于带有texreg的表,我没有成功(至少在剩余文档以markdown编写时是这样) 我得到这个错误: pandoc document conversion failed with error 43 不管我使用的是texreg()(用于LatTex)、htmlreg(),还是screenreg()
knitr
在Rmarkdown中的markdown(非LaTex)文档中使用texreg
我以为我可以,但似乎我错了?我可以将LaTex代码包含在Rmarkdown中,用于等式,但对于带有texreg
的表,我没有成功(至少在剩余文档以markdown编写时是这样)
我得到这个错误:
pandoc document conversion failed with error 43
不管我使用的是texreg()
(用于LatTex)、htmlreg()
,还是screenreg()
,我都无法在PDF文档中找到一个合适的表格
---
title: "Title"
fontsize: 12pt
output:
pdf_document:
latex_engine: xelatex
---
然后尝试开发一个表:
```{r Table_with_texreg, results="asis"}
library(texreg)
modellist = list(model1, model2, model3, model4, model5)
texreg(modellist, type = 'un',
summaries = c('ChiSqM_Value', 'ChiSqM_DF', 'ChiSqM_DF', 'ChiSqM_PValue', 'CFI',
'RMSEA_Estimate'), single.row=TRUE)
(texreg()
之后的一些参数是用于MplusAutomation
包的命令,用于指定哪些参数(未标准化)以及哪些模型适合使用索引。对于问题/帖子来说,这些参数不应具有任何意义。)
这是编织时生成的LaTex代码,导致转换错误43:
\begin{tabular}{l c c c c c }
\hline
& Model 1 & Model 2 & Model 3 & Model 4 & Model 5 \\
\hline
DISCRIM$<$-AGEA & $-0.00 \; (0.00)^{***}$ & $-0.01 \; (0.00)^{***}$ & $-0.01 \; (0.00)^{***}$ & $-0.00 \; (0.00)^{***}$ & $-0.01 \; (0.00)^{***}$ \\
DISCRIM$<$-GNDR & $0.05 \; (0.02)^{**}$ & $0.07 \; (0.02)^{***}$ & $0.09 \; (0.02)^{***}$ & $0.03 \; (0.02)^{*}$ & $0.07 \; (0.02)^{***}$ \\
DISCRIM$<$-MINORITY & $0.14 \; (0.04)^{***}$ & $0.17 \; (0.04)^{***}$ & $0.13 \; (0.04)^{***}$ & $0.19 \; (0.04)^{***}$ & $0.19 \; (0.04)^{***}$ \\
PDJSEX\_R$<$-AGEA & $-0.01 \; (0.00)^{***}$ & & & $-0.01 \; (0.00)^{***}$ & \\
PDJSEX\_R$<$-GNDR & $0.38 \; (0.03)^{***}$ & & & $0.38 \; (0.03)^{***}$ & \\
PDJSEX\_R$<$-MINORITY & $0.11 \; (0.04)^{**}$ & & & $0.11 \; (0.04)^{**}$ & \\
PDJETN\_R$<$-AGEA & $-0.00 \; (0.00)^{***}$ & & $-0.00 \; (0.00)^{***}$ & & \\
PDJETN\_R$<$-GNDR & $-0.02 \; (0.02)$ & & $-0.02 \; (0.02)$ & & \\
PDJETN\_R$<$-MINORITY & $0.94 \; (0.08)^{***}$ & & $0.93 \; (0.08)^{***}$ & & \\
PREDJ\_R$<$-DISCRIM & $1.00 \; (0.00)$ & $1.00 \; (0.00)$ & $1.00 \; (0.00)$ & $1.00 \; (0.00)$ & $1.00 \; (0.00)$ \\
LKRSP\_R$<$-DISCRIM & $1.05 \; (0.01)^{***}$ & $1.08 \; (0.01)^{***}$ & $1.08 \; (0.01)^{***}$ & $1.08 \; (0.01)^{***}$ & \\
TRTBD\_R$<$-DISCRIM & $1.02 \; (0.01)^{***}$ & $1.05 \; (0.01)^{***}$ & $1.05 \; (0.01)^{***}$ & $1.05 \; (0.01)^{***}$ & \\
PDJSEX\_R$<$-$>$DISCRIM & $0.74 \; (0.02)^{***}$ & & & $0.76 \; (0.02)^{***}$ & \\
PDJETN\_R$<$-$>$DISCRIM & $0.62 \; (0.02)^{***}$ & & $0.66 \; (0.02)^{***}$ & & \\
PDJETN\_R$<$-$>$PDJSEX\_R & $0.78 \; (0.02)^{***}$ & & & & \\
PREDJ_R$1<-Thresholds & $0.19 \; (0.10)^{*}$ & $0.19 \; (0.10)^{*}$ & $0.18 \; (0.10)$ & $0.18 \; (0.10)$ & $0.18 \; (0.10)$ \\
PREDJ_R$2<-Thresholds & $0.71 \; (0.09)^{***}$ & $0.71 \; (0.09)^{***}$ & $0.70 \; (0.09)^{***}$ & $0.70 \; (0.09)^{***}$ & $0.70 \; (0.09)^{***}$ \\
LKRSP_R$1<-Thresholds & $0.14 \; (0.07)$ & $0.13 \; (0.07)$ & $0.15 \; (0.07)^{*}$ & $0.14 \; (0.07)$ & $0.14 \; (0.07)$ \\
LKRSP_R$2<-Thresholds & $0.72 \; (0.07)^{***}$ & $0.72 \; (0.07)^{***}$ & $0.73 \; (0.07)^{***}$ & $0.72 \; (0.07)^{***}$ & $0.72 \; (0.07)^{***}$ \\
TRTBD_R$1<-Thresholds & $0.51 \; (0.06)^{***}$ & $0.51 \; (0.06)^{***}$ & $0.50 \; (0.06)^{***}$ & $0.51 \; (0.06)^{***}$ & $0.50 \; (0.06)^{***}$ \\
TRTBD_R$2<-Thresholds & $1.11 \; (0.07)^{***}$ & $1.11 \; (0.07)^{***}$ & $1.12 \; (0.07)^{***}$ & $1.11 \; (0.07)^{***}$ & $1.11 \; (0.07)^{***}$ \\
PDJSEX_R$1<-Thresholds & $0.86 \; (0.06)^{***}$ & $0.86 \; (0.06)^{***}$ & $0.87 \; (0.06)^{***}$ & $0.86 \; (0.06)^{***}$ & $0.86 \; (0.06)^{***}$ \\
PDJSEX_R$2<-Thresholds & $1.44 \; (0.06)^{***}$ & $1.44 \; (0.06)^{***}$ & $1.44 \; (0.06)^{***}$ & $1.44 \; (0.06)^{***}$ & $1.44 \; (0.06)^{***}$ \\
PDJETN_R$1<-Thresholds & $0.80 \; (0.07)^{***}$ & $0.79 \; (0.07)^{***}$ & $0.80 \; (0.07)^{***}$ & $0.79 \; (0.07)^{***}$ & $0.80 \; (0.07)^{***}$ \\
PDJETN_R$2<-Thresholds & $1.29 \; (0.09)^{***}$ & $1.29 \; (0.09)^{***}$ & $1.29 \; (0.09)^{***}$ & $1.29 \; (0.09)^{***}$ & $1.29 \; (0.09)^{***}$ \\
DISCRIM$<$-$>$DISCRIM & $0.79 \; (0.02)^{***}$ & $0.74 \; (0.02)^{***}$ & $0.74 \; (0.02)^{***}$ & $0.75 \; (0.02)^{***}$ & $0.84 \; (0.01)^{***}$ \\
PDJSEX\_R$<$-DISCRIM & & $1.03 \; (0.01)^{***}$ & $1.03 \; (0.01)^{***}$ & & $0.98 \; (0.01)^{***}$ \\
PDJETN\_R$<$-DISCRIM & & $0.90 \; (0.01)^{***}$ & & $0.90 \; (0.01)^{***}$ & $0.87 \; (0.01)^{***}$ \\
LKRSP\_R$<$-AGEA & & & & & $-0.01 \; (0.00)^{***}$ \\
LKRSP\_R$<$-GNDR & & & & & $0.06 \; (0.02)^{***}$ \\
LKRSP\_R$<$-MINORITY & & & & & $0.14 \; (0.04)^{***}$ \\
TRTBD\_R$<$-AGEA & & & & & $-0.00 \; (0.00)^{**}$ \\
TRTBD\_R$<$-GNDR & & & & & $0.06 \; (0.02)^{**}$ \\
TRTBD\_R$<$-MINORITY & & & & & $0.20 \; (0.05)^{***}$ \\
LKRSP\_R$<$-$>$DISCRIM & & & & & $0.74 \; (0.02)^{***}$ \\
TRTBD\_R$<$-$>$DISCRIM & & & & & $0.72 \; (0.02)^{***}$ \\
TRTBD\_R$<$-$>$LKRSP\_R & & & & & $0.88 \; (0.01)^{***}$ \\
\hline
ChiSqM_Value & 331.41 & 808.73 & 776.77 & 568.88 & 648.83 \\
ChiSqM_DF & 10 & 17 & 14 & 14 & 10 \\
ChiSqM_PValue & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\
CFI & 0.97 & 0.93 & 0.94 & 0.95 & 0.95 \\
RMSEA_Estimate & 0.02 & 0.03 & 0.03 & 0.03 & 0.03 \\
\hline
\multicolumn{6}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\begin{tabular}{lc}
\赫林
&模型1、模型2、模型3、模型4和模型5\\
\赫林
DISCRIM$您正在编写您想要生成PDF,但不使用LaTeX
。您确定这一点吗?我不知道如何做到这一点,但这里有两种解决方案,用于生成a)HTML输出和b)使用LaTeX生成PDF,这两种方法都使用texreg
:
解决方案1(用于HTML输出):
---
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
```{r,echo=FALSE,message=FALSE}
ctl感谢您的评论,感谢您开发了一个很棒的软件包!也许我得用乳胶。在学习了R的基础知识之后,我发现学习LaTex也是一个相当陡峭的学习曲线。(在Rmarkdown中生成PDF没有问题,除非我添加了表。)谢谢您的代码!当然很高兴你发现它有用!不过,老实说,我不明白为什么你会认为第二种解决方案的学习曲线更陡。您仍然编写降价代码,它只是在内部转换为LaTeX,因为您希望生成PDF。正如你所看到的,这两种解决方案的代码非常相似。谢谢,你的代码在编织到PDF时工作得很好,但我的代码却不是。。。我添加了texreg生成的LaTex代码,该代码导致转换错误。感谢您在更新的问题中提供了更详细的示例。您需要双转义下划线等。例如,您应该在系数名称中写入\\\\\\
,而不是\\\\
。如果不想更改模型中的系数名称,可以使用custom.coef.names
参数来执行此操作。我不知道这是否是唯一的问题,但这绝对是一个障碍。如果您需要更多帮助,您应该提供一个最小的自包含示例,从头开始重新生成您的表。例如,在PREDJ_R$1中
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE, message = FALSE}
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
library("texreg")
```
Some equation:
$$a + b = 2$$
Table follows:
```{r, results = 'asis', echo = FALSE}
htmlreg(lm.D9, star.symbol = "\\*", doctype = FALSE, center = FALSE, caption = "")
```
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE, message = FALSE}
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
library("texreg")
```
Some equation:
$$a + b = 2$$
Table follows:
```{r, results = 'asis', echo = FALSE}
texreg(lm.D9, table = FALSE, use.packages = FALSE)
```