Beginner's questions (figures, bibliography) with Sweave/R/LaTeX---my first document

I am just starting with Sweave and with R. Here I am using R to output some data and I am also trying to include a plot. The code does not sweave. I have one Sweave example from the web that compiles well in RStudio with 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}

and the file apples.d contains:

#Number of apples I ate
year value
8   12050  #year 2008  
9   15292  #year 2009 
10  23907  #year 2010 
11  33997  #year 2011

What am I doing wrong?

Other related question:

Does a Sweave document support normal LaTeX bibliography file. How to do the compilation?

Thanks a lot...

Answers


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}

Need Your Help

JDBCTemplate returns data when debugging but not during normal operation

java spring jdbctemplate

Inside a web application, Spring's JdbcTemplate returns an empty list for some simple sql (SELECT * FROM view_name -- view_name is a view of one column) where there is known data.

XUL Runner Log HTTP Requests Python

python http xul xulrunner

Looking for some help to log HTTP requests for a given page.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.