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}
答案 0 :(得分:0)
Reusing the chunks in a slightly different way (thanks @george-dontas) gets me what I want. The calculated values before the R and the R with its discussion in the appendix.
\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}
答案 1 :(得分:0)
I less tidy solution (which might be useful if there are a lot of chunks or only a few variables) might be to use a LaTeX support file.
\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}