情况是这样,我有一段分析线性回归的小程序,它需要读取一些硬盘上的数据,在RStudio中能够正常运行,而且在Sweave文件中测试别的简单代码(不含读文件)也能正常运行。RStudio 版本0.97.511,操作系统版本MacOSX10.7.5,texwork版本Version 0.4.4 r.1004 (release)
问题代码
\documentclass{article}
\title{A Test Sweave Document}
\author{Yihui Xie}
\usepackage{Sweave}
\begin{document}
\SweaveOpts{concordance=TRUE}
\input{Test_R2-concordance}
\maketitle
We can take a look at some random numbers from N(0, 1):
\begin{Schunk}
\begin{Sinput}
> x = rnorm(5)
> x
\end{Sinput}
\begin{Soutput}
[1] -1.6554026 -0.6548836 -1.1055773 -0.6931843 0.9935209
\end{Soutput}
\end{Schunk}
The 3rd number is -1.10557732386475.
Draw a plot for the iris data:
\begin{Schunk}
\begin{Sinput}
> setwd("/Users/jtl/Dropbox/Regression_Programing/Data")
> H <- read.csv("hardness.txt")
> head(H, 3)
\end{Sinput}
\begin{Soutput}
Run Temp Hard
1 1 30 55.8
2 2 30 59.1
3 3 30 54.8
\end{Soutput}
\begin{Sinput}
> plot(iris[, 1:2])
> x
\end{Sinput}
\begin{Soutput}
[1] -1.6554026 -0.6548836 -1.1055773 -0.6931843 0.9935209
\end{Soutput}
\end{Schunk}
\includegraphics{Test_R2-test2}
\end{document}
系统提示:
During startup - Warning messages:
1: Setting LC_CTYPE failed, using "C"
2: Setting LC_COLLATE failed, using "C"
3: Setting LC_TIME failed, using "C"
4: Setting LC_MESSAGES failed, using "C"
5: Setting LC_PAPER failed, using "C"
> grDevices::pdf.options(useDingbats = FALSE); utils::Sweave('test_Rcode.Rnw', encoding='ASCII')
Writing to file test_Rcode.tex
Processing code chunks with options ...
You can now run (pdf)latex on 'test_Rcode.tex'
>
>
Running pdflatex on test_Rcode.tex...failed
Issues: 2 errors
然后尝试运行test_Rcode.tex,得到提示
! LaTeX Error: File `Test_R2-concordance.tex' not found.
Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)
Enter file name:
以下是我想执行的代码(R):
#Source: Hardness-calculationsForSimpleLSE.R
#s
setwd("/Users/jtl/Dropbox/Regression_Programing/Data")
H <- read.csv("hardness.txt")
head(H, 3)
x <- H$Temp
y <- H$Hard
xc <- x-mean(x)
yc <- y-mean(y)
sxx <- sum(xc^2)
sxy <- sum(xc*yc)
b1 <- sxy/sxx
b0 <- mean(y)-b1*mean(x)
c(b0, b1)
#
#plot data and fitted line
plot(x, y, xlab="hardness", ylab="temperature")
abline(a=b0, b=b1, col="orange")
#this works too,
#lines(x, yHat, col="orange")
#box-whisker plot of residuals
yHat <- b0 + b1*x
res <- y-yHat
boxplot(res)
#normal probability plot of residuals
qqnorm(res)
qqline(res, col="red")
#
#recommended way to fit regression in R
ans <- lm(y~x)
ans
summary(ans)
resid(ans)
fitted(ans)
而且下述代码能够正常执行,图像也能正常输出
\documentclass{article}
\title{A Test Sweave Document}
\author{Yihui Xie}
\SweaveOpts{pdf=TRUE, eps=FALSE}
\begin{document}
\SweaveOpts{concordance=TRUE}
\maketitle
We can take a look at some random numbers from N(0, 1):
<<>>=
x = rnorm(5)
x
@
The 3rd number is \Sexpr{x[3]}.
Draw a plot for the iris data:
<<test2, fig=TRUE>>=
plot(iris[, 1:2])
x
@
\end{document}