0
votes

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,

1

1 Answers

2
votes

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.