如何让 Sweave 将图形放在单独的文件夹中并以 Rnw 文件命名
我已经看到了一些与此相关的问题,但无法弄清楚如何做我想做的事情。
默认情况下,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这可能应该分配给最内层的地狱,但我知道获取当前运行脚本的名称的唯一方法是使用(滥用?)附加到函数的源引用。考虑文件中的这个 Sweave 块
foo.Rnw
当我使用 Sweave 处理
foo.Rnw
时,我得到:您可以在 Sweave 文件的顶部创建一个虚拟函数,pull从源引用中取出
$filename
,并对其进行处理以删除扩展名,例如:然后构建
prefix.string
所需的字符串,并说出其中
prefix.string
包含构建的路径和前缀。这是一个完整的示例:
当由 Sweave 处理时给出:
根据您认为合适的方式进行调整。
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
When I process
foo.Rnw
with Sweave I get: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.:and then build up the string needed for
prefix.string
, with saywhere
prefix.string
contains the built up path and prefix.Here is a complete example:
that when processed by Sweave gives:
Tweak that as you see fit.
虽然 Gavin 的 hack 很棒,但如果您确实希望 Sweave 做不同类型的事情,最好的方法是编写自己的驱动程序;例如,我使用这种方法让 Sweave 以不同的方式缩放不同的图形。您不必从头开始,只需根据需要修改现有代码即可。
对于这种情况,只有几个小功能需要更改,但是,由于命名空间问题,从所有 Sweave 代码开始会更容易;你可以获得2.13 Sweave代码在这里。
必要的更改如下。他们添加了一个新选项
subdir
,并将其添加到原始chunkprefix
之前,这是 rnw 文件的名称。需要SweaveParseOptions
行,因为该函数不是由utils
导出的,而是位于不同的源文件中。将更改保存为
SweaveDriversNew.R
后,将选项放入Rnw
文件中,如下所示,然后使用新驱动程序进行 Sweave,如下所示。
结果是图像被命名为
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 originalchunkprefix
, which is the name of the rnw file. TheSweaveParseOptions
line is needed because this function is not exported byutils
and is in a different source file.With the changes saved as
SweaveDriversNew.R
, put the option in yourRnw
file like thisAnd then Sweave using the new driver like this.
The result is that images are named
foo/test-graphic.pdf
, wherefoo
is defined in the Sweave file andtest
is automagically taken from the name of Sweave file.