更高效的 R / Sweave / TeXShop 工作流程?

发布于 2024-10-16 09:03:08 字数 868 浏览 3 评论 0原文

现在,我的 Mac OS X 10.6 机器上一切正常,这样我就可以使用 Sweave 创建外观漂亮的 LaTeX 文档,其中包括 R 代码片段、输出和 LaTeX 格式。不幸的是,我觉得我的工作流程有点笨拙和低效:

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

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

    Sweave(file="/Users/mymachine/Documents/Assign4.Rnw", 
            语法=“SweaveSyntaxNoweb”)
    

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

    <块引用>

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

    然后我在 R 目录中找到 .tex 文件 (Assign4.tex),并将其复制到我的文档 ~/Documents/ 中 .Rnw 文件所在的文件夹(以将所有内容保留在一处)。

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

有没有一种方法可以让我一键点击编译所有内容?具体来说,直接从 TextWrangler 或 TeXShop 调用 Sweave / R 会很好。我怀疑可能可以在终端中编写脚本来完成此操作,但我没有使用终端的经验。

如果我可以做任何其他事情来简化或改进我的工作流程,请告诉我。

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.

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

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

发布评论

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

评论(11

单身狗的梦 2024-10-23 09:03:08

我对 Sweave 文档使用以下形式的 Makefile

pdf: myfile.tex
    R CMD texi2pdf myfile.tex

myfile.tex: myfile.Rnw
    R CMD Sweave myfile.Rnw

可以通过运行命令 make pdf 在 Mac OS 终端中一步构建文档

然后我 当然有一种方法可以让它更接近 Mac OS X 中的一键式目标,但这对我来说已经足够好了。

I use a Makefile of the following form for my Sweave documents:

pdf: myfile.tex
    R CMD texi2pdf myfile.tex

myfile.tex: myfile.Rnw
    R CMD Sweave myfile.Rnw

Then I can build the document in one step in the Mac OS Terminal by running the command make pdf

I'm sure there is a way to bring this closer to your one-click goal in Mac OS X, but this works well enough for me.

顾铮苏瑾 2024-10-23 09:03:08

使用 Gregor Gorjanc 的 Sweave.sh 脚本可以在 TeXShop 中轻松完成一键式 Sweaving。
http://cran.r-project.org/contrib/extra/scripts/Sweave 获取.sh 并将其放入您的 ~/Library/TeXShop/bin/ 文件夹中。

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

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'您必须设置 Sweave.sh 和两个引擎文件的权限以允许执行。

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

注意:“Sweave”选项会自行清理,删除 LaTeX 和 Sweave 创建的所有额外文件。如果您的文件名为 myfile.Rnw,则这将包括名为 myfile.R 和 myfile.tex 的文件。所以忠告一句:确保你的 Rnw 文件的基名是唯一的;那么任何意外的事情都不会被覆盖然后删除。

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

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.

哀由 2024-10-23 09:03:08

在 bash shell 命令行上:

R CMD Sweave foo.Rnw && pdflatex foo.tex

运行 Sweave,如果成功,则继续执行 pdflatex。弹出一个pdf文件。如果你在 bash 终端中得到了这个,那么只需点击向上箭头即可将其恢复并再次执行。又来了。又来了。

Makefile 解决方案也不错。

On the bash shell command line:

R CMD Sweave foo.Rnw && pdflatex foo.tex

Runs Sweave, and if that succeeds it goes on to do pdflatex. Out pops a pdf. If you've got this in a bash Terminal then just hit up-arrow to get it back and do it again. And again. And Again.

Makefile solution also good.

夜未央樱花落 2024-10-23 09:03:08

RStudio 有一个按钮可以一次性完成此操作。需要注意的是,它在自己的会话中运行,因此您可能设置的任何工作区变量都将被忽略。

编译按钮

RStudio has a button that does this in one go. One caveat is that it runs in its own session, so any workspace variables you may have set are ignored.

the compile button

圈圈圆圆圈圈 2024-10-23 09:03:08

请注意:您实际上可以使用 texi2dvi (在工具包中)直接从 R 调用 pdflatex 等内容。例如:

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

texi2pdf("Assign4.tex")

将您的 Rnw 文件编译为 pdf。因此,不需要让 R 来处理 tex->pdf 步骤。

Just a note: you can actually call things like pdflatex etc. directly from R using texi2dvi (in the tools package). For example:

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

texi2pdf("Assign4.tex")

would compile your Rnw file into a pdf. Thus, no need to leave R to handle the tex->pdf step.

错々过的事 2024-10-23 09:03:08

我将它们(保存为 sweave.engine 和 sweavebibtex.engine)用于 texshop 中的自定义引擎。我通常在 R 中编写一个代码块,然后将该块复制到我在 texshop 中打开的 rnw 文件中。我想要一个解决方案,让我可以在同一文档(不是 emacs)中对 R 和 tex 进行语法突出显示和拼写更正。

#!/bin/bash
echo 'SWEAVE | PDFLATEX. Custom engine--Akasurak-16Nov2009'
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
pdflatex "${1%.*}"

第二,对于 bibtex 也是如此:

#!/bin/bash
date
before="$(date +%s)"
echo 'SWEAVE | PDFLATEX | BIBTEX | PDFLATEX | PDFLATEX. Custom engine--Akasurak-16Nov2009'
#Updated 20Jul2010 for auto including Sweave.sty


export PATH=$PATH:/usr/texbin:/usr/local/bin

R CMD Sweave "$1"
R CMD pdflatex "${1%.*}"
bibtex  "${1%.*}.aux"
R CMD pdflatex "${1%.*}"
R CMD pdflatex "${1%.*}"


after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
date
echo Elapsed time '(m:s)': $(date -r $elapsed_seconds +%M:%S)

不能说它们是最好的做事方式,但它们确实有效。

I use these (saved as sweave.engine and sweavebibtex.engine) for custom engines in texshop. I usually work up a code chunk in R, then copy the block into the rnw file I have open in texshop. I'd love a solution that lets me do syntax highlighting and spelling correction of R and tex in the same document (that isnt emacs).

#!/bin/bash
echo 'SWEAVE | PDFLATEX. Custom engine--Akasurak-16Nov2009'
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
pdflatex "${1%.*}"

and the second, for doing bibtex as well:

#!/bin/bash
date
before="$(date +%s)"
echo 'SWEAVE | PDFLATEX | BIBTEX | PDFLATEX | PDFLATEX. Custom engine--Akasurak-16Nov2009'
#Updated 20Jul2010 for auto including Sweave.sty


export PATH=$PATH:/usr/texbin:/usr/local/bin

R CMD Sweave "$1"
R CMD pdflatex "${1%.*}"
bibtex  "${1%.*}.aux"
R CMD pdflatex "${1%.*}"
R CMD pdflatex "${1%.*}"


after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
date
echo Elapsed time '(m:s)': $(date -r $elapsed_seconds +%M:%S)

Can't say they are the best way of doing things, but they do work.

温暖的光 2024-10-23 09:03:08

我使用 Aquamacs 或 Eclipse 来编辑 .Rnw 文件,然后使用以下 shell 函数进行编译和编译:查看它:

sweaveCache () {
  Rscript -e "library(cacheSweave); setCacheDir(getwd()); 
    Sweave('$1.Rnw', driver = cacheSweaveDriver)" && 
  pdflatex --shell-escape $1.tex && 
  open $1.pdf
}

请注意,我正在使用 cacheSweave 驱动程序,这有助于避免不断地重新执行需要很长时间才能运行的代码部分。

顺便说一句,我也尝试切换到 Babel 而不是 Sweave ;不确定我最终会更频繁地使用哪个,但 Babel 肯定有我喜欢的方面。

I use either Aquamacs or Eclipse to do the editing of the .Rnw file, then I use the following shell function to compile & view it:

sweaveCache () {
  Rscript -e "library(cacheSweave); setCacheDir(getwd()); 
    Sweave('$1.Rnw', driver = cacheSweaveDriver)" && 
  pdflatex --shell-escape $1.tex && 
  open $1.pdf
}

Notice that I'm using the cacheSweave driver, which helps avoid constantly re-executing code sections that take a long time to run.

BTW, I'm also trying to switch over to Babel instead of Sweave; not sure which I'll end up using more often, but there are definitely aspects of Babel that I like.

终陌 2024-10-23 09:03:08

最好的解决方案就在这里:您创建一个新的 *.engine 供 TeXShop 使用,然后使用快捷键或 1 按钮进行排版。

http://cameron.bracken.bz/sweave-for-texshop

Cameron也很反应灵敏,所以我强烈推荐他的解决方案。

The best solution is here: you create a new *.engine for TeXShop to use, then typeset using the shortcut or the 1 button.

http://cameron.bracken.bz/sweave-for-texshop

Cameron is also very responsive, so I highly recommend his solution.

层林尽染 2024-10-23 09:03:08

如果您愿意切换到(付费)解决方案,TextMate 有一个 Sweave 插件,可以将您从 .Rnw 转换为 PDF一步完成:编织、排版和查看。与可配置为重新加载 PDF 的 Skim 结合使用,它使调整文件变得非常容易。

If you are open to switching to a (paid) solution, TextMate has a Sweave plugin that takes you from .Rnw to PDF in one step: Sweave, typeset, and view. Combined with Skim, which can be configured to reload PDFs, it makes tweaking files pretty easy.

无敌元气妹 2024-10-23 09:03:08

我遇到了同样的问题(我使用 Mac OSX),我选择下载 Eclipse Classic 3.6.2。然后安装StatET插件。设置起来有点麻烦,但一旦你这样做了,这个环境就很好了,因为你可以使用 pdflatex 一键编译你的 .Rwn Sweave 文档,并为你最喜欢的查看器设置选项,这样当你像它一样编译时,.pdf 就会自动弹出在 TeXShop 中。您也可以在 TeXShop 中执行此操作,但 TeXShop 不太适合调试 .Rnw 文件,并且它不会突出显示 .Rwn 文件中的 R 代码。在 Eclipse 中,您可以自定义语法突出显示(不是 Texclipse 端最好的,但还可以),以便您可以轻松区分 R 和 LaTeX 代码。您还可以从 Eclipse 中启动 R 控制台,它有一个图形对象浏览器。无论如何,我可以继续下去。如果您想了解如何全部安装的详细信息,请给我留言。

I had this same issue (I use Mac OSX) and I opted to download Eclipse Classic 3.6.2. and then installed the StatET plugin. It's a bit hairy to get set up but once you do this environment is nice because you can one-click compile your .Rwn Sweave document using pdflatex and set options for your favorite viewer so the .pdf automatically pops up when you compile like it does in TeXShop. You can do this in TeXShop as well, but TeXShop is lousy for debugging .Rnw files and it doesn't highlight the R-code in the .Rwn file. In Eclipse you can customize the syntax highlighting (not the greatest from the Texclipse end, but ok) so that you can easily distinguish between your R and LaTeX code. You can also launch the R console from within Eclipse and it has a graphical object browser. Anyway, I could go on. If you want details about how to get it all installed, message me.

琉璃繁缕 2024-10-23 09:03:08

猜猜我在这方面迟到了,但我整理了一个网页,记录了我基于 Eclipse 的 Sweave 工作流程(使用一键式 sweave):

http://www.stanford.edu/~messing/ComputationalSocialScienceWorkflow.html

Guess I'm late to the party on this, but I put together a webpage that documents my Sweave workflow based on Eclipse (with one-touch sweave):

http://www.stanford.edu/~messing/ComputationalSocialScienceWorkflow.html

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