R Sweave - 文本和绘图将打印机打印到两个不同的文件
R Sweave - text and plots get printet to two different files
我想开始使用 Sweave(我对 LaTeX 非常熟悉,尤其是在学生时代)。
为了了解如何使用它,我正在浏览 this intro。
我基本上是将他们的示例文件(见下文)复制到 Rstudio 中并单击 "Compile 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}
我找到了解决办法!我不知道他们为什么在介绍中省略了这一点,但无论如何添加 "fig = TRUE" 语句就可以了。
例如在第一个块中:
<<>>=
data(rock)
require(ggplot2)
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
print(plot1)
@
我所要做的就是添加语句:
<<fig = TRUE>>=
data(rock)
require(ggplot2)
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
print(plot1)
@
现在所有内容都打印到一个文件中。
我想开始使用 Sweave(我对 LaTeX 非常熟悉,尤其是在学生时代)。
为了了解如何使用它,我正在浏览 this intro。
我基本上是将他们的示例文件(见下文)复制到 Rstudio 中并单击 "Compile 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}
我找到了解决办法!我不知道他们为什么在介绍中省略了这一点,但无论如何添加 "fig = TRUE" 语句就可以了。
例如在第一个块中:
<<>>=
data(rock)
require(ggplot2)
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
print(plot1)
@
我所要做的就是添加语句:
<<fig = TRUE>>=
data(rock)
require(ggplot2)
plot1 = qplot(x=area, y=log(perm), data=rock) + theme_bw() + geom_smooth(col="red")
print(plot1)
@
现在所有内容都打印到一个文件中。