LY X&SWEAVEINACTION USING
to facilitate the learning of L
TEX and consequently of
. The section “LYX customisation” shows howto customise LYX to work with
. I close withsome discussion.
LYX & Sweave in action
In this section I give a brief tutorial on using
with LYX. You might also read the “Introduction toLYX” and “The LYX Tutorial” manuals for additionalinformation on the ﬁrst 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 ﬁle with the
menu and save it. Start typing some text. You canpreview your work in a
menu, where * indicates one of the tools/routes(
, etc.) that are used to convert L
. 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
menu andchoose one of the document classes that indicates
, say “article (Sweave noweb)”. That is all.You can continue typing some text.
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.
-like implemen-tation, where the start of a code chunk is indicatedwith
, while a line with
in the ﬁrst 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
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
to go onto a new line. You will notice aspecialsymbol(Figure1)attheendofthelinesmark-ing unbroken newline. Now write the above chunkof R code, save the ﬁle and preview a
. If the
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
,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
TEX code.Follow the
menu, or just type2