作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经看到了一些关于此的问题,但无法弄清楚如何做我想做的事。
默认情况下,Sweave 通过连接 Rnw 文件的名称和图形对象标签的名称来创建图形。
从这个问题( Make Sweave + RweaveHTML put all graphics in a specified folder )如果我想要文件夹 foo 中的所有图形并命名为 bar-graphic 我可以使用
\SweaveOpts{prefix.string=foo/bar}
最佳答案
好的,这可能应该分配给最内层的 hell ,但我知道获取当前正在运行的脚本名称的唯一方法是使用(滥用?)附加到函数的源引用。考虑文件中的这个 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}
$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)
@
\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}
关于r - 如何让 Sweave 将图形放在单独的文件夹中并以 Rnw 文件命名它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7254228/
我是一名优秀的程序员,十分优秀!