Showing posts with label Sweave. Show all posts
Showing posts with label Sweave. Show all posts

2010-07-28

IPSUR book used LyX-Sweave

Jay Kerns wrote a book titled "Introduction to Probability and Statistics Using R" or IPSUR for short. He has put up a web site for the book and Rcmdr plugin. There are at least two very important points from my side (I did not read the book, yet): 1. it is free to download (thank you Jay!!!) and 2. it was written with the help of LyX-Sweave. This shows that combination of LyX and Sweave is really powerful.

2010-06-29

Sweave.sh in Eclipse-StatET

Sébastien Bihorel sent the following instructions on how to use my sweave.sh shell script in Eclipse-StatET.
1- First, you need to know the path to your TEXINPUTS settings. Type R CMD env |grep TEXINPUTS in a shell. In my installation (opensuse 11.2), the shell returned the following
TEXINPUTS=.::/usr/lib/R/share/texmf:

2- Edit your .bashrc file (located in your home directory) and add the following statement
export TEXINPUTS

3- Download Gregor Gorjanc's sweave.sh script at http://cran.r-project.org/contrib/extra/scripts/Sweave.sh. Copy it to /usr/local/bin, rename the file to sweave (mv sweave.sh sweave) and make the file executable (chmod +ax sweave) if it is not already.

4- Open Eclipse and create a new Program in External Tools Configuration using the following settings:
>Main:
- location: /usr/local/bin/sweave
- working directory: ${container_loc}
- Arguments: -ld ${resource_loc}

>Refresh:
no selection

>Build:
Build before launch: checked
The project containing the selected resource: checked
Include referenced projects: checked

>Environment: added a new variable
Variable: TEXINPUTS
Value: .:/usr/lib/R/share/texmf: <- Use here the path obtained at step 1 (for some reason you have to add .: before the path and : after. Do not use quotes around the Value) Append environment to native environment: checked >Common:
Local file: checked
Console encoding: default - inherited (UTF-8)
Standard Input and Ouput: Allocate console
Launch in background: checked


Now you should be able to see sweave as a new program in your main screen. Hope it helps

Sebastien

2009-01-21

Sweave.sh plays with weaver

After adding support for the cacheSweave package (see here) I have also added support for the weaver package to my Sweave.sh script. The experimental version is available here. After testing and feedback I will upload it to CRAN. One can use this new feature with the command (see previous post for the Sweave file):
Sweave.sh --weaver test.Rnw
This will turn caching on. If one has Sweave file with code chunks specifying chunk=true, but would like to recompute all information that is cached, the following command can be used:
Sweave.sh --skipweaver test.Rnw
I noticed that weaver does not play nice with example from my previous posts, i.e., it still waits for 15 seconds. My guess is that cacheSweave is a more complete implementation, but I might be wrong!

2009-01-17

Versions of Sweave.sh

There are now many places where one can find "my" Sweave.sh shell script for running Sweave and post processing with LaTeX directly from the command line. I published first version of Sweave.sh here (web page of the department). Later I moved my pages to Google Pages and uploaded the script to CRAN. There has been at least one update (adding the support for cacheSweave) since first version and the version at CRAN is the most recent one and can be treated as "the offical and stable" version. New versions will be uploaded directly to CRAN, while the old site will be switched off in the near future.

Use of include and input in Sweave documents

When you write a large structured document using LaTeX, it is wise to use \input{} and/or \include{} commands (see here for a nice description). However, you can not "fully" use these two commands with Sweave documents. You can use them, but when you weave the master file, the \input{} and \include{} files are not weaved! The author of Sweave instead implemented the \SweaveInput{} command. If you still want to use \input{} and/or \include{}, then you can take the following approach.

Say we have a master file file0.Rnw, which includes file1.Rnw and file2.Rnw. Then you need first to run Sweave on file1.Rnw and file2.Rnw and at the end on file0.Rnw. Warning! - there can be no interaction between files, which basically means that you can use this approach only if each file represents a complete analysis. This might be a suitable Makefile in such cases:
all: sweave # Compile the whole document
Sweave.sh --latex2pdf file0.Rnw

