RMarkdown表中作为变量名的动态希腊字母
如何让xtable(尽管我在pander.table中也有这个问题)将希腊字母分配给print函数中数据帧的列,而无需呈现表格,然后手动键入希腊字母的Latex 以下是一个可复制示例的数据:RMarkdown表中作为变量名的动态希腊字母,r,r-markdown,reproducible-research,R,R Markdown,Reproducible Research,如何让xtable(尽管我在pander.table中也有这个问题)将希腊字母分配给print函数中数据帧的列,而无需呈现表格,然后手动键入希腊字母的Latex 以下是一个可复制示例的数据: #data in chiSq <- 1600 df <- 850 p <- 0.95 CFI <- 0.95 TLI <- 0.95 RMSEA <- 0.04 LOWRMSEA <- 0.03 HIGHRMSEA <- 0.04 这就产生了: \beg
#data in
chiSq <- 1600
df <- 850
p <- 0.95
CFI <- 0.95
TLI <- 0.95
RMSEA <- 0.04
LOWRMSEA <- 0.03
HIGHRMSEA <- 0.04
这就产生了:
\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA}
\begin{tabular}{rrrrrrrrr}
\hline
& chiSq & df & p & CFI & TLI & RMSEA & LOWRMSEA & HIGHRMSEA \\
\hline
1 & 1600.00 & 850.00 & 0.95 & 0.95 & 0.95 & 0.04 & 0.03 & 0.04 \\
\hline
\end{tabular}
\end{table}
但是,我需要手动编辑该表以创建以下内容:
\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA}
\begin{tabular}{rrrrrrrrrrrr}
\hline
& $x^2$ & {\it df} & {\it p} & CFI & TLI & RMSEA\\
\hline
&1600.00 & 850.00 & 0.00 & 0.95 & 0.95 & 0.04 (0.03 - 0.04) \\
\hline
\end{tabular}
\end{table}
我希望能够在不手动编辑表的情况下动态执行此操作,以便可以将其作为代码块包含在markdown文档中。
谢谢。至于合并RMSEA&LOWRMSEA&HIGHRMSEA值,最好在data.frame中粘贴。比如说
fit.stat <- data.frame(chiSq, df, p, CFI, TLI,
RMSEA = paste0(RMSEA , " (",LOWRMSEA," - ", HIGHRMSEA,")"))
请注意,我做了一些工作来获取默认的sanitize
函数,并在列上运行该函数,以确保没有任何问题。然后我开始替换要更改的值。请注意,对于“it”类的东西,我们必须使用两个转义斜杠。但是我们使用的函数如下
print(xtable(fit.stat, caption = "Model Fit Information for CFA"),
caption.placement="top", sanitize.colnames.function = formatcolheads,
type = "latex")
这就产生了
\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA}
\begin{tabular}{rrrrrrl}
\hline
& $x^2$ & {\it df} & {\it p} & CFI & TLI & RMSEA \\
\hline
1 & 1600.00 & 850.00 & 0.95 & 0.95 & 0.95 & 0.04 (0.03 - 0.04) \\
\hline
\end{tabular}
\end{table}
这似乎是您想要的输出。您能解释得更清楚些吗?我在表中使用希腊字母没有问题。您的问题是“您应该手动编辑吗?”?“如果是这样的话,那就太离题了。如果你想帮助你编写代码,这样你就不必做那些编辑了,你应该做一个例子来说明问题,并澄清所需的输出@Flick先生:我想增加投票,但我需要等我在这里呆足够长的时间才能获得许可。这是一个很好的解决方案,应该也适用于pander.table。您使用
sanitize
功能大大改进了我的可复制工作流。注意,在您发布的解决方案中x@bfoste01抢手货我修好了。
formatcolheads<-function(x) {
sanitize<-get("sanitize", parent.frame())
x<-sanitize(x)
x<-gsub("chiSq","$x^2$",x)
x<-gsub("df","{\\\\it df}",x)
x<-gsub("p","{\\\\it p}",x)
x
}
print(xtable(fit.stat, caption = "Model Fit Information for CFA"),
caption.placement="top", sanitize.colnames.function = formatcolheads,
type = "latex")
\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA}
\begin{tabular}{rrrrrrl}
\hline
& $x^2$ & {\it df} & {\it p} & CFI & TLI & RMSEA \\
\hline
1 & 1600.00 & 850.00 & 0.95 & 0.95 & 0.95 & 0.04 (0.03 - 0.04) \\
\hline
\end{tabular}
\end{table}