1. How to Embedding R Code in L TEX
A
Lighton Phiri
September 13, 2012
Sweave is a function built right within R to facilitate dynamic reporting. Figure
1 is an example of an R plot dynamically created with Sweave and the tikzDevice
package.
5
Intuitive
Simple
4 Satisfied
Subjects
Expected
3 Response
Effective
Beneficial
2
1
0
Search and Browse Navigability Similarity General
Average UX Ratings
Figure 1: An R Barplot Integrated with L TEX
A
Listing 1 is the R command used to produce the ux-evaluation.tex TEXfile that
is used within the main L TEXdocument; Listing 2 shows the Sweave code that
A
was used to generated the barplot in Figure 1; and Listing 3 shows the portion
of the LaTeX document that uses the Sweave output file as input.
1
2. Listing 1: R Command for Processing Sweave File
R CMD Sweave ux−e v a l u l a t i o n . Rnw
Listing 2: R Sweave Code - ux-evaluation.Rnw
<< f i g=FALSE, echo=FALSE>>=
# l o a d r e q u i r e d l i b r a r i e s and d e f i n e environment
require ( ’ t i k z D e v i c e ’ )
t i k z ( ’ ux−e v a l u a t i o n . t e x ’ , standAlone=FALSE, bareBones=FALSE,
width =4.5 , h e i g h t =2.5)
# d e f i n e data source
b o n o l o u i <− read . table ( ” b o n o l o u i . dat ” , h e a d e r=T, s e p=” t ” )
# d e f i n e p l o t area
par ( xpd=T, mar=par ( ) $mar+c ( 0 , 0 , 0 , 3 ) , cex =0.6 , f o n t =2)
barplot ( as . matrix ( b o n o l o u i ) , b e s i d e=TRUE, x l a b=” Average UX R a t i n g s ” ,
y l a b=” S u b j e c t s ” ,
ylim=c ( 0 , 6 ) , col=rainbow ( 7 ) , l a s =1,
s p a c e=c ( 0 , 3 ) , names . a r g=
c ( ” S e a r c h and Browse ” , ” N a v i g a b i l i t y ” ,
” S i m i l a r i t y ” , ” G e n e r a l ” ) , a x e s=FALSE, ann=FALSE, f o n t . l a b =2)
axis ( 2 , l a s =1, a t =(seq ( 0 , 5 , by=1)))
legend ( 3 8 , 5 , c ( ” I n t u i t i v e ” , ” Simple ” ,
” S a t i s f i e d ” , ” Expected ” ,
” Response ” , ” E f f e c t i v e ” , ” B e n e f i c i a l ” ) ,
f i l l =rainbow ( 7 ) , bty=” o ” , text . width =5);
abline ( h=(seq ( 0 , 5 , 0 . 5 ) ) , col=” l i g h t g r a y ” , l t y=” d o t t e d ” )
#box (” i n n e r ” , ” d o t t e d ”)
@
Listing 3: Portion of LaTeX Document
d o c u m e n t c l a s s [ a4paper , 1 0 pt ] { a r t i c l e }
usepackage { t i k z }
:
b e g i n { document }
:
b e g i n { f i g u r e } [ ht ]
centering
framebox [ t e x t w i d t h ] {
i n p u t {ux−e v a l u a t i o n . t e x }
}
c a p t i o n {An R B a r p l o t I n t e g r a t e d with LaTeX}
l a b e l { r−b a r p l o t }
end { f i g u r e }
:
end { document }
2