在knitr

时间:2018-02-09 10:49:43

标签: r knitr

我想知道是否可以根据要显示的数据动态重新定义表的列数和行数。也就是说,我想创建一个VAR估计结果表,它应该自动有两到三列,具体取决于内生变量的数量。 是否可以根据R矩阵对象的尺寸在LaTeX表中动态添加其他列?

我确实有以下对象,在这种情况下是一个带有三个内生变量的VAR模型。这导致data.table:

dt <-  structure(list(Var1 = c(0.769609193394589, 0.0296664895070589, 
2.23623485051503e-148, 0.168646879269767, 0.0501044748412352, 
0.00076293106259367, 0.0283276398763084, 0.0108774314407999, 
0.00920733675873214, 0.11332085808532, 0.0525935034976135, 0.0311888374773895, 
0.401036698246847, 0.129930114061118, 0.00202489145457509, 0.00013141936702598, 
0.0147083530090182, 0.992870983672657, -0.111042253384097, 0.108534218583886, 
0.306256671270779), Var2 = c(0.00219370648887803, 0.007911074537242, 
0.781553105965536, 1.08039140256749, 0.0480718334369218, 7.36951952786006e-112, 
-0.00618410770110546, 0.00319257929695447, 0.0527422189888414, 
-0.000606983838455166, 0.0115572151619192, 0.958114435594411, 
0.128307715717084, 0.0464342638236593, 0.00572355751692026, -0.00617589275766937, 
0.00646284258412713, 0.339274331468835, -0.0226517891403121, 
0.0220961058842707, 0.305293138105623), Var3 = c(0.0138066477213567, 
0.0308063385230423, 0.65402650661071, 0.0650397844072665, 0.0998573250370163, 
0.514835342240951, 0.618196004490569, 0.0636540133711865, 0.000000000000000000000268512388733353, 
0.0291100731911744, 0.0516237558174359, 0.572829608804111, -0.198986432833733, 
0.204649695705541, 0.330887875932738, 0.137626149238574, 0.0576631051748573, 
0.0169990617660536, 0.0236390793300487, 0.085763924744362, 0.782832545097234
), Type = c("Coefficient", "Standard Error", "p-value", "Coefficient", 
"Standard Error", "p-value", "Coefficient", "Standard Error", 
"p-value", "Coefficient", "Standard Error", "p-value", "Coefficient", 
"Standard Error", "p-value", "Coefficient", "Standard Error", 
"p-value", "Coefficient", "Standard Error", "p-value")), .Names = c("Var1", 
"Var2", "Var3", "Type"), class = c("data.table", "data.frame"
), row.names = c(NA, -21L))

如何动态定义LaTeX表,我为每个内生变量获取一个额外的列,但取决于内生变量的数量。当然会调整布局,也可能省略p值并用星号代替。 我将dt写入RDS文件,然后使用以下Rnw文件(与父Rnw / tex文件相关联)来编织tex文件。

<<child-test,results="hide",include=FALSE>>=
      setwd("correct working directory")


# Read in the data for the table
my.dt<- readRDS("dt.RDS")
my_inline_hook <- knitr:::.inline.hook.tex
knit_hooks$set(inline=my_inline_hook)
@
\begin{table}[htbp]
   \footnotesize
    \sisetup{table-parse-only}
 \begin{center} 
\begin{tabularx}{0.95\textwidth}{X 
                                 >{\centering}p{0.005\textwidth}
                     S[table-format=3.2
                              , round-mode=places
                              , round-precision=2
                              , table-align-text-post = false]
                     S[table-format=3.2
                              , round-mode=places
                              , round-precision=2
                              , table-align-text-post = false]
                     S[table-format=3.2
                              , round-mode=places
                              , round-precision=2
                              , table-align-text-post = false]
                     S[table-format=3.2
                              , round-mode=places
                              , round-precision=2
                              , table-align-text-post = false]

                      }
        \toprule
  & & {Mean} &       {Median} &           {Std. Dev.}         \tabularnewline
 \cmidrule{1-1} \cmidrule{3-6}
Quarterly Oil Production
                & % Empty Column
                & \Sexpr{as.character( my.dt[Type == "Coefficient", 1][1])} %
                & \Sexpr{as.character( my.dt[Type == "Coefficient", 2][1])}  %
                & \Sexpr{as.character( my.dt[Type == "Coefficient", 3][1])}%
                 \tabularnewline  
\bottomrule
\end{tabularx}\captionof{table}{Caption of table}\label{t.pvar.output}
\end{center}
\end{table}

相关问题是Iteratively producing latex tables in knitr

0 个答案:

没有答案