Could the \include{} command be used in a knitr .Rnw file? - r

Using \include{} usually works, unless there are code chunks in the file to be included:
\documentclass{article}
\begin{document}
% \include{test.tex}
\include{test.Rnw}
\end{document}
with test.tex or test.Rnw define e.g. by
<<echo=TRUE,eval=TRUE>>=
plot(cars)
#
won't display the cars plot. Is there some chance to find a way to bypass that trouble?
Thanks,
Julyan

You can include .Rnw file with child.
\documentclass{article}
\begin{document}
<<test, child='test.Rnw'>>=
#
\end{document}
were test.Rnw contains:
<<echo=TRUE,eval=TRUE>>=
plot(cars)
#

Related

call a variable in environnment

I would like to know how to display the result of a variable that is in the environment
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
Hello,
\input{test2.Rnw}
\end{document}
test2.Rnw
\section{test}
<<eval=TRUE, echo=F>>=
paste0("my variable v is worth: ", v)
#
When I compile I get this
You can use the knitr package inside your Rmarkdown code. Check https://riptutorial.com/r/topic/4334/r-in-latex-with-knitr and also this SO Q&A : knitr chunk option eval=TRUE, echo=TRUE, include=FALSE

Sweave control of chuk output

I'm using Sweave to make a report in R-Studio, and I have the problem, that my teacher will only accept reports, where the code is placed in an Appendix. This means that I need to control the position of the chunk outputs (graphs).
Is this possible in Sweave?
Regards,
Jens
This will return everything in your code. Using the \usepackage{fancyvrb} and the \VerbatimInput{test.Rnw} assuming that your file is named test.Rnw
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<my_code_chunk>>=
2+2
#
\section{Appendix}
\VerbatimInput{test.Rnw}
\end{document}
Here is another example
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<my_code_chunk>>=
2+2
#
\section{Appendix}
<<>>=
<<my_code_chunk>>
#
\end{document}

Read chunk from Rmd file to Rnw file

I want to read chunks in an RMD file from an RNW file, both of which are in different folders. I cannot make it work. It seems like it is not possible to read chunks from an RMD file. read_chunk() function seems to read only from an .R file. But in my case I cannot make it work as well.
Here are my three files (in different folders) and the output of my RNW file at the end. Any ideas why this is not working?
"..\Folder_R\trial_r.R"
## #knitr r_chunk_1
14 + 17
cat("SUCCESS THIS IS R CHUNK 1!!!")
## #knitr r_chunk_2
plot(cars)
"..\Folder_html\trial_html.RMD"
---
title: "Untitled"
output: html_document
---
```{r html_chunk_1}
## #knitr html_chunk_1
cat("SUCCESS THIS IS HTML CHUNK 1!!!")
```
```{r html_chunk_2, echo=FALSE}
## #knitr html_chunk_2
plot(cars)
```
"..\Folder_latex\trial_latex.RNW"
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<echo=FALSE>>=
library(knitr)
if (file.exists(file.path("..","Folder_html", "trial_html.Rmd")))
cat("`trial_html` File Exists!!\n\n")
read_chunk(file.path("..","Folder_html", "trial_html.Rmd"))
#
<<latex_chunk_1, echo=FALSE>>=
cat("This is LATEX Chunk!!")
#
<<latex_chunk_2, ref.label='html_chunk_1', echo=FALSE>>=
#
<<latex_chunk_3, echo=FALSE>>=
<<html_chunk_1>>
#
<<html_chunk_1, echo=FALSE>>=
#
<<latex_chunk_4, echo=FALSE>>=
if (file.exists(file.path("..","Folder_R", "trial_r.R")))
cat("`trial_r` File Exists!!\n\n")
read_chunk(file.path("..","Folder_R", "trial_r.R"))
#
<<latex_chunk_5, echo=FALSE>>=
<<r_chunk_1>>
#
<<r_chunk_2>>=
#
\end{document}
As a result the only thing I see in the PDF file is :
`trial_html` File Exists!!
This is LATEX Chunk!!
`trial_r` File Exists!!
I checked following sources THIS, THIS is somewhat what I want but did not help to solve my problem, and THIS is very helpful but I cannot make it work.
My warning message from the compilation is:
You can now run (pdf)latex on 'trial_latex.tex'
Warning messages:
1: In utils::Sweave("trial_latex.Rnw", encoding = "ISO8859-1") :
reference to unknown chunk 'html_chunk_1'
2: In utils::Sweave("trial_latex.Rnw", encoding = "ISO8859-1") :
reference to unknown chunk 'r_chunk_1'
Running pdflatex.exe on trial_latex.tex...completed
I think you were using Sweave instead of knitr. If you were using RStudio, be sure to change the option:

