\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}

\title{Meu exemplo usando Sweave e Knitr}
\author{Fabio Fajardo}

\begin{document}
\SweaveOpts{concordance=TRUE}
\maketitle

<<bernoulli,echo=TRUE>>=
# Gerador de numeros pseudo-aleatorios
RNGkind("Marsaglia-Multicarry") 
set.seed(c(362436069,521288629))
n<-100
p<-0.5
REP<-5000
#
bernoulli<-function(n,p){
	valores<-function(p){
		x<-ifelse(runif(1)<=p,1,0)
	}
replicate(n,valores(p))
}

# Forma 1
media1<-media2<-rep(0,REP)
system.time({for(i in 1:REP){
	media1[i]<-mean(bernoulli(n,p))
}
summary(media1)}
)

#Forma 2
system.time({media2<-apply(replicate(REP,bernoulli(n,p)),2,mean)
summary(media2)}
)

<<figura1,echo=false,fig=true>>=
par(mfrow=c(1,2))
hist(media2,main="Distribuição empírica das médias", prob=TRUE, ylim=c(0,8.5), breaks=20,col="yellow3")
curve(dnorm(x, mean(media2), sd(media2)), add=TRUE, col="darkblue", lwd=2)
car::qqPlot(media2)
@

<<binomial,echo=TRUE,fig=false>>=
#-----------------------------------------
# Geração de valores de uma dist. Binomial
#-----------------------------------------
N<-2
binomial<-function(n,N,p){
	valores<-function(N,p){
		x<-sum(runif(N)<p)
	}
replicate(n,valores(N,p))
}
media3<-rep(0,REP)
media3<-apply(replicate(REP,binomial(n,N,p)),2,mean)
summary(media3)
@

<<figura2,echo=false,fig=true>>=
par(mfrow=c(1,2))
hist(media3,main="Distribuição empírica das médias", prob=TRUE, ylim=c(0,6.5), breaks=20,col="yellow3")
curve(dnorm(x, mean(media3), sd(media3)), add=TRUE, col="darkblue", lwd=2)
car::qqPlot(media3)
@

Here is a linear regression example:
<<nome,echo=TRUE,fig=true>>=
library(car)
data(Prestige)
attach(Prestige)
qqPlot(income)
set.seed(123)
n<-100; x<-rnorm(n,sd=2)
y<-1-0.5*x+rnorm(n)
fit<-lm(y~x); summary(fit)

<<FitLinearModel,cache=true>>=
library(datasets)
library(stats)
data(airquality)
fit <- lm(Ozone ~ Temp + Solar.R + Wind, data = airquality)
@

<<LinearModelTable,results=tex>>=
library(xtable)
print(xtable(fit,caption="Tabela de resultados"))
@

\begin{figure}
\centering
<<LinearModelDiagnosticPlot,fig=true,eps=true>>=
par(mfcol = c(2, 2))
plot(fit)
@
\caption{Linear model diagnostic plots}
\end{figure}
<<>>=
getwd()
url<-"http://www.leg.ufpr.br/~paulojus/dados/gam01.txt"
download.file(url, "file.txt" )
@

Sweave embeds the results!
\end{document}