我已经看到了一些关于这个的问题,但是无法解决如何做我想做的事情.
默认情况下,Sweave通过连接Rnw文件的名称和图形对象标签的名称来创建图形.
从这个问题(Make Sweave + RweaveHTML将所有图形放在指定的文件夹中)如果我想将所有图形放在文件夹foo中并命名为bar-graphic我可以使用
\SweaveOpts{prefix.string=foo/bar}
Run Code Online (Sandbox Code Playgroud)
但是如何在文件夹foo中获取图形但命名为rnwfile-graphic?
好吧,这可能应该被分配到地狱的最内圈,但我知道获取当前运行脚本名称的唯一方法是使用(滥用?)附加到函数的源引用.在文件中考虑这个Sweave块foo.Rnw
<<>>=
foo <- function(x) {x}
srcref <- attr(body(foo), "srcref")[[1]]
attr(srcref, "srcfile")$filename
@
Run Code Online (Sandbox Code Playgroud)
当我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}
Run Code Online (Sandbox Code Playgroud)
您可以在Sweave文件的顶部创建一个虚函数,$filename从源引用中拉出,并处理它以删除扩展名,例如:
> sub("\\.Rnw", "", "foo.Rnw")
[1] "foo"
Run Code Online (Sandbox Code Playgroud)
然后建立所需的字符串prefix.string,比方说
<<>>=
fname <- sub("\\.Rnw", "", attr(srcref, "srcfile")$filename)
prefix.string <- paste("foo/", fname, "-graphic", sep = "")
@
\SweaveOpts{prefix.string=\Sexpr{prefix.string}}
Run Code Online (Sandbox Code Playgroud)
其中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)
@
Run Code Online (Sandbox Code Playgroud)
当由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}
Run Code Online (Sandbox Code Playgroud)
根据你的需要调整一下.
| 归档时间: |
|
| 查看次数: |
1031 次 |
| 最近记录: |