Can I place all sweave figures at the end of the document? - r

I have a LaTeX manuscript that inputs from a single Sweave file. The file creates plots and places them in the document using <fig=TRUE>.
Is there a way that I can tell all of the figures to go to the end of the document, where the rest of the figures are? All of the other figures are placed at the end with the LaTeX option [ht!]
The only solution I have come up with is to use <fig=FALSE>, save the file as a graphics file, and then call the figure from within LaTeX at the end of the document. Is there a way to do this within the R-code part of the Sweave document itself (presumably this would be easier to do).

Another alternative is to use the LaTeX 'endfloat' package. Add the following to your preamble, and it will put all of the figures at the end.
\usepackage[noheads,nomarkers]{endfloat}

You could also do <chunk_name, fig = TRUE, include = F> in the chunk where you create the plot and call it at the end using
<new_chunk, fig = TRUE>
<<chunk_name>>
#

The image created with fig=TRUE will always have the name:
FILENAME-CHUNKNAME.pdf
Where FILENAME is the name of your .Rnw file without extension, So with this you can easilly make the includegraphics call yourself:
% Somewhere in source:
<<CHUNKNAME,fig=TRUE,include=FALSE>>=
plot(1)
#
% Later in source:
\begin{figure}
\centering
\includegraphics{FILENAME-CHUNKNAME}
\caption{Whatever}
\label{fig:whatever}
\end{figure}

Related

Display a graphic in report with sweave : Just display the final report and no intermediate file

I know R well but I start in Sweave.
I produce a report under Rstudio from several scripts:
graphic.R which defined a graphic created with ggplot2 called graph_1. I want to display it in the report.
script.Rnw containing Latex and Sweave code. I call graph_1 to display my graphic.
main.R script that executes the .Rnw code and therefore produces the report.
In graphic.R
library(ggplot2)
graph_1 <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(color=Species, shape=Species))
In script.Rnw
\documentclass[french,12pt]{article}
\begin{document}
... no important...
\begin{figure}[h]
\begin{center}
<< fig=TRUE, echo=FALSE, height = 2.5>>=
graph_1
#
\caption{caption figure 1}
\label{graph_1}
\end{center}
\end{figure}
\end{document}
In main.R
Sweave("script.Rnw", encoding="UTF-8")
tools:::texi2dvi(file="script.tex"), pdf=TRUE)
Everything is OK : my report (.pdf) is generated in the folder. But I also have a file : 'script-001.pdf' containing my graphic which is added in the same folder.
I would like only the final report and not the intermediate file : so not the file 'script-001.pdf'.
Somebody know if it is possible ?
Thank you,
The figure file is required so that LaTeX can include it. But you can delete it safely after running Sweave and LaTeX.
I don't like my working directory to be littered with intermediate PDFs, too. So I have them end up in a folder called figures. With Sweave you can accomplish this by setting a suitable prefix:
\SweaveOpts{prefix.string=figures/fig}
You also need to create the figure folder yourself for it to work.
And a personal recommendation: You may want to have a look at knitr – it's a more powerful modern version of Sweave – in my opinion.

pick either png or pdf in knitr when both are available

I like to generate both pdf and png image files when I create a (latex) document using knitr. This can be done using dev = c("pdf", "png").
However, I don't seem to be able to choose (on a per-figure) basis which of the two is picked in my latex figure environment. Currently the only way in which I can get, say, a png input file for Fig. 1 and a pdf input file for Fig. 2 is to generate only the required format (by using dev = "png", fig.ext = "png").
Is there a way in which I can still generate both, but at the latex level can select which one is shown? It could be solved easily by allowing an extension in the \includegraphics command, I suppose.
Any input appreciated...
Ron
Minimal example:
\documentclass[a4paper,12pt]{article}
%\VignetteEngine{knitr::knitr}
\DeclareGraphicsExtensions{.pdf,.png}
\begin{document}
%\maketitle
<<knitrInitialization,echo=FALSE>>=
require("knitr", quietly=TRUE)
opts_chunk$set(comment=NA,background='transparent',size='small',fig.width=6,fig.height=6,out.width='\\textwidth',dev=c('pdf','png'))
#
%% this one generates two figures, and the pdf version is shown
%% because of the order in DeclareGraphicsExtensions
\begin{figure}[tb]
\centering
<<testPDF,echo=FALSE>>=
plot(1:10)
#
\caption{PDF figure}
\label{fig:pdf}
\end{figure}
%% if I want to show a png (e.g., because the pdf is too large) I can
%% only do that by not generating a pdf in the first place
\begin{figure}[tb]
\centering
<<testPNG,echo=FALSE,dev='png',fig.ext='png'>>=
plot(1:10)
#
\caption{PNG figure}
\label{fig:png}
\end{figure}
\end{document}
You can simply put
\DeclareGraphicsExtensions{.png,.pdf}
in your document when you want PNG to have precedence, and then
\DeclareGraphicsExtensions{.pdf,.png}
later if you want to go back to the PDF preference. This works in the body of a LaTeX document, not just in the header.

