How to get sweave to place graphics in a separate folder And name them after the Rnw file

I saw a few questions about this, but I can’t figure out how to do what I want.

By default, Sweave creates graphics by combining the Rnw file name and the label name of the graphic.

From this question ( Make Sweave + RweaveHTML put all the graphics in the specified folder ). If I want all my graphs in the foo folder and are named bar-graphic I can use

\SweaveOpts{prefix.string=foo/bar}

But how can I get the graphics in the foo folder, but with the name rnwfile-graphic?

+5
source share
2 answers

, , , , script - (?) , . Sweave foo.Rnw

<<>>=
foo <- function(x) {x}
srcref <- attr(body(foo), "srcref")[[1]]
attr(srcref, "srcfile")$filename
@

foo.Rnw Sweave, :

\begin{Schunk}
\begin{Sinput}
> foo <- function(x) {
+     x
+ }
> srcref <- attr(body(foo), "srcref")[[1]]
> attr(srcref, "srcfile")$filename
\end{Sinput}
\begin{Soutput}
[1] "foo.Rnw"
attr(,"encoding")
[1] "ASCII"
\end{Soutput}
\end{Schunk}

Sweave, $filename , , :

> sub("\\.Rnw", "", "foo.Rnw")
[1] "foo"

, prefix.string,

<<>>=
fname <- sub("\\.Rnw", "", attr(srcref, "srcfile")$filename)
prefix.string <- paste("foo/", fname, "-graphic", sep = "")
@

\SweaveOpts{prefix.string=\Sexpr{prefix.string}}

prefix.string .

:

<<>>=
foo <- function(x) {x}
srcref <- attr(body(foo), "srcref")[[1]]
attr(srcref, "srcfile")$filename
@

<<>>=
fname <- sub("\\.Rnw", "", attr(srcref, "srcfile")$filename)
prefix.string <- paste("foo/", fname, "-graphic", sep = "")
@

\SweaveOpts{prefix.string=\Sexpr{prefix.string}}

<<fig=TRUE>>=
plot(1:10)
@

Sweave :

\begin{Schunk}
\begin{Sinput}
> foo <- function(x) {
+     x
+ }
> srcref <- attr(body(foo), "srcref")[[1]]
> attr(srcref, "srcfile")$filename
\end{Sinput}
\begin{Soutput}
[1] "foo.Rnw"
attr(,"encoding")
[1] "ASCII"
\end{Soutput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
> fname <- sub("\\.Rnw", "", attr(srcref, "srcfile")$filename)
> prefix.string <- paste("foo/", fname, "-graphic", sep = "")
\end{Sinput}
\end{Schunk}



\begin{Schunk}
\begin{Sinput}
> plot(1:10)
\end{Sinput}
\end{Schunk}
\includegraphics{foo/foo-graphic-003}

, .

+5

Gavin hack , , Sweave , - ; , , , Sweave -. , .

, , , Sweave; 2.13 Sweave .

. subdir chunkprefix, rnw. SweaveParseOptions , utils .

16a17,18
> SweaveParseOptions <- utils:::SweaveParseOptions
> 
69c71
<                     concordance = FALSE, expand = TRUE, figs.only = FALSE)
---
>                     concordance = FALSE, expand = TRUE, figs.only = FALSE, subdir=".")
520c522
<                    "grdevice")
---
>                    "grdevice", "subdir")
568c570
<     chunkprefix
---
>     file.path(options$subdir, chunkprefix)

SweaveDriversNew.R, Rnw ,

\SweaveOpts{subdir=foo}

Sweave, , .

source("SweaveDriversNew.R")
Sweave("test.Rnw", driver=RweaveLatex)

foo/test-graphic.pdf, foo Sweave test Sweave.

+3

All Articles