1

我只是从Sweave和开始R。在这里,我R用来输出一些数据,我也试图包括一个情节。代码不摆动。我有一个Sweave来自网络的例子,它可以很好地RStudioLaTeX.

\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}

并且文件 apples.d 包含:

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

我究竟做错了什么?

其他相关问题:

文件是否Sweave支持普通LaTeX bibliography文件。如何进行编译?

非常感谢...

4

2 回答 2

6

修正了几个问题,用 %%%% 或 #### 标记

\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}
于 2011-12-01T07:08:41.007 回答
2

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

于 2011-12-01T15:50:35.900 回答