More efficient R / Sweave / TeXShop work-flow?
Asked Answered
G

11

19

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.

Geraint answered 7/2, 2011 at 14:31 Comment(0)
N
10

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.

Nonu answered 7/2, 2011 at 16:33 Comment(12)
Thanks Aaron; I followed your instruction; however still no luck. The TeXShop console spits out two lines (after compiling with Sweave option in the dropdown): /Users/kg/Library/TeXShop/Engines/Sweave.engine: line 2: /Users/kg/Library/TeXShop/bin/Sweave: No such file or directoryGeraint
@user594795: Try modifying the two engine files to say "Sweave.sh" instead of just "Sweave"; I've edited my answer accordingly.Nonu
You mentioned above I need to set the permissions. I took that to mean changing all the files to "read & write" after clicking "Get Info" on the files. The reason I ask is that now I am getting the following message (still in the TeXShop console): /Users/kg/Library/TeXShop/Engines/Sweave.engine: line 2: /Users/kg/Library/TeXShop/bin/Sweave.sh: Permission deniedGeraint
The permissions you need to set are read and execute. From the command line, it's chmod +x <filename> to add the execute permission.Nonu
It now works great -- thank you for your help and patience! I'm happy to finally be learning about the new world of the Terminal.Geraint
This is very helpful. One note that I had missed originally is to make sure that your file is a .Rnw file.Conjunctive
Thanks Aaron. Really handy! I prefer it compared to the Eclipse StatET Sweave option on Mac OSx (pdf does not refresh there).Whimsy
@Jan: To have PDFs automatically refresh on a Mac, use the Skim pdf viewer instead of Preview.Nonu
@Aaron: I am using Skim pdf viever as the default pdf viewer now and the pdf is still not refreshing using StatET within Eclipse. The TexShop Sweave option explained above is working fine.Whimsy
@Jan: When the file is changed by running latex from the command line, Skim notices that it changed and asks if it should automatically update it when changes are noticed. I thought this would happen regardless of what changed the file, but it appears not. Glad to hear TeXShop Sweave is working fine.Nonu
@Aaron Thanks for the advice - it was really helpful! If it's useful the exact command I had to run was chmod +x Sweave.sh when in \bin. In addition I needed to add a Sweave.sty file to the folder I'm working in. Do you know if I can move the .sty permanently somewhere else?Immerse
Put a link to it in ~/Library/texmf/tex/latex. See pstat.ucsb.edu/faculty/niemi/2011/03/30/sweave-on-macosxNonu
S
12

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.

Snifter answered 7/2, 2011 at 14:59 Comment(1)
I also use a makefile for Sweave; I posted several examples of makefiles for Sweave compilation: see the links at the bottom of this post: jeromyanglim.blogspot.com/2010/12/…Attaway
N
10

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.

Nonu answered 7/2, 2011 at 16:33 Comment(12)
Thanks Aaron; I followed your instruction; however still no luck. The TeXShop console spits out two lines (after compiling with Sweave option in the dropdown): /Users/kg/Library/TeXShop/Engines/Sweave.engine: line 2: /Users/kg/Library/TeXShop/bin/Sweave: No such file or directoryGeraint
@user594795: Try modifying the two engine files to say "Sweave.sh" instead of just "Sweave"; I've edited my answer accordingly.Nonu
You mentioned above I need to set the permissions. I took that to mean changing all the files to "read & write" after clicking "Get Info" on the files. The reason I ask is that now I am getting the following message (still in the TeXShop console): /Users/kg/Library/TeXShop/Engines/Sweave.engine: line 2: /Users/kg/Library/TeXShop/bin/Sweave.sh: Permission deniedGeraint
The permissions you need to set are read and execute. From the command line, it's chmod +x <filename> to add the execute permission.Nonu
It now works great -- thank you for your help and patience! I'm happy to finally be learning about the new world of the Terminal.Geraint
This is very helpful. One note that I had missed originally is to make sure that your file is a .Rnw file.Conjunctive
Thanks Aaron. Really handy! I prefer it compared to the Eclipse StatET Sweave option on Mac OSx (pdf does not refresh there).Whimsy
@Jan: To have PDFs automatically refresh on a Mac, use the Skim pdf viewer instead of Preview.Nonu
@Aaron: I am using Skim pdf viever as the default pdf viewer now and the pdf is still not refreshing using StatET within Eclipse. The TexShop Sweave option explained above is working fine.Whimsy
@Jan: When the file is changed by running latex from the command line, Skim notices that it changed and asks if it should automatically update it when changes are noticed. I thought this would happen regardless of what changed the file, but it appears not. Glad to hear TeXShop Sweave is working fine.Nonu
@Aaron Thanks for the advice - it was really helpful! If it's useful the exact command I had to run was chmod +x Sweave.sh when in \bin. In addition I needed to add a Sweave.sty file to the folder I'm working in. Do you know if I can move the .sty permanently somewhere else?Immerse
Put a link to it in ~/Library/texmf/tex/latex. See pstat.ucsb.edu/faculty/niemi/2011/03/30/sweave-on-macosxNonu
F
8

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.

Forney answered 7/2, 2011 at 15:3 Comment(2)
I use this too but a bit longer (also you can drop file extensions). Basically it just adds bibtex in the chain and opens the pdf when its done: R CMD Sweave foo && R CMD pdflatex foo && R CMD bibtex foo && foo.pdfProximo
Isn't it possible to say R CMD Sweave --pdf foo.Rnw to get a pdf directly?Idolla
P
8

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

Pansypant answered 6/4, 2011 at 16:27 Comment(0)
D
7

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.

Drizzle answered 19/11, 2011 at 5:25 Comment(0)
R
2

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.

Robinrobina answered 11/3, 2011 at 18:31 Comment(0)
G
2

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.

Grub answered 16/3, 2011 at 16:8 Comment(0)
P
2

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.

Praise answered 5/4, 2011 at 19:54 Comment(0)
A
1

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.

Aspect answered 7/2, 2011 at 16:8 Comment(0)
S
1

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.

Santoyo answered 12/3, 2011 at 7:35 Comment(0)
M
1

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

Midvictorian answered 20/3, 2011 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.