More efficient R / Sweave / TeXShop work-flow?

2020-05-11 21:24发布

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.

11条回答
三岁会撩人
2楼-- · 2020-05-11 21:33

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.

查看更多
来,给爷笑一个
3楼-- · 2020-05-11 21:36

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.

查看更多
别忘想泡老子
4楼-- · 2020-05-11 21:36

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.

查看更多
萌系小妹纸
5楼-- · 2020-05-11 21:36

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

查看更多
唯我独甜
6楼-- · 2020-05-11 21:37

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.

查看更多
ら.Afraid
7楼-- · 2020-05-11 21:38

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.

查看更多
登录 后发表回答