Sublime Forum

R, sweave, and ST2

#1

Hello all,

I recently made the switch from TextMate2 to SublimeText2 and I am really impressed with the editor so far. That said, I am still trying to get some of the functionality I had in TextMate in SublimeText2. I am going to outline two of the things I am trying to accomplish below with the hope that somebody can give me some advice.

  1. I would like to be able to “sweave, typeset and view” a .Rnw file in the same way that I can in TextMate. The idea is that I can type cmd + R (cmd + B in ST2) and the R chunks are sent to R, sweaved, and then placed into a .tex file. That file is then compiled and SKIM is called to open the corresponding PDF file. My understanding is that I need to create a custom build in ST2 to do this. This is what I have so far:
{
	"selector": "source.Rnw"],

	"cmd": "osascript", "-e", "tell app \"R64\" to cmd \"setwd('$file_path')\"\n", 
						 "-e", "tell app \"R64\" to cmd \"Sweave('$file')\"\n"],


	"target": "make_pdf",
	"selector": "text.tex.latex",
	"osx":
	{
		"cmd": "latexmk", "-cd",
				"-e", "\\$pdflatex = 'pdflatex %O -interaction=nonstopmode -synctex=1 %S'",
				//"-silent",
				"-f", "-pdf"],
		"path": "$PATH:/usr/texbin:/usr/local/bin",
		"file_regex": "^(...*?):([0-9]+): ([0-9]*)(^\\.]+)"
	}
}

Basically I am calling R, setting the working directory, and sweaving the .Rnw file. This part works fine. The .tex file is created and can be compiled if opened in ST2 independently. What I cannot do is make this build compile the .tex file after it is created. The second half of the code (from “target”: “make_pdf”, to the end) is copied from the LaTeX build. So any insight on how I can get this to work would be great. Now on to problem 2.

  1. I would like to be able to use the Rconsole in one window and have my .R file in the other. I would like to build the .R file and have it sent to the R console in the first window. RIght now I am able to build the .R file, but the R.app is opened and the output is shown there, not in ST2. Any insight on how I could get this to work would also be appreciated. Thank you in advance.
0 Likes

Support for .Rnw files
#2

Bump

0 Likes

#3

Do you have SublimeREPL and LaTeXTools installed on your machine?

0 Likes

#4

Yes, I do. In fact, what I am trying to accomplish relies on SublimeREPL. I see it as the next logical step in order to bring the functionality of R in sublime text up to par with Textmate or R Studio.

0 Likes

#5

Cool. I have just started to work on this, as well. I am more interested in 1st scenario you describe, as I don’t think I’ll be moving totally away from RStudio all that soon. But yes, writing/editing/compiling .Rnw in ST2 will be a great feature.

0 Likes

#6

I’ve put what I’ve done so far on Github. Please feel free to take a look and help out. I thought a lot more people would be wanting this type of functionality. Hopefully somebody who knows more than I do will be able to contribute.

github.com/jvcasill/Sweave-in-ST2

0 Likes

#7

I have found a solution to problem 2 on this website:

tomschenkjr.net/using-sublime-text-2-for-r/

The key was setting the path in the Sublime-REPL preferences to “/usr/bin/r”. Im still working on problem 1.

0 Likes

#8

Just started working on this, but what I have so far will compile the Sweave file and then the resulting .tex file and open skim to view it. Be advised this only has been tested/works on a mac.

"{

    "cmd": "bash", "-c", "/usr/bin/R64 CMD Sweave '${file_name}' && pdflatex '${file_base_name}.tex' -interaction=nonstopmode -synctex=1 %S -f -pdf && /Applications/Skim.app/Contents/MacOS/Skim '${file_base_name}.pdf'"], 
    "path": "$PATH:/usr/texbin:/usr/local/bin", 
    "selector": "text.tex.latex.sweave","shell":false,
    "file_regex": "^(...*?):([0-9]+): ([0-9]*)(^\\.]+)"
}

Obviously the goal will be to tie this into the awesome framework that is LaTeXtools…

0 Likes

#9

This sounds good. Can someone please post steps (for newbies like myself) on how to get this working?

0 Likes