在 R 计算之前,如何在 knitr / LaTeX 文档中使用值和图表?
How can I use values and charts in a knitr / LaTeX document before R has calculated them?
knitr 文档包含 R 和 LaTeX 部分。我的老板想阅读摘要(在 LaTeX 中),但不想阅读 R。但是 R 应该在附录中可用,因此如果需要可以检查代码(见下文)。在附录中的 R 创建值和图表之前,如何让 LaTeX(老板)可以使用它们?
\documentclass[12pt, a4paper]{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
\section{For the Boss}
The average is 3.3. Surely this should be calculated?
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
我可以像这样重用块名称(见下文),但现在所有 R 代码都在文档的开头,并与附录中的周围讨论分开。在这个演示中这不是问题,但我正在处理的实际文档很大。
\documentclass{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
<<data, echo=FALSE>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
<<statistics, echo=FALSE>>=
the.mean <- mean(df$n)
@
\section{For the Boss}
The average is \Sexpr{the.mean}.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, eval=FALSE>>=
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, eval=FALSE>>=
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
以稍微不同的方式重用这些块(感谢@george-dontas)让我得到了我想要的。 R前的计算值和R及其讨论在附录中。
\documentclass{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
<<*, echo=FALSE, include=FALSE>>=
<<data>>
<<chart>>
<<statistics>>
@
\section{For the Boss}
The average is \Sexpr{the.mean}.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
我不太整洁的解决方案(如果有很多块或只有几个变量可能会有用)可能是使用 LaTeX 支持文件。
\documentclass{article}
\IfFileExists{\jobname.var}%
{%
\input{\jobname.var}%
}{}%
\newwrite\variablesfile
\immediate\openout\variablesfile=\jobname.var
\newcommand{\newvariable}[2]{%
\immediate\write\variablesfile{
\string\newcommand{\string #1}{#2}
}
}%
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
\section{For the Boss}
The average is \mean.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
\newvariable{\mean}{\Sexpr{the.mean}}
The arithmetic mean is \mean.
\end{document}
knitr 文档包含 R 和 LaTeX 部分。我的老板想阅读摘要(在 LaTeX 中),但不想阅读 R。但是 R 应该在附录中可用,因此如果需要可以检查代码(见下文)。在附录中的 R 创建值和图表之前,如何让 LaTeX(老板)可以使用它们?
\documentclass[12pt, a4paper]{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
\section{For the Boss}
The average is 3.3. Surely this should be calculated?
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
我可以像这样重用块名称(见下文),但现在所有 R 代码都在文档的开头,并与附录中的周围讨论分开。在这个演示中这不是问题,但我正在处理的实际文档很大。
\documentclass{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
<<data, echo=FALSE>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
<<statistics, echo=FALSE>>=
the.mean <- mean(df$n)
@
\section{For the Boss}
The average is \Sexpr{the.mean}.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, eval=FALSE>>=
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, eval=FALSE>>=
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
以稍微不同的方式重用这些块(感谢@george-dontas)让我得到了我想要的。 R前的计算值和R及其讨论在附录中。
\documentclass{article}
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
<<*, echo=FALSE, include=FALSE>>=
<<data>>
<<chart>>
<<statistics>>
@
\section{For the Boss}
The average is \Sexpr{the.mean}.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
The arithmetic mean is \Sexpr{the.mean}.
\end{document}
我不太整洁的解决方案(如果有很多块或只有几个变量可能会有用)可能是使用 LaTeX 支持文件。
\documentclass{article}
\IfFileExists{\jobname.var}%
{%
\input{\jobname.var}%
}{}%
\newwrite\variablesfile
\immediate\openout\variablesfile=\jobname.var
\newcommand{\newvariable}[2]{%
\immediate\write\variablesfile{
\string\newcommand{\string #1}{#2}
}
}%
\begin{document}
\title{For Bosses and R Experts}
\author{Joe Collins}
\maketitle
\section{For the Boss}
The average is \mean.
\appendix
\section{For The R Expert}
\subsection{Data}
<<data, echo=TRUE, results='markup'>>=
n = c(2, 3, 5)
s = c("One", "Two", "Three")
df = data.frame(n, s)
@
\subsection{Chart}
Show a chart.
<<chart, echo=TRUE, fig.height=3, fig.lp="chart">>=
barplot(df$n, names.arg=df$s)
@
\subsection{Statistics}
Calculate the mean.
<<statistics, echo=TRUE>>=
the.mean <- mean(df$n)
@
\newvariable{\mean}{\Sexpr{the.mean}}
The arithmetic mean is \mean.
\end{document}