2015-02-06 11 views
0

我想開始使用Sweave(我對LaTeX非常熟悉,特別是形成我作爲學生的日子)。R Sweave - 文本和圖表獲得printet到兩個不同的文件

試圖瞭解如何使用它,我要通過this intro

我基本上將他們的示例文件(見下文)複製到Rstudio中,然後單擊「編譯PDF」。

但是會發生什麼情況是帶有R等測試輸出的文本會將printet轉換爲與我的.Rnw文件具有相同名稱的PDF,而這些繪圖會將printet轉換爲另一個名爲Rplots.pdf的文件。

如何讓它將圖和文本打印到同一文件?如果我錯過了一些明顯的事情,我提前道歉,這是我第一次在今天使用Sweave。

testSweave.Rnw:

\documentclass{article} 

\usepackage{graphicx, verbatim} 
\setlength{\textwidth}{6.5in} 
\setlength{\textheight}{9in} 
\setlength{\oddsidemargin}{0in} 
\setlength{\evensidemargin}{0in} 
\setlength{\topmargin}{-1.5cm} 

\begin{document} 
\SweaveOpts{concordance=TRUE} 
\begin{center} 
{\bf \Large Stat 500 Assignment 1\\} 
\end{center} 

Using the rock data in the datasets package. First we do some plots:\\ 
\setkeys{Gin}{width=.3\linewidth} 
<<>>= 
data(rock) 
require(ggplot2) 
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red") 
print(plot1) 
@ 
%% lattice and ggplots must be inside a print statement to show up 
<<>>= 
print(plot1 + aes(x = peri)) 
@ 
<<>>= 
print(plot1 + aes(x = shape)) 
@ 

Now go back and remove the \%\% comments on the line 15 above here 
to set each plot width to .3 
times linewidth, and the three plots should fit on one line. If you leave a  blank line between the three code chunks above, they will start on new lines. 

Summary of the linear model: 
<<>>= 
    rock.lmfit <- lm(log(perm) ~ ., rock) 
    summary(rock.lmfit) 
@ 

<<>>= 
xtable::xtable(summary(rock.lmfit)$coef, digits=5) 
@ 
<<>>= 
rock.rlmfit = MASS::rlm(log(perm) ~ ., rock) 
xtable::xtable(summary(rock.rlmfit)$coef, digits = 4) 
    ## assumes that you have the xtable package available. It creates latex  tables. 


#To print any R object use a \verb|\Sexpr{any_R_object}| statement like this: 
#AIC of the linear model is \Sexpr{AIC(rock.lmfit)}. 
@ 

\end{document} 

回答

0

我找到了解決辦法!我不知道爲什麼他們在介紹中忽略了這一點,但無論如何,添加一個「fig = TRUE」的說法都有效。

E.g.第一大塊:

<<>>= 
data(rock) 
require(ggplot2) 
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red") 
print(plot1) 
@ 

我所要做的就是添加stament:

<<fig = TRUE>>= 
data(rock) 
require(ggplot2) 
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red") 
print(plot1) 
@ 

現在,這一切被打印到一個文件。

相關問題