Is it possible to include Sexpr before the expression has been evaluated in Sweave / R?

I am writing a Sweave document, and I want to include a small section that details the versions of R and the package, platofrms and how long it took to evaluate the level of detail, however I want to put this in the middle of the document!

I used \ Sexpr {elapsed} to do this (it didn’t work), but thought that if I put the code print passed in the piece that is evaluated at the end, I could then add the piece halfway, which also fails.

My document looks something like this.

% \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} 

But this does not work (amazing!)

Does anyone know how I can do this?

thanks

Paul.

+4
source share
3 answers

This works fine for me:

 \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} 

I had to delete the version line inside \Sexp{} as I get the underscore with x86_64 , which then upsets LaTeX. Otherwise, it’s just fine, and now you will receive the elapsed time a little less than the sleeping amount.

You can either use R to cache the elapsed time in a temporary file for the next run, or pass it to LaTeX as some variable, but you cannot use “direct links” as fragments of R evaluated in turn.

+3
source

btw you usually don’t need print to evaluate R variables

 \Sexpr{version$version.string} 

works great

+2
source

The answer to Dirk is almost perfect, but still doesn't allow you to put the answer halfway through the document. I was very upset thinking that it should work, but I realized that the code I had was to open the time file at the beginning of each run (and empty it) and write an empty result in my document, and then put the answer in a file time to end!

In the end, I did something similar, but used R to just open and write the file at the end, which would work just fine!

 \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} 
+2
source

Source: https://habr.com/ru/post/1307562/


All Articles