I'm using knitr
to generate reports. However, I often first prepare "internal" documents which contain all plots, and then for a final report or paper want to include only some of the figures.
Say, I have a set of systematically produced figures, say I loop over my specimen:
<<sample, fig.keep = 'all'>>=
specimen <- c("436a", "783a", "10b")
for (s in specimen) # in reality it would e.g. be levels (specimen)
plot (results [s])
@
This will produce a number of files figures/sample1.pdf
, figures/sample2.pdf
, etc.
While the numbering with 1, 2, ... is good for the knitr-generated report, if I want to include one of the plots in a paper it is cumbersome and error prone to find out which .pdf belongs to which sample.
How can I tell knitr
to use file names like "figures/sample-436a.pdf"
?
I tried <<fig.path = sprintf ("figures/sample-%s", s)>>=
, but that doesn't work: s
is unknown, so I guess fig.path
is evaluated right at the beginning of the chunk processing, not when saving the chunk.
PS: One obvious way to reduce the risk of errors are titles, but that is IMHO ugly in a paper.
Good question. Not sure if this could be a satisfactory answer:
\documentclass{article}
\begin{document}
<<prepare-src, include=FALSE>>=
s = names(iris)[-5]
# construct the source document
src = sprintf('<<sample-%s>>=
hist(iris[,"%s"], col="gray", border="white")
@', s, s)
@
% evaluate the source
\Sexpr{knit(text = src)}
\end{document}
In your case, you only need to decide which specimen to go into s
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With