• 検索結果がありません。

Introduction to Sweave Rのドキュメント環境について(R) script of

N/A
N/A
Protected

Academic year: 2018

シェア "Introduction to Sweave Rのドキュメント環境について(R) script of"

Copied!
11
0
0

読み込み中.... (全文を見る)

全文

(1)

Introduction to Sweave

Virgilio G´omez-Rubio

Department of Epidemiology and Public Heath Imperial College London

London, UK

(2)

What is Sweave?

◮ Created by Friedrich Leisch (member of R Core Team) ◮ Main site:

http://www.ci.tuwien.ac.at/~leisch/Sweave/

◮ Sweave provides a way of interfacing R code with LATEX

documents

◮ A version for Open Document Format (ODF) documents (for

(3)

Basics of Sweave and literate programming

◮ Literate programming is a philosophy of computer

programming based on the premise that a computer program should be written similar to literature, with human readability as a primary goal. (Wikipedia, 14 Nov. 2007)

◮ Sweave is based on Cweb, developed by Donald Knuth for the

C programming language

◮ The aim is to provide high quality documentation by mixing

text and programming code

◮ There are two main tools to handle these documents:

◮ Sweave

Produces suitable documentation from the main source files ◮ Stangle

(4)

Building a Rnw document

◮ Main structure is that of a LATEX document

◮ LATEX package Sweavemust be included in the list odf

packages loaded

◮ R code can be included as follows:

Rnw file <<>>= a<-1 b<-4 print(a+b) @

LATEX code

\begin{Schunk} \begin{Sinput} > a <- 1 > b <- 4 > print(a + b) \end{Sinput} \begin{Soutput} [1] 5 \end{Soutput} \end{Schunk} DVI document

> a <- 1 > b <- 4

> print(a + b)

(5)

Processing Rnw files

Rnw file Sweave.sh TeX file pdflatex PDF file

◮ A script (Sweave.sh) can be used to Sweave documents from

the command line:

#!/bin/sh

echo "library(\"utils\"); Sweave(\"$1\")" | R --no-save --no-restore

(6)

Extracting the code from the chunks

Stangle provides a way of extracting the code in the chunks ◮ Stangle("myfile.Rnw") will create a file called myfile.Rwith

the R chunks

◮ Each chunk is identified with a number using R comments ◮ Useful to identify possible problems when compiling the Rnw

file

◮ A script (Stangle.sh) can be used to Stangle documents from

the command line:

#!/bin/sh

echo "library(\"utils\"); Stangle(\"$1\")" | R --no-save --no-restore

(7)

Chunk options

Several options can be placed in<< >>=to control how the code in the chunks is executed.

eval (TRUE, FALSE)

Whether the R chunk is run.

◮ echo (TRUE, FALSE)

Whether the R chunk is shown in the LATEX file.

◮ results (verbatim, tex, hide)

(8)

In-line chunks

◮ \Sexpr{Rcode} allows for a limitedin-line computation and

output

◮ Rnw code

$e^{2\pi}$ equals \Sexpr{formatC(exp(2*pi), 5)}

◮ LATEX code

$e^{2\pi}$ equals 535.49

DVI file

e2π

(9)

Plots in Sweave

Chunk options

fig(FALSE, TRUE)

Whether the output is a figure. By default, EPSandPDFfiles are produced.

◮ width

Width of the plot

◮ height

Height of the plot

Hints

◮ The Sweave code can be included within a LATEX figure

environment

Trellis graphics must be wrapped within aprint call:

(10)

Some issues on creating Vignettes

◮ R vignettes are usually developed usig Sweave

◮ They are placed in thepkg_name/inst/docdirectory ◮ A vignette index entry must be included in the header of the

Rnw file.

\VignetteIndexEntry{MCMC Example}

◮ WhenR CMD check is run on the package source, the R

(11)

Resources

◮ Sweave main page

http://www.ci.tuwien.ac.at/~leisch/Sweave/

◮ Charlie Geyer’s demo

http://www.stat.umn.edu/~charlie/Sweave/

◮ Literate programming (D. Knuth’s web site)

http://www-cs-faculty.stanford.edu/~knuth/lp.html

◮ Literate Programming

参照

関連したドキュメント

Abstract: In this paper, we investigate the uniqueness problems of meromorphic functions that share a small function with its differential polynomials, and give some results which

Now it makes sense to ask if the curve x(s) has a tangent at the limit point x 0 ; this is exactly the formulation of the gradient conjecture in the Riemannian case.. By the

[1] Feireisl E., Petzeltov´ a H., Convergence to a ground state as a threshold phenomenon in nonlinear parabolic equations, Differential Integral Equations 10 (1997), 181–196..

Then, the existence and uniform boundedness of global solutions and stability of the equilibrium points for the model of weakly coupled reaction- diffusion type are discussed..

The damped eigen- functions are either whispering modes (see Figure 6(a)) or they are oriented towards the damping region as in Figure 6(c), whereas the undamped eigenfunctions

“Breuil-M´ezard conjecture and modularity lifting for potentially semistable deformations after

This paper gives a decomposition of the characteristic polynomial of the adjacency matrix of the tree T (d, k, r) , obtained by attaching copies of B(d, k) to the vertices of

The advection-diffusion equation approximation to the dispersion in the pipe has generated a considera- bly more ill-posed inverse problem than the corre- sponding