Using knitr to create HTML slide with separate output of just the R code

I am using knitr to create a set of lecture slides for a class using R. I would like to create a separate "companion file" that contains just has the R code (corresponding to the slides), so that students can execute the R code by cutting and pasting from the companion file.
For example, in the .Rmd file:
``` {r ....}
plot(x,y)
```
Then there would be a text file with:
plot(x,y)
But, have such a file be automatically produced from the .Rmd file?
Even better if the .Rmd file has such tags:
``` {r basic.plot ....}
plot(x,y)
```
Then, text file has:
# basic.plot
plot(x,y)
Can this be accomplished using knitr?
Yes, this is possible. What you're trying to do is called tangling, and it comes from the world of literate programming.
The knit function supports a tangle option that should be set to TRUE if you want to extract source code.

Dynamic LaTeX references in R comment with knitr

I had ask a similar question to this with respect to Sweave (
Dynamic references to figures in a R comment within Sweave document
) and would like to see if anyone as a similar answer when using knitr.
The goal is to have the following code chunk
<<"example", fig.cap = "some figure", highlight = FALSE>>=
# the following code generated Figure \ref{fig:example}
plot(1:10, 1:10)
#
have be displayed in the resulting .pdf as
# the following code generated Figure 1.1
plot(1:10, 1:10)
So far I have found that by setting highlight = FALSE the R code is placed into a verbatim environment in the resulting .tex file. If the environment could be alltt instead of verbatim then we'd have the desired output. Is it possible to have the non-highlighted code chunks be placed in alltt environments via a knitr option?
I have added an example 072-latex-reference.Rnw in the knitr-examples repository. The basic idea is to restore the escaped \ref{} (which should have been \textbackslash{}ref\{\} in the default output).

Adding a table of contents to PDF with R plots

I'm creating a larger number of plots in R and save them as PDF (using grDevices / pdf). Is there an easy way to add a (meta-data) table of contents to the PDF as it is created?
I don't mean to add a separate page, but a TOC that PDF viewers like Preview.app display in the sidebar to make navigation easier.)
Example of such a TOC:
The only way I know is with LaTeX, but you don't necessarily need Sweave; perhaps you could just generate the LaTeX file directly with your RPython code. If you've got two pictures which are 6x6 (the default size) named tmp-001.pdf and tmp-002.pdf, this is how you'd make a section called Section A with two subsections for the two pictures.
\documentclass{article}
\usepackage[paperwidth=6in, paperheight=6in, margin=0in]{geometry}
\usepackage{hyperref}
\usepackage{graphicx}
\parindent 0pt
\begin{document}
\pdfbookmark[1]{Section A}{anchorname_aa}
\pdfbookmark[2]{plot aa}{anchorname_aa}
\includegraphics{tmp-001.pdf}
\newpage
\pdfbookmark[2]{plot bb}{anchorname_bb}
\includegraphics{tmp-002.pdf}
\end{document}
You could create a first page using the names of your objects or plots as the arguments to text in columns.
plot(1:10,1:10, type="n", main ="Index", axes=FALSE,frame.plot=FALSE,xlab="")
text(1,10, "plot")
text(3,10, "page")
text(5,1:10, rev(1:10+1))
text(2,1:10, rev(letters[1:10]))
I cannot think of a way to generate a navigable TOC, but this option may be more pleasing and easier to integrate with Beamer-type displays. The Hmisc package function latex provides an interface to the Latex longtable package. My Latex-fu is weak but if yours is stronger, you could also divert the dvi code that is created for integration within other applications. I get an intermediate dvi file put in a temporary directory which then opens my dvi viewer and allows saving as pdf:
require(Hmisc)
?latex # you will probably want to review this
latex(Plot_list<-data.frame(Plots=letters[1:10], Pages=2:11))
Not as far as I know. I think you have to use sweave for that.

Resources