2017-02-12 58 views
1

簡單的問題:我可以在使用knitr的Rmarkdown的降價(而非LaTex)文件中使用texreg嗎?texreg in Rmarkdown/knitr

我以爲我可以,但似乎我錯了?我可以包括方程中Rmarkdown LaTex的代碼,但有一個與texreg表沒有成功(至少在剩餘的文件是寫在降價

我得到這個錯誤:

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包,指定哪個參數(未標準化),並且模型擬合指數使用的命令。不應該對問題/後任何意義)

這是進行編織時,導致丁文錯誤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} 

P.S.編織到HTML(不PDF)和使用htmlreg(不是texreg)似乎工作正常。不過,我寧願使用PDF。

回答

1

您正在編寫要生成PDF,但不使用LaTeX。你確定嗎?我不知道該怎麼做,但這裏是生成)的HTML輸出兩種溶液B)的PDF用乳膠,用texreg兩個:

解決方案1(對於HTML輸出):

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

這裏是輸出:

HTML output

溶液2(PDF輸出):

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

這裏是輸出:

PDF output

+0

感謝您的評論,並感謝開發大包!也許我必須使用LaTex。在學習了R的基礎知識之後,我發現學習LaTex也是一個相當陡峭的學習曲線。 (除非添加表格,否則在Rmarkdown中生成PDF是沒有問題的。)感謝代碼! – cibr

+0

當然!很高興你覺得它很有用!但說實話,我不明白你爲什麼會認爲第二種解決方案的學習曲線更陡。您仍然編寫Markdown代碼,它只是在內部轉換爲LaTeX,因爲您要生成PDF。如您所見,這兩種解決方案的代碼非常相似。 –

+0

謝謝,當編織成PDF時,你的代碼工作正常,但不是我的......我添加了由texreg生成的LaTex代碼,此代碼導致轉換錯誤。 – cibr