Romain Francois, Professional R Enthusiast

To content | To menu | To search

Tag - sweave

Entries feed - Comments feed

Thursday, January 22 2009

Code2html speaks Latex too

For highlighting code inside latex documents there are several options:
This might be a judgement call, but I don't find the latex output of listings or highlight to be visually pleasing enough, but maybe it is just a lack of understanding on how to customize them. Anyway, jedit has the Code2HTML plugin which does a good job of translating the buffer being edited in jedit into html markup, preserving pretty much everything that appear on the jedit text area. This takes advantage of jedit mode files which offers a great deal of flexibility, including nested languages, such as javascript/php, or R and Sweave if you have the right mode files.
So I had a look into the code of the Code2HTML plugin which is simple enough, and have basically done a big s/html/latex/g to support latex output (I realize this is not the best option and I should probably abstract things out so that it would be easier to add new formats: ansi, svg, ...) but it does the trick for now. Here is a screenshot of an example file edited in jedit: ScreenshotJedit.png And a screenshot of the pdf file ScreenshotPdf.png Here are the relevant files if you want to try it out: Next steps:
  • do something about the gutter
  • figure out how to invoke that from the command line
  • write a sweave driver that would call it so that R code appears formatted in sweave documents

Wednesday, January 21 2009

python code in sweave document

It would be great if we could not only use R or S in sweave code chunks but also some other languages such as python for example. Why would you want that, well python has some additional graphics capabilities R does not have, some software is written in python but you still want to write your document in sweave, ... Here is a first attempt, obviously not complete.

A custom sweave driver

The first trick is to write a custom sweave driver, based on the basic RweaveLatex driver which does something with the content of a chunk when the engine is set to python :

driver <- RweaveLatex() 
runcode <- driver$runcode
driver$runcode <- function(object, chunk, options){
if( options$engine == "python" ){
driver$writedoc( object, c("\\begin{python}", chunk, "\\end{python}") )
} else{
runcode( object, chunk, options )
}
}
Sweave( "python.Rnw", driver = driver )
The only thing the driver does is convert python code chunks into a python environment, so that this in the Rnw file:
<<hello,engine=python>>=
print "hello"
print "world"
@
becomes that in the tex file:
\begin{python}
print "hello"
print "world"
\end{python}

Process the python code


Then you need to install the python package into your texmf tree and texhash (just google around if you don't know what it means). The python package defines the python environment so that when you compile the tex file, latex calls python and brings back the output of the python script. The catch is that you need to compile your tex file with the option -shell-escape.
$ pdflatex -shell-escape python.tex

Beyond the simple trick


So we can get hello world from python, this needs more thinking to enable:
  • production of graphics from python with a fig option, just like you do it in R, see this for example
  • some way to share the data between R and python so that variables created in the R world could be used in the python world and vice-versa, I don't know the best way to do that at the moment, but from the  top of my head we could either use rpy for the communication or the database that gets generated by the cacheSweave package

Monday, January 12 2009

R code completion in sweave chunks

In this post I said that it would be useful to add completion of R code within a sweave chunk, and today I finally found the time to play with it.

Completion of R code within a Sweave chunk

You need revision 195 at least to get this going.

Wednesday, December 31 2008

Edit Sweave files with the workbench

Edit Sweave Files with the Workbench

Sweave is a very useful combination of LaTeX and R together in one document. You can find more information about sweave by visiting its homepage or by simply typing ?Sweave at your R command line.
This post demonstrates some of the features of the Power Editor plugin for the biocep workbench when editing Sweave files, we will see other features in subsequent posts.

The LaTeXTools plugin for jedit gave a good starting point for Sweave integration as most of the parsing of LaTeX syntax is directly borrowed from it, however the plugin could not directly cope with the mixture of latex and R in the same document, so there is a small bit of coding around it to get things working. Also the sidekick tree for latex gives a too restrictive set of icons for the sections of the file, so some coding was needed to get a nice R icon to represent a sweave code chunk.

Here is a screenshot of the workbench when editing a sweave file, this example is the grid vignette, which you may find by typing :
R> vignette( "grid", package = "grid")$file

You can see the sidekick view on the right showing a browsable outline of the document. The editor and the sidekick view are synchronized so when you click on a node of the tree, the editor will scroll to the appropriate location and when you move to some other part of the document, the tree will update to show the location being edited.


The Power Editor plugin also allows to visually identify documentation and code parts of the document as you can see in the following screenshot where Sweave code chunks are being highlighted with a light blue background.


Requirements

To get the features documented here, you need both updated versions of biocep and the Power Editor Plugin (at least revision 194). I will do an other post about how to install these things.
You also need R and Sweave mode files (I still need to find a way to embed them in the plugin) saved in your jedit settings directory with the following lines in your catalog file :

<MODE NAME="R"    FILE="r.xml" 
FILE_NAME_GLOB="*.R"
FIRST_LINE_GLOB="#!/*{R,Rscript}" />
<MODE NAME="sweave"   
FILE="sweave.xml"
FILE_NAME_GLOB="*.{R,S}nw" />

Coming Next

It would be nice to :
  • allow preview of graphics when fig=TRUE is set, I need to understand some of the packages providing cache feature for Sweave
  • have R completion when inside the code chunk , see this post
  • completion of the options used by the sweave driver
  • actions to weave and tangle the current file
  • jump between sweave code chunks
  • integrate this as a view
  • support the html flavour of sweave