Beamer Adding Sweave - Biostatistics - Academic …yho/ph8400/HoExample4.pdf · Beamer Adding...

24
Beamer Adding Sweave Making Nice Slides using Beamer and Sweave Yen-Yi Ho Division of Biostatistics School of Public Health University of Minnesota 1/24

Transcript of Beamer Adding Sweave - Biostatistics - Academic …yho/ph8400/HoExample4.pdf · Beamer Adding...

BeamerAdding Sweave

Making Nice Slides using Beamer and Sweave

Yen-Yi Ho

Division of BiostatisticsSchool of Public HealthUniversity of Minnesota

1/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Syntax in Beamer

\documentclass{beamer}\usepackage{beamerthemeshadow}\title{Mytitle}

\begin{document}\begin{frame}\titlepage\end{frame}

\begin{frame}Content for Slide 1\end{frame}

\end{document}2/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

lists with pause

Introduction to LATEX

Course 2

Termpapers and presentations with LATEX

Beamer class

3/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

lists with pause

Introduction to LATEX

Course 2

Termpapers and presentations with LATEX

Beamer class

4/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

lists with pause

Introduction to LATEX

Course 2

Termpapers and presentations with LATEX

Beamer class

5/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

lists with pause

Introduction to LATEX

Course 2

Termpapers and presentations with LATEX

Beamer class

6/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Tables

Date Instructor TitleWS 04/05 John Mich First steps with LATEX

SS 05 Yen-Yi Ho LATEX Course serial

7/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

splitting screen

Beamer

Beamer Class

Beamer Class Latex

Instructor TitleJohn LATEX Course 1

Anding Course serial

8/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Animation

subject 1

subject 2

9/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Animation

subject 1

subject 2

150.

00

3.00

2.00

0.50

0.75

1.00

1.50

5.00

12.5

0

25.0

0

50.0

0

75.0

0

4161311812219141011151737208569

Relative Concentration

Pro

bese

t

SpikeIn.pm

−2 0 2Row Z−Score

060

140

Color Keyand Histogram

Cou

nt

10/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Animation

subject 1

subject 2

11/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Animation

subject 1

subject 2

1.00

1.50

2.00

0.50

0.75

3.00

12.5

0

5.00

25.0

0

50.0

0

75.0

0

150.

00

6852097153113141210216174111918

Relative Concentration

Pro

bese

t

FoldChange

−15 0 10Value

040

80

Color Keyand Histogram

Cou

nt

12/24

BeamerAdding Sweave

PauseTableSplitting ScreenAnimationLove Letter Too

Love Letter Too

If I speak in the tongues of menor of angels, but do not have love, I am

only a resounding gong or a clanging cymbal. IfI have the gift of prophecy and can fathom all mys-teries and all knowledge, and if I have a faith that canmove mountains, but do not have love, I am nothing.If I give all I possess to the poor and give over my bodyto hardship that I may boast, but do not have love, Igain nothing. Love is patient, love is kind. It doesnot envy, it does not boast, it is not proud. Itdoes not dishonor others, it is not self-seeking,it is not easily angered, it keeps no recordof wrongs. Love does not delight in evil

but rejoices with the truth. It al-ways protects, always trusts,

always hopes, alwaysperseveres.

13/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Literate programming means that text, data, and computer codeare interwoven in a single self-contained document.

14/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

This is not literate programming

A research document involving multiple files with figures and tablescut and paste from various places. For instance,

a stata do-file

an excel spreadsheet with results

an excel spreadsheet with data

a directory with filenames like “old.doc” and “new.doc”

a word document with tables and figures cut and paste fromvarious places

*Changes to the stata do-file are not automatically propogated tothe excel spread-sheet or to the Word document.

15/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Why you should use literate programming

Reproducible research

Dynamic reports

R Package vignettes: R vignettes are usually developed usingSweave

16/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

This presentation

If you have beamer and tex installed, try reproducing this talk:

> Sweave("HoExample4.Rnw")

> Sys.setenv(PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":"))

> texi2pdf("HoExample4", quiet=F)

17/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Literate programming in R using Sweave

Essentially requires a single source document – a ’.Rnw’ file.

*.RnwSweave−−−−→ *.tex

latex−−−−→ *.dvixdvi−−−−→ view of document

see http://www.bias-project.org.uk/Rpackages_course/intro_Sweave.pdf

18/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Syntax using Beamer and Sweave

\documentclass{beamer}\usepackage{beamerthemeshadow}\usepackage{Sweave}\begin{document}\begin{frame}

Content for Slide 1\end{frame}� �=my R code chunk 1@\begin{frame}

Content for Slide 2� �=my R code chunk 2@\end{frame}

\end{document}

19/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Code chunk options

eval(TRUE, or FALSE)

Whether the R chunk is run

echo(TRUE, FALSE)

Whether the R chunk is shown in the LATEX file

results(verbatim, hide, tex)

