Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
27Activity
0 of .
Results for:
No results containing your search query
P. 1
Using Sweave with LyX

Using Sweave with LyX

Ratings:

5.0

(3)
|Views: 3,813|Likes:
Published by Gregor Gorjanc
Gorjanc, G. 2008. Using Sweave with LyX. R news, 8(1): 2-9
Gorjanc, G. 2008. Using Sweave with LyX. R news, 8(1): 2-9

More info:

Categories:Types, School Work
Published by: Gregor Gorjanc on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less

11/27/2012

pdf

 
USING 
SWEAVE
WITHL
Using
Sweave
with LYX
How to lower the L
A
TEX/
Sweave
learning curve
by Gregor Gorjanc
Introduction
L
A
TEX (L
A
TEX Project,2005) is a powerful typesetting language, but some people find that acquiring aknowledge of L
A
TEX presents a steep learning curvein comparison to other “document processors.” Un-fortunately this extends also to “tools” that rely onL
A
TEX. Such an example is
Sweave
(Leisch, 2002),which combines the power of R and L
A
TEX using lit-erate programming as implemented in
noweb
(Ram-sey, 2006). Literate programming is a methodologyof combining program code and documentation inone (source) file. In the case of 
Sweave
, source filecan be seen as a L
A
TEX file with parts (chunks) of Rcode. The primary goal of 
Sweave
is not in docu-menting the R code, but in the delivery of results of a data analysis. L
A
TEX is used to write the text, whileR code is replaced with its results during the processof compiling the document. Therefore,
Sweave
is infact a literate reporting tool.
Sweave
is of consider-able value, but its use is somewhat hindered by thesteep learning curve needed to acquire L
A
TEX.The R package
odfWeave
(Kuhn,2006) uses the same principle as
Sweave
, but instead of L
A
TEX usesan
XML
-based markup language named Open Docu-ment Format (
ODF
). This format can be easily editedin OpenOffice. Although it seems that
odfWeave
solves problems for non-L
A
TEX users, L
A
TEX has qual-ities superior to those of OpenOffice. However, thegap is getting narrower with tools like OOoL
A
TEX(Piroux, 2005), an OpenOffice macro for writingL
A
TEX equations in OpenOffice, and Writer
2 L
A
TEX( Just, 2006), which provides the possibility of con-verting OpenOffice documents to L
A
TEX. L
A
TEX hasexisted for decades and it appears it will remain inuse. Anything that helps us to acquire and/or useL
A
TEX is therefore welcome. LYX (LYX Project, 2006)definitely is such tool.LYX is an open source document preparation sys-tem that works with L
A
TEX and other “companion”tools. Inshort, IseeLYXasa“Word”-likeWYSIWYM(What You See Is What You Mean) front-end for edit-ingL
A
TEXfiles, withexcellentimportandexportfacil-ities. Manuals shipped with LYX and posted on thewiki site (
)give an accessibleand detailed description of LYX, as well as pointersto L
A
TEX documentation. I heartily recommend theseresources for studying LYX and L
A
TEX. Additionally,LYX runs on Unix-like systems, including MacOSX,as well as on MS Windows. The LYX installer forMS Windows provides a neat way to install all thetools that are needed to work with L
A
TEX in general.This is not a problem for GNU/Linux distributionssince package management tools take care of the de-pendencies. TEX Live (TEX Live Project, 2006) is an-other way to get L
A
TEX and accompanying tools forUnix, MacOSX, and MS Windows. LYX is an idealtool for those who may struggle with L
A
TEX, and itwould be an advantage if it could also be used for
Sweave
.Johnson(2006)was the first to embark on this initiative. I have followed his idea and extendedhis work using recent developments in R and LYX.In the following paragraphs I give a short tutorial“LYX & Sweave in action”, where I also show a way1
 
LX&SWEAVEINACTION USIN
SWEAVE
WITHL
to facilitate the learning of L
A
TEX and consequently of 
Sweave
. The section “LYX customisation” shows howto customise LYX to work with
Sweave
. I close withsome discussion.
LYX & Sweave in action
In this section I give a brief tutorial on using
Sweave
with LYX. You might also read the “Introduction toLYX” and “The LYX Tutorial” manuals for additionalinformation on the first steps with LYX. In order toactively follow this tutorial you have to customiseLYX as described in the section “LYX customisation”.Open LYX, create a new file with the
File
–>
New
menu and save it. Start typing some text. You canpreview your work in a
PDF
via the
View
–>
PDF(*)
menu, where * indicates one of the tools/routes(
latex
,
pdflatex
, etc.) that are used to convert L
A
TEXfile to
PDF
. The availability of different routes of con-version,aswellassomeothercommands,dependonthe availability of converters on your computer.
The literate document class
To enable literate programming with R you need tochoose a document class that supports this method-ology. Follow the
Document
–>
Settings
menu andchoose one of the document classes that indicates
Sweave
, say “article (Sweave noweb)”. That is all.You can continue typing some text.
Code chunk
To enter R code you have to choose an appropriatestyle so that LYX will recognise this as program code.R code typed with a standard style will be treated asstandard text. Click on the button “Standard” (Fig-ure1— top left) and choose a scrap style, which isused for program code (chunks) in literate program-ming documents. You will notice that now the textyou type has a different colour (Figure1). This is anindicator that you are in a paragraph with a scrapstyle. There are different implementations of literateprogramming.
Sweave
uses a
noweb
-like implemen-tation, where the start of a code chunk is indicatedwith
<<>>=
, while a line with
@
in the first columnindicates the end of a code chunk (Figure1). Try en-tering:
<<myFirstChunkInLyX>>=xObs <- 100; xMean <- 10; xVar <- 9x <- rnorm(n=xObs, mean=xMean, sd=sqrt(xVar)) mean(x)@
Did you encounter any problems after hitting the
ENTER
key? LYX tries to be restrictive with spaces andnew lines. A new line always starts a new paragraphwith a standard style. To keep the code “together”in one paragraph of a scrap style, you have to use
CTRL+ENTER
to go onto a new line. You will notice aspecialsymbol(Figure1)attheendofthelinesmark-ing unbroken newline. Now write the above chunkof R code, save the file and preview a
PDF
. If the
PDF
is not shown, check the customisation part orread further about errors in code chunks. You canuse all the code chunk options in the
<<>>=
markuppart. For example
<<echo=FALSE, fig=TRUE>>=
,will have an effect of hidding output from R func-tions, while plots will be produced and displayed.
Inline code chunks
LYX also supports the inclusion of plain L
A
TEX code.Follow the
Insert
–>
TeX Code
menu, or just type2
 
LX&SWEAVEINACTION USIN
SWEAVE
WITHL
Figure 1: Screenshot of LYX with
Sweave
in action: LYX GUI (top-left), produced PDF (top-right), source code(Sweave) in an editor (bottom-left), and echo from weaving in a terminal (bottom-right)3

Activity (27)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
armandog5 liked this
Kalle Berg liked this
Serge Dossongui liked this
JimWat liked this
m1riojose liked this
jrinconr liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->