sweave: # Sweave individual files
Sweave.sh file1.Rnw file2.Rnw
If master file is a pure LaTeX file, you can change the all target to:
all: sweave # Compile the whole document
Sweave.sh --noweave --latex2pdf file0.tex

2008-11-26

Sweave.sh plays with cacheSweave

I have added support for caching to Sweave.sh script as implemented in cacheSweave R package written by Roger D. Peng. Now, one can set caching on for chunks that are time consuming (data import, some calculations, ...) and the Sweaving process will reuse the cached objects each time they are needed. Read the details about the cacheSweave package in the package vignette. Option --cache for Sweave.sh script should also be easy to understand. However, here is a minimalist example:
\documentclass{article}
\usepackage{Sweave}
\begin{document}

<<setup>>=
n <- 10
s <- 15
@

Let us first simulate \Sexpr{n} values from a normal distribution and add a \Sexpr{s} sec pause to show the effect of caching.

<<simulate, cache=true>>=
x <- rnorm(n)
Sys.sleep(s)
@

Now print the values:

<<print, results=verbatim>>=
print(x)
@

\end{document}
Now, one can run the following command:
Sweave.sh --cache test.Rnw
and the output on the command line is:
Run Sweave and postprocess with LaTeX directly from the command line
- cache mode via cacheSweave R package

R version 2.8.0 (2008-10-20)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(package='cacheSweave'); Sweave(file='test.Rnw', driver=cacheSweaveDriver);
Loading required package: filehash
filehash: Simple key-value database (2.0 2008-08-03)
Loading required package: stashR
A Set of Tools for Administering SHared Repositories (0.3-2 2008-04-30)
Writing to file test.tex
Processing code chunks ...
1 : echo term verbatim (label=setup)
2 : echo term verbatim (label=simulate)
3 : echo term verbatim (label=print)

You can now run LaTeX on 'test.tex'
When you repeat the Sweaving process, which you more or less always do, there is no need to wait for 15 second since cacheSweave package takes the x object from the cache! Excellent job Roger!

2008-09-03

Using Beamer with LyX-Sweave

Dan Weeks (web1, web2) provided a solution on how to use Beamer with LyX & Sweave.

Make a literate-beamer layout file for LyX!

#% Do not delete the line below; configure depends on this
# \DeclareLaTeXClass[beamer, Sweave.sty]{beamer (beamer Sweave noweb)}
#
# This is a copy of literate-article.layout from LyX, but changed for
# Sweave - NoWeb syntax:
# - changed noweb.sty to Sweave.sty
# - moved preamble to literate-scrap.inc

Format 2
Input beamer.layout
Input literate-scrap.inc


LyX then has a new Document Class that allows one to use the "scrap" style, and that portion of the text is sent to Sweave properly. However, there is a problem related with verbatim environment. Sweave makes a lot of use of verbatim environments, and beamer doesn't like those. You need to declare that a slide contains verbatim or you get errors. The reasonable solution is to add two ERT boxes in each slide that will contain "Sweave material". The ERT box at the beginning of the slide should contain

\begin{frame}[containsverbatim]
\frametitle{Slide title}


while the ERT box at the endo of the slide should contain

\end{frame}

2008-07-12

Mixing Noweb and Sweave in LyX