Type of output used to show the printed results produced bythe R code.

fig(TRUE, FALSE)

Whether the output is a figure. By default, PDF files areproduced.

�figname, fig=TRUE, include=FALSE�

See the HoExample4.Rnw for examples.

20/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Extracting code chunks

To extract code chunks from a .Rnw file:

> Stangle("HoExample4.Rnw")

This command generates the file HoExample4.R containing thecode chunks used in this presentation.

21/24

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Dynamic reports

> require(geneplotter) || {

+ message("package not available. downloading from Bioconductor")

+ source("http://www.bioconductor.org/biocLite.R")

+ biocLite("geneplotter", type="source")

+ }

> x1 <- matrix(rnorm(1e4), ncol=2)

> x2 <- matrix(rnorm(1e4, mean=3, sd=1.5), ncol=2)

> x <- rbind(x1,x2)

> ##I use include=FALSE and then use latex to put the figure exactly where I want

> par(mfrow=c(2,2), las=1)

> par(mar=c(2,2,2,1))

> smoothScatter(x, nrpoints=0)

> smoothScatter(x)

> smoothScatter(x,nrpoints=Inf,colramp=colorRampPalette(RColorBrewer::brewer.pal(9, "YlOrRd")),bandwidth=1)

> colors <- densCols(x)

> plot(x, col=colors, pch=20)

22/24

Interweaving text, R code, and figures

Any changes to the preceding code will be propagated to this figure

automatically.

−4 −2 0 2 4 6 8

−2

0

2

4

6

8

x[,1]

−4 −2 0 2 4 6 8

−2

0

2

4

6

8

x[,1]

x[,2

]

−4 −2 0 2 4 6 8

−2

0

2

4

6

8

●●

●●●

●●

●●

●●

●●

● ●

● ●

● ●

●●

●●

● ●

●●

●●●

● ●

● ●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●●

● ●

●●

●●

●●

● ●

●●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

● ●

● ●

●●●

●●

●●

● ●

●●

● ●

● ●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

● ●

● ●

●●

●●

●●

●●● ●

●●

●●● ●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●●

●●

●●

●●

●●

●● ●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●● ●

●●

●●

● ●

●●

●●

● ●

● ●

●●

●●

● ●

●●

●●

●● ●

●●

●●

● ●

● ●

●●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

● ●●

●●

● ●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

● ●

●●

●●

●●

● ●

●● ●

● ●●●

●●

● ●

● ●

●●

●●

● ●

● ●

●●

●●

●● ●

●●

●●

● ●

●●

●● ●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●●

●●

●●

● ●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

● ●

●●●

●●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

● ●

●●

● ●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

● ●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●●

●●

●●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●

●●

●●●

●●

●●

●●

● ●

●●

●● ●

●●●

●●

●●●

● ●

●●

●●

●●

●●

● ●

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ●

●●

●●●

● ●●

●●

●●

●●

●●

● ●

●●

●●

●●●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●●

● ●●

●●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●●

● ●

●● ● ●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

● ●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●●

●●

● ●●

●●

●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

● ● ●●

● ●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ● ●

●●●

●●

●●

●●

●●

● ●

●●

●●●

● ●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●●

●●

● ●

●●●

●●

●●

●●

●●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●

●●

● ●

● ●

●●●

●●

● ●

● ●

● ●

●●●

● ●●

● ●

●●

●●

●●

●●●

●●

●●

● ●

● ●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

● ●

●●

●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

● ●●

● ●

●●

● ●

●●

● ●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

● ●

● ●

● ●●

● ●

●●●

●●

●●

●●

● ●

● ●

●●●

●●

●●

● ●

●●

● ●

● ●

● ●

●●

●●

●●

● ●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

● ●

●●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●● ●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●●

●●

● ●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●●

● ●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●●

●●

●●

●●●

● ●

●●●

●●

●●

●●

● ●

● ●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●● ●

●●

●●

●●

● ●

● ●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●●

● ●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●● ●

● ●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●●

●●

●●

●●

●●

●● ●

●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●●

● ●

●●

●●

●●

●●

●●

●● ●

● ●

●●

●●

●●

●●

●●●

●●

● ●

●● ●

●●

●●

●●

● ●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●●

●●

● ●

●●

● ●●

●●

●●

● ●

●●

●●

●●

● ●

●● ●

●●

●●

●●

●●

● ●

● ●

●●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●●

●●

● ●

●●

●●

● ●

●●

●●

●● ●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●● ●

● ●

●●

●●

●●

●● ●●

●●

● ●

●●

● ●

●●

●●

●●

● ●

●●

● ●

−4 −2 0 2 4 6 8

−2

0

2

4

6

8

x[,2

]

BeamerAdding Sweave

Literate ProgrammingWhy SweaveHow Sweave worksMaking Nice Plots

Many public resources

Key words for google: reproducible research, Sweave

24/24