Displaying <<..>>= in output

I want demonstrate a sample piece of R code WITH the knitr <<..>>= preamble in a LaTeX document. Here is an example of the output I desire:
It's got to be simple - but I'm missing something. I checked the documentation and scanned stack overflow - but without luck. Here is a MWE:
\documentclass{article}
\begin{document}
<<mychunk, cache=TRUE, eval=FALSE, dpi=100>>=
"hello world"
#
\end{document}
Suggestions? I tried indenting the code in LaTex and wrapping in a verbatim block, but only got errors.
I just checked the manual of knitr. This is how the package author solved the problem:
<<use-ext-chunk, echo=FALSE, comment=NA>>=
cat('<<Q1, echo=TRUE, tidy=TRUE>>=','#',sep='\n')
#
which produces the output as shown on page 9 of the knitr manual
Here is a minimal example:
\documentclass[a4paper]{article}
\begin{document}
<<use-ext-chunk, echo=FALSE, comment=NA>>=
cat('<<Q1, echo=TRUE, tidy=TRUE>>=','#',sep='\n')
#
\end{document}
which produces the attached output.
I had the same question on tex.stackexchange.com a year ago and got a few nice responses: https://tex.stackexchange.com/q/35485/3419. This is for Sweave but I think it will work the same in knitr.
I think I ended up just using \Sexpr{"<<>>="} and \Sexpr{"#"} in verbatim environment. e.g.:
\documentclass{article}
\begin{document}
\begin{verbatim}
\Sexpr{"<<mychunk, cache=TRUE, eval=FALSE, dpi=100>>="}
"hello world"
\Sexpr{"#"}
\end{verbatim}
\end{document}
Just a quick follow-up: this feature has been implemented in knitr (devel version >= 0.8.15); see examples for both Rnw and Rmd. An alternative solution is in knitr FAQ.

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

I'm writing a Sweave document, and I want to include a small section that details the R and package versions, platofrms and how long ti took to evalute the doucment, however, I want to put this in the middle of the document !
I was using a \Sexpr{elapsed} to do this (which didn't work), but thought if I put the code printing elapsed in a chunk that evaluates at the end, I could then include the chunk half way through, 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 doesn't seem to work (amazingly !)
Does anyone know how I could do this ?
Thanks
Paul.
This works just 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 remove the version string inside the \Sexp{} as I get an underscore with via x86_64 which then upsets LaTeX. Otherwise just fine, and you now get the elapsed time of just over the slept amount.
You could use either R to cache the elapsed time in a temporary file for the next run, or pass it to LaTeX as some sort of variable -- but you will not be able to use 'forward references' as the R chunks gets evaluated in turn.
btw you don't usually need print to evaluate variables R
\Sexpr{version$version.string}
works fine as well
Dirk's answer is almost perfect, but still doesn't let you put the answer half way through the document. I got quite frustrated thinking it should work, but realised that the code I had was opening the time file at the start of each run (and emptying it) and writing the empty result into my document, then putting the answer in the time file at the end !
I eventually did something similar but using R to only open and write the file at the end, which worked great !;
\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}

Resources