4

我正在写一个 Sweave 文档,我想包含一个小部分,详细说明 R 和包版本、平台以及评估文档所需的时间,但是,我想把它放在文档的中间!

我正在使用 \Sexpr{elapsed} 来执行此操作(这不起作用),但我想如果我将打印经过的代码放在最后评估的块中,然后我可以在中途包含该块,这也失败。

我的文档看起来像这样

% 
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{longtable}
\usepackage{geometry}
\usepackage{Sweave}
\geometry{left=1.25in, right=1.25in, top=1in, bottom=1in}
\begin{document}

<<label=start, echo=FALSE, include=FALSE>>=
startt<-proc.time()[3]
@ 
Text and Sweave Code in here
% 
This document was created on \today, with \Sexpr{print(version$version.string)} running
 on a \Sexpr{print(version$platform)} platform. It took approx sec to process.
<<>>=
    <<elapsed>>
@ 
More text and Sweave code in here
<<label=bye, include=FALSE, echo=FALSE>>= 
odbcCloseAll()
endt<-proc.time()[3]
elapsedtime<-as.numeric(endt-startt)
@ 
<<label=elapsed, include=FALSE, echo=FALSE>>=
print(elapsedtime)
@ 
\end{document}

但这似乎不起作用(令人惊讶!)

有谁知道我该怎么做?

谢谢

保罗。

4

3 回答 3

3

这对我来说很好:

\documentclass{article}
\usepackage{Sweave}
\begin{document}

<<label=start, echo=FALSE, include=FALSE>>=
startt<-proc.time()[3]
@

Text and Sweave Code in here

This document was created on \today, with
\Sexpr{print(version$version.string)}.

<<results=hide,echo=FALSE>>=
Sys.sleep(2)  # instead of real work
@

More text and Sweave code in here

<<label=bye, include=FALSE, echo=FALSE>>=
endt<-proc.time()[3]
elapsedtime<-as.numeric(endt-startt)
@

It took approx \Sexpr{elapsedtime} seconds to process.

\end{document}

我不得不删除里面的版本字符串,\Sexp{}因为我得到一个下划线,然后通过x86_64它扰乱 LaTeX。否则就好了,你现在得到刚刚超过睡眠量的经过时间。

您可以使用 R 将经过的时间缓存在临时文件中以供下次运行,也可以将其作为某种变量传递给 LaTeX - 但您将无法使用“前向引用”,因为 R 块在转动。

于 2010-04-21T16:23:13.110 回答
2

顺便说一句,您通常不需要 print 来评估变量 R

\Sexpr{version$version.string}

也可以正常工作

于 2010-04-21T16:33:02.680 回答
2

Dirk 的答案几乎是完美的,但仍然不允许您将答案放在文档的一半。我很沮丧认为它应该可以工作,但意识到我的代码是在每次运行开始时打开时间文件(并将其清空)并​​将空结果写入我的文档,然后将答案放入时间文件中结束 !

我最终做了类似的事情,但使用 R 只在最后打开和写入文件,效果很好!

\documentclass[a4paper]{article} 
\usepackage[OT1]{fontenc} 
\usepackage{longtable} 
\usepackage{geometry} 
\usepackage{Sweave} 
\geometry{left=1.25in, right=1.25in, top=1in, bottom=1in} 
\begin{document} 

<<label=start, echo=FALSE, include=FALSE>>= 
startt<-proc.time()[3] 
@  
Text and Sweave Code in here 
%  
This document was created on \today, with \Sexpr{print(version$version.string)} running 
 on a \Sexpr{print(version$platform)} platform. It took approx \input{time}
 sec to process. 

More text and Sweave code in here 
<<label=bye, include=FALSE, echo=FALSE>>=  
odbcCloseAll() 
endt<-proc.time()[3] 
elapsedtime<-as.numeric(endt-startt) 
@  
<<label=elapsed, include=FALSE, echo=FALSE>>=
fileConn<-file("time.tex", "wt") 
writeLines(as.character(elapsedtime), fileConn) 
close(fileConn) 
@ 
\end{document}
于 2010-04-23T08:13:35.023 回答