如何将数据框中的值直接输入到 sweave latex pdf 处理中

How to enter values from dataframe directly into sweave latex pdf processing

我在 Rstudio-Sweave 中有以下代码生成一个简单的 table:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|l|l|l|l|}
\hline
5  & 6  & 11 & 22 & 33 & 44 & 55 & 55 & \multicolumn{4}{l|}{name1} & 66 & 777 & 888 \ \hline
v1 & v2 & v3 & v4 & v5 & v6 & v7 & v8 & \multicolumn{4}{l|}{name2} & v9 & v10 & v11 \ \hline
\end{tabular}
\end{table}



\end{document}

我如何编辑代码,以便值“5”、“6”、'v1'-'v11' 等直接从我已经创建的 data.frame 中获取值.例如,如果我有一个名为 'DATAFRAME' 的数据框,是否可以做这样的事情(这是行不通的),但肯定有像这样简单的事情可以完成吗?

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|l|l|l|l|}
\hline
DATAFRAME[1,1]  & 6  & 11 & 22 & 33 & 44 & 55 & 55 & \multicolumn{4}{l|}{name1} & 66 & 777 & 888 \ \hline
v1 & v2 & v3 & DATAFRAME[4,4] & v5 & v6 & v7 & v8 & \multicolumn{4}{l|}{DATAFRAME[4,5]} & v9 & v10 & v11 \ \hline
\end{tabular}
\end{table}



\end{document}

我用从数据帧中动态获取的值替换了 table 中的值 - 这可以做到吗?

尝试使用 \Sexpr{DATAFRAME[1,1]}\Sexpr{DATAFRAME[4,4]} 等。这将从数据框中提取您指定的值。但是,如果你想一次制作一个完整的 table,一定要查看 Kristofersen 和 Imo 提到的 knitrxtable 中的选项。比重复 \Sexpr{} 调用容易得多。

编辑: 尝试以下代码,看看这是否适用于您的机器。我只是将它设置为使用 DATAFRAME 的随机数据集。

\documentclass{article}

\begin{document}

<<data, echo = FALSE>>=
set.seed(100)
DATAFRAME <- data.frame(round(rnorm(n = 10),2),
                        round(rnorm(n = 10),2),
                        round(rnorm(n = 10),2),
                        round(rnorm(n = 10),2),
                        round(rnorm(n = 10),2))
@

\SweaveOpts{concordance=TRUE}
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|l|l|l|l|}
\hline
\Sexpr{DATAFRAME[1,1]}  & 6  & 11 & 22 & 33 & 44 & 55 & 55 & \multicolumn{4}{l|}{name1} & 66 & 777 & 888 \ \hline
v1 & v2 & v3 & \Sexpr{DATAFRAME[4,4]} & v5 & v6 & v7 & v8 & \multicolumn{4}{l|}{\Sexpr{DATAFRAME[4,5]}} & v9 & v10 & v11 \ \hline
\end{tabular}
\end{table}

\end{document}