domande per principianti (figure, bibliografia) con Sweave / R / LaTeX --- il mio primo documento
Domanda
Io sono appena agli inizi con Sweave
e con R
. Qui sto usando R
di uscita alcuni dati e sto anche cercando di includere un complotto. Il codice non sweave. Ho un esempio Sweave
dal web che raccoglie bene in RStudio
con LaTeX
.
\documentclass[a4paper]{article}
\begin{document}
<<echo=TRUE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
<<echo=FALSE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
<<echo=TRUE>>=
test.frame<-read.table(file="apples.d",header=T,sep= "")
names(test.frame)
head(test.frame)
class(test.frame)
@
\begin{figure}[htbp]
\begin{center}
\setkeys{Gin}{width=0.5\textwidth}
<<echo=FALSE,fig=TRUE,width=4,height=4>>=
plot(year,value)
@
\end{center}
\end{document}
e il file apples.d contiene:
#Number of apples I ate
year value
8 12050 #year 2008
9 15292 #year 2009
10 23907 #year 2010
11 33997 #year 2011
Che cosa sto facendo di sbagliato?
Altra domanda relativa:
Fa un file normale Sweave
LaTeX
supporto documenti bibliography
. Come fare la compilazione?
Grazie mille ...
Soluzione
Several problems corrected, marked by %%%% or ####
\documentclass[a4paper]{article}
\begin{document}
<<echo=TRUE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
<<echo=FALSE>>=
x <- rnorm(100)
xm <- mean(x)
xm
@
<<echo=TRUE>>=
##### Remove all comments from your data file
test.frame<-read.table(file="apples.d",header=T,sep= "")
names(test.frame)
head(test.frame)
class(test.frame)
@
\begin{figure}[htbp]
\begin{center}
\setkeys{Gin}{width=0.5\textwidth}
<<echo=FALSE,fig=TRUE,width=4,height=4>>=
#### Must tell plot where to get the data from. Could also use test.frame$year
with(test.frame,plot(year,value))
@
\end{center}
\end{figure}
\end{document}
Altri suggerimenti
Sweave does not take care of your bibliography, so you have to compile it by yourself; I believe some people have automated this job in their R scripts, but I would recommend you to use LyX if you are an experienced LaTeX user. LyX has official support to Sweave, and it takes care of everything you want in LaTeX intelligently (including bibliography). See the manual: https://github.com/downloads/yihui/lyx/sweave.pdf