如何让 Sweave 将图形放在单独的文件夹中并以 Rnw 文件命名

发布于 2024-12-02 09:14:51 字数 416 浏览 0 评论 0原文

我已经看到了一些与此相关的问题,但无法弄清楚如何做我想做的事情。

默认情况下,Sweave 通过连接 Rnw 文件的名称和图形对象标签的名称来创建图形。

从这个问题(Make Sweave + RweaveHTML 将所有图形放入指定的文件夹)如果我想要文件夹 foo 中的所有图形并命名为 bar-graphic 我可以使用

\SweaveOpts{prefix.string=foo/bar}

但是如何获取文件夹 foo 中但命名为 rnwfile-graphic 的图形?

I've seen a few questions about this, but can't work out how to do what I want.

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

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

\SweaveOpts{prefix.string=foo/bar}

But how can I get the graphics in folder foo but named rnwfile-graphic ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

触ぅ动初心 2024-12-09 09:14:51

好吧,这可能应该分配给最内层的地狱,但我知道获取当前运行脚本的名称的唯一方法是使用(滥用?)附加到函数的源引用。考虑文件中的这个 Sweave 块 foo.Rnw

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

当我使用 Sweave 处理 foo.Rnw 时,我得到:

\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 文件的顶部创建一个虚拟函数,pull从源引用中取出 $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}

根据您认为合适的方式进行调整。

OK, this probably should be assigned to the innermost circle of hell, but the only way I know of to get the name of the currently running script is to use (abuse?) the source reference attached to functions. Consider this Sweave chunk in a file foo.Rnw

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

When I process foo.Rnw with Sweave I get:

\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}

You could make a dummy function at the top of your Sweave file, pull out the $filename from the source reference, and process it to remove the extension, e.g.:

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

and then build up the string needed for prefix.string, with say

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

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

where prefix.string contains the built up path and prefix.

Here is a complete example:

<<>>=
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)
@

that when processed by Sweave gives:

\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}

Tweak that as you see fit.

病女 2024-12-09 09:14:51

虽然 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 文件的名称。

While Gavin's hack is great, if you really want Sweave to do different sorts of things, the best way to do so is to write your own driver; I use this method, for instance, for getting Sweave to scale different figures differently. You don't have to start from scratch but can just modify the existing code as needed.

There's only a couple minor functions that need to be changed for this case, however, with the namespace issues it's easier to just start with all the Sweave code; you can get the 2.13 Sweave code here.

The necessary changes are below. They add a new option, subdir, and prepend it to the original chunkprefix, which is the name of the rnw file. The SweaveParseOptions line is needed because this function is not exported by utils and is in a different source file.

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)

With the changes saved as SweaveDriversNew.R, put the option in your Rnw file like this

\SweaveOpts{subdir=foo}

And then Sweave using the new driver like this.

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

The result is that images are named foo/test-graphic.pdf, where foo is defined in the Sweave file and test is automagically taken from the name of Sweave file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文