0
votes

I have a basic folder structure that looks something like:

> Top/    
    > FolderA/
    > FolderB/

and in each FolderA/ and FolderB/ ...etc. there is a file called myfile.rnw

Each rnw file consists of the following code:

<<setup, eval=TRUE, include=FALSE, echo=FALSE>>=
library(knitr)
library(dplyr)
library(ggplot2)
opts_chunk$set(fig.path=paste("figure", unique_letter, "/", unique_letter, sep="")) 
#pretend that unique_letter makes the figure path unique for each rnw file
@
\documentclass[10pt]{article}
\begin{document}
Hello. This is file A. See the figure below.
<<plot, fig.cap="My test caption", include=TRUE, echo=FALSE>>=
a = data.frame(A = seq(1:100)) %>% 
  mutate(B = A^3)
myplot = ggplot()+
  geom_area(data = a, aes(x = A, y = B))
myplot
@
\end{document}

and I am trying to compile each rnw file using knitr with the following code:

library(knitr)

mylist = list.files(path = "Path/To/Top",
                   pattern = ".rnw",
                   full.names = TRUE,
                   recursive = TRUE,
                   ignore.case = TRUE,
                   include.dirs = FALSE)

for (i in 1:length(mylist)) {
  knit(mylist[[i]], output = paste(dirname(mylist[[i]]), "/myfile.tex", sep = ""))
}

however when I run the code to compile the multiple rnw files I end up with a .tex file but no folder to hold all the figures. I used the following link as reference (the first two are very relevant):

Can Sweave produce many pdfs automatically?

Using loops with knitr to produce multiple pdf reports... need a little help to get me over the hump

and then this one seems relevant since in my real folder structure, the rnw file is sourcing multiple R analysis files (.R) and general rnw files for repeated text with some variables replaced.

https://github.com/yihui/knitr/issues/913

although I don't think it's entirely relevant since I am fine putting the pdf documents in the same directory as the rnw files respectively.

I can't seem to use knit2pdf as it is not working with my R version (which I am in the process of reverting back so I can use it).

I do have the next step to convert the .tex file to PDF but the .tex file won't compile anyways right now since the figure folder is not created for the rnw files. Any help is appreciated!

1

1 Answers

0
votes

I think the folder of figures is in your working directory. In your loop, you can try adding a setwd to point to the directory of the final Tex files. Even if this is not really clean to change directory in a middle of a script like this... However, you should directly use knit to create the PDF file. If this is a problem of R version, you should really update...