Ed asked:
Is it possible to configure LyX so that I can create a literate article, book or report with either the "standard" LyX/NoWeb settings or the Sweave/R settings? I've spent a few hours trying to figure out how to doit, but everything I've tried only gives me one of the two options.
You want to mix Noweb and Sweave in the same file? No way, since the literate layout files (see http://cran.r-project.org/contrib/extra/lyx/) were modified for Sweave. You can also not use Noweb in one file and Sweave in another file without changing the literate layouts and preferences file.

2008-07-11

LyX-Sweave on MS Windows

Jay Kerns has recently notified me that for LyX-Sweave on MS Windows one needs to add the bin directory of R installation (say contrib/extra/lyx/) to the PATH system variable. He is right, since LyX can not find R binary otherwise. On MS Windows I always add R to PATH variable, since I often use R CMD check or other commands in MS Windows Command Tool - terminal. New version of INSTALL file will be updated with this information. Goosh, administration of Linux is getting better and better in comparison to MS Windows, at least for me.

2008-06-09

Using Sweave with LyX customisation script

Gabor suggested I should write a short batch file or R script which would copy the various files needed to use Sweave in LyX so that one does not need to actually do it onself. That would be nice, but there are several things that I should take care of:
  • I would need to find the LyX user/library directory. I do not know how to get his information in scriptable manner.
  • If the preference file already exists, I would have to append new content. That is easy, but in case some definitions are already used I would first need to parse the old version of prefences. That would not be so easy.
  • I would need to write a Unix shell script for Unix like systems and a batch file for MS Windows. I am not sure about Mac OS-X - I think Unix shell script should work there out of the box.
I think I will leave this step to someone else. I actually hope, that LyX developers might find a way to provide automatic customisation if R is installed on the computer. Some ideas were given here. Additionally, Gabors' {Sweave,Stangle}.bat could be used instead of {Sweave,Stangle}.sh that are shipped with R.

2008-06-08

Using Sweave with LyX on MS Windows

Ian Holliday has informed me that my solution for using Sweave with LyX has additional requirement that I have not stated. Beside LyX & R (obviously) one needs also a variant of Unix shell. I have not realized this before since I usually use Linux, which always has installed a variant of Unix shell. Additionally, I always install a set of Unix tools (via Cygwin or MSYS) on my MS Windows boxes. Therefore, I could not realize that Unix shell is needed. However, I could have figured this requirement since R CMD Sweave and R CMD Stangle (commands that drive the weaving and the tangling process - this two commands are also inserted in LyXs' preferences file) use shell scripts Sweave.sh and Stangle.sh. I have provided additional info about this issue in INSTALL file. New version of the Rnews paper will appear soon.

2008-04-30

Sweave (bash) shell script II

Fixed buglet with --noquiet, -nq option. Fixed version is available here. I will not post this version to CRAN, not yet.

2008-04-27

Sweave (bash) shell script

My Sweave.sh (bash) shell script eases the compilation of Sweave (link1, link2) file to Postscript or PDF format i.e. Sweave --> LaTeX --> Postscript/PDF. Although, this seems an easy task, it can get quite involved, since there are many ways and glitches on how to compile LaTeX (LaTeX web page, Wikipedia) source file. For example:
  • LaTeX --> PS via texi2dvi or latex and dvips and then ps2pdf for PS --> PDF,
  • LaTeX --> PDF via texi2pdf or pdflatex.
Until now Sweave.sh worked only with Sweave source files i.e. a mix of LaTeX and R code. Today I have modified it in such a way so that it can also work with LaTeX file only. Now I can easily type
Sweave.sh --noweave -otld file.tex
to compile file.tex to PDF and open it directly in default PDF previewer. New version will be posted on CRAN. For the impatient I uploaded the script to my webpage.

See also:

2007-12-28

Using Sweave with LyX

It has been a while since I managed to configure LyX (a great "word processor") to work with Sweave (literate programming using LaTeX and R - wikipedia page). The paper describing the whole thing will probably be published in the next issue of Rnews. Since, I received some inquiries, I have created a temporary web page at Google with all the files needed for the configuration, ...

Happy LyX-ing with R!

2007-08-02

Computer Algebra System (CAS) in LyX via Ryacas

I have answered question on using Computer Algebra System (CAS) in LyX via Ryacas. You can see the answer here. In summary, LyX has a way to work with different CAS internally, but I have shown how to use Ryacas via Sweave in LyX. I sent yesterday a note on the latter to Rnews. Stay tuned!

2007-04-16