【问题标题】:Beginner's questions (figures, bibliography) with Sweave/R/LaTeX---my first documentSweave/R/LaTeX 的初学者问题(数字、参考书目)---我的第一个文档
【发布时间】:2011-12-01 06:49:02
【问题描述】:

我只是从SweaveR 开始。在这里,我使用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 文件。怎么编译?

非常感谢...

【问题讨论】:

  • 您可以像在任何 LaTeX 文档中一样进行参考书目。请记住,在 Sweave 块之间,它“只是”LaTeX 代码。
  • @Roman 谢谢。这对初学者来说是件好事。 1 票赞成

标签: r latex sweave figure


【解决方案1】:

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

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

【讨论】:

  • @Dieter 谢谢。该代码有效。为什么要求从日期文件中删除所有 cmets? 1 票赞成。
  • 因为我认为 # 在数据文件中不起作用。以前从未这样做过,并且错过了文档中的 comment.char。永远不要相信假设......
  • Sweaving 将添加\usepackage{Sweave} 行,您不必这样做。
【解决方案2】:

Sweave 不处理您的参考书目,因此您必须自己编译;我相信有些人已经在他们的 R 脚本中自动化了这项工作,但如果你是一个有经验的 LaTeX 用户,我建议你使用 LyX。 LyX 对 Sweave 有官方支持,它可以智能地处理你在 LaTeX 中想要的一切(包括参考书目)。见手册:https://github.com/downloads/yihui/lyx/sweave.pdf

【讨论】:

  • 好的。这样我就可以像往常一样使用BibTeX来编译文档了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 2010-10-24
相关资源
最近更新 更多