This must be a simple question but I don't figure out how I can do it.

I would like to produce a pdf report using Sweave. And actually, many many reports.

I shortened and simplified the sweave code to get to the point. Here's the code (short.rnw) :





As you can see, this code simply includes a graphic named "boxp.png" in the report. I actually don't need sweave to do this but what I really want to do is to generate multiple reports with a varying filename.

So, is there a way to pass a string parameter (or any kind of parameter) to build the report without having to rewrite the sweave template each time ?

I generate the reports from a bash script with the command 'R CMD Sweave short.rnw', the string parameters being defined before this call.



for f in ${files}

prog1 f 
# prog1 is a program which generates figures in a directory dir
# this directory has a name depending on f name
# for instance if f="foo.txt", then dir="./foo_dir"

#And now HERE I have to create my report with the images
#in foo_dir
# And I don't know how I can specify the varying names of dir
# in my template...





there are two ways to do what you seek. one is using sweave, the other is using an R package called brew which overcomes sweave's limitations of looping over a global variable. i am providing both code chunks.

The Sweave Code Chunk

<<echo = F, results = tex>>=

figdir   = paste(getwd(), 'fig', sep = '/')
fignames = read.csv('fignames.csv');
for (i in seq_along(fignames)) {

 filename = file.path(figdir, i)

 cat("\\section{", i, "}", sep = "");
 cat("\\caption*{MyCaption", i, "}", sep = "");
 cat("\\includegraphics[width = 0.5\\textwidth]{", filename, "}", sep = "");


The Brew Code Chunk

<% figdir   = paste(getwd(), 'fig', sep = '/') %> 
<% fignames = read.csv('fignames.csv');
<% for (i in seq_along(fignames)) { -%>


<% filename = file.path(figdir, i) %>  
<%= cat("\section{", i, "}", sep = "") %>


<%= cat("\\includegraphics[width = 0.5\\textwidth]{filename}")

<% } -%>

you can check out the link to brew on how to use it from R

EDIT: this would produce a single file with all the output.

  • Thank you Ramnath. This is useful but not exactly what I want to do. The fact is that I don't have a static file like "fignames.csv" from where I can extract all the paths. I am in a loop in a bash script and the fignames are discovered one by one dynamically. No clue in advance of which should be drawn. So I think I am going to use a makefile to 'sweave' the document. Paths will be arguments of the makefile.
    – tony
    Dec 8 '10 at 18:59
  • @Tony. Can you shed more light on how the figure names are discovered. Are they all in the same directory? If you can specify the criteria for deciding which figures to include, one can easily write a template .Rnw file and use R to loop through the figures and produce one pdf file for each figure.
    – Ramnath
    Dec 8 '10 at 19:01
  • I've edited my post. Hope you understand my problem better now.
    – tony
    Dec 8 '10 at 19:37
  • So, actually, I don't need a loop in R. My loop is done in the bash script. I just need Sweave to dynamically create the right paths to the right images. I need a Sweave trick...Thank you for your help.
    – tony
    Dec 8 '10 at 19:51

I had a similar issue where I wanted to 'push' a variable from my R session into a sweave document (see https://stackoverflow.com/questions/2912270/sweave-cant-see-a-vector-if-run-from-a-function).

Rscript sweavescript.R f

It seems that when Sweave runs, it can see objects in the global environment., so you could call R from your bash script passing the value of 'f' to the script (see https://stackoverflow.com/questions/3433603/parsing-command-line-arguments-in-r-scripts). The script would then put that value of 'f' into the global environment

f<<-commandArgs(trailingOnly = T)

and then runs sweave, where your sweave template then uses the value of f to generate images.

Hope this gets you started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.