在创建变量的代码块之前使用R变量 [英] Using an R variable before the code chunk in which the variable was created

查看:72
本文介绍了在创建变量的代码块之前使用R变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在摘要中包含一个R计算. R的计算位于文档的底部,因此当我编译Rnw文件时遇到错误.

I would like to include an R calculation in the abstract. The R calculation is at the bottom of document, so when I compile the Rnw file I get an error.

这是一个最小的示例:

\documentclass{article}
\begin{document}

\begin{abstract}
    This paper... and we got a mean of \Sexpr{mean.data}.
\end{abstract}

<<>>=
data <- c(1,2,3,4,5)
mean.data <- mean(data)
@

\end{document}

推荐答案

如果您需要将计算结果显示在摘要之后, 您可以将结果保存到文件中,然后抽象地加载它. 您必须两次编译LaTeX文件.

If you need the computations to appear after the abstract, you can save the result to a file, and load it in the abstract. You have to compile the LaTeX file twice.

\documentclass{article}
\begin{document}

\begin{abstract}
 This paper... and we got a mean of \Sexpr{load("a.RData"); mean.data}.
\end{abstract}

<<Computations>>=
data <- c(1,2,3,4,5)
mean.data <- mean(data)
save(mean.data, file="a.RData")
@

\end{document}

这篇关于在创建变量的代码块之前使用R变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