更有效的R/Sweave/TeXShop工作流程? [英] More efficient R / Sweave / TeXShop work-flow?

查看:113
本文介绍了更有效的R/Sweave/TeXShop工作流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经可以在Mac OS X 10.6机器上正常工作,以便可以使用Sweave创建外观不错的LaTeX文档,其中包括R代码,输出和LaTeX格式的片段.不幸的是,我觉得我的工作流程有点笨拙且效率低下:

  1. 使用TextWrangler,我在一个.Rnw文件中一起编写LaTeX代码和R代码(在R代码块的上方和下方的<< >> =包围).

  2. 保存更改后,我使用Sweave命令从R调用.Rnw文件

    Sweave(file="/Users/mymachine/Documents/Assign4.Rnw", 
            syntax="SweaveSyntaxNoweb")
    

    作为响应,R输出以下消息:

    您现在可以在"Assign4.tex"上运行LaTeX

    因此,然后我在R目录中找到.tex文件(Assign4.tex),并将其复制到我的.Rnw文件所在的文档~/Documents/的文件夹中(以将所有内容保存在一个位置)./p>

  3. 然后我在TeXShop中打开.tex文件(例如Assign4.tex)并将其编译为pdf格式.只是在这一点上,我才能看到对文档所做的任何更改,并查看它是否看起来不错".

有没有一种方法可以一键编译所有内容?特别是直接从TextWrangler或TeXShop调用Sweave/R会很好.我怀疑可以在Terminal中编写脚本来执行此操作,但是我对Terminal没有经验.

请告知我是否还有其他方法可以简化或改善我的工作流程.

解决方案

使用Gregor Gorjanc的Sweave.sh脚本,在TeXShop中轻松进行一键式编织. 从> http://cran.r-project.org/contrib/extra/scripts/Sweave获取.sh 并将其放在您的~/Library/TeXShop/bin/文件夹中.

然后将以下文件添加到您的~/Library/TeXShop/engines/文件夹:

作为Sweave.engine:

#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh  -ld "$1"

SweaveNoClean.engine:

#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh  -nc -ld "$1"

您必须在Sweave.sh和两个引擎文件上设置权限以允许执行.

要一键进行Sweave,请在添加这些文件后重新启动TeXShop,打开Sweave文档(具有Rnw扩展名),然后在文档窗口上方的下拉菜单中,将其从LaTeX更改为Sweave或SweaveNoClean.

注意:"Sweave"选项将自行清除,删除LaTeX和Sweave创建的所有多余文件.如果您的文件名为myfile.Rnw,它将包括名为myfile.R和myfile.tex的文件.明智的选择是:确保Rnw文件的基本名称唯一.那么所有意外内容都不会被覆盖然后删除.

SweaveNoClean选项本身不会清除.这样可以确保您不会删除任何意外内容;但是,如果您编织myfile.Rnw,它仍然可以覆盖名为myfile.tex的文件.如果您希望将它们与完整的排版文档分开,这也不会删除任何已创建的图形.

I've now got everything to work properly on my Mac OS X 10.6 machine so that I can create decent looking LaTeX documents with Sweave that include snippets of R code, output, and LaTeX formatting together. Unfortunately, I feel like my work-flow is a bit clunky and inefficient:

  1. Using TextWrangler, I write LaTeX code and R code (surrounded by <<>>= above and @ below R code chunk) together in one .Rnw file.

  2. After saving changes, I call the .Rnw file from R using the Sweave command

    Sweave(file="/Users/mymachine/Documents/Assign4.Rnw", 
            syntax="SweaveSyntaxNoweb")
    

    In response, R outputs the following message:

    You can now run LaTeX on 'Assign4.tex'

    So then I find the .tex file (Assign4.tex) in the R directory and copy it over to the folder in my documents ~/Documents/ where the .Rnw file is sitting (to keep everything in one place).

  3. Then I open the .tex file (e.g. Assign4.tex) in TeXShop and compile it there into pdf format. It is only at this point that I get to see any changes I have made to the document and see if it 'looks nice'.

Is there a way that I can compile everything with one button click? Specifically it would be nice to either call Sweave / R directly from TextWrangler or TeXShop. I suspect it might be possible to code a script in Terminal to do it, but I have no experience with Terminal.

Please let me know if there's any other things I can do to streamline or improve my work flow.

解决方案

One-click Sweaving is easy to do in TeXShop using the Sweave.sh script by Gregor Gorjanc. Get it from http://cran.r-project.org/contrib/extra/scripts/Sweave.sh and put it in your ~/Library/TeXShop/bin/ folder.

Then add the following files to your ~/Library/TeXShop/engines/ folder:

As Sweave.engine:

#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh  -ld "$1"

As SweaveNoClean.engine:

#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh  -nc -ld "$1"

You'll have to set the permissions on Sweave.sh and the two engine files to allow execution.

To Sweave with one click, restart TeXShop after adding these files, open the Sweave document (with Rnw extension) and in the dropdown menu above the document window, change it from LaTeX to Sweave or SweaveNoClean.

BEWARE: The "Sweave" option wll clean up after itself, deleting all the extra files LaTeX and Sweave creates. If your file is called myfile.Rnw, this will include files called myfile.R and myfile.tex. So a word to the wise: make sure the basename of your Rnw file is unique; then nothing unexpected will be written over and then deleted.

The SweaveNoClean option does not clean up after itself. This makes sure you don't delete anything unexpected; though it could still write over a file called myfile.tex if you Sweave a myfile.Rnw. This also doesn't delete any graphics that have been created, in case you want to have them separate from your full typeset document.

这篇关于更有效的R/Sweave/TeXShop工作流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