Error when compiling pdf using knitr in rstudio

2019-02-09 05:57发布

问题:

I have installed the packages Sweave as well as knitr on both Linux mint and Windows 7, and it works well on Windows, but fails when using rstudio in Linux. After installing the pkgs, I write a simple dw.Rnw as follows:

\documentclass{article}

\begin{document}

The regression model is as follows
<<echo=TRUE>>=
pop=read.table("pop.txt",header=TRUE)
attach(pop)

lm.sol=lm(y~year)
summary(lm.sol)
@


\end{document}

When I press the button 'Compile PDF' in rstudio, it returns the following error messages:

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" 
6: Setting LC_PAPER failed, using "C" 
7: Setting LC_MEASUREMENT failed, using "C" 
> grDevices::pdf.options(useDingbats = FALSE); require(knitr); knit('dw.Rnw', encoding='UTF-8')
Loading required package: knitr
Warning in readLines(if (is.character(input2)) { :
  cannot open file 'dw.Rnw': No such file or directory
Error in readLines(if (is.character(input2)) { : 
  cannot open the connection
Calls: knit -> readLines
Execution halted

It seems there is something wrong with the function knit. However, if I type

knit("dw.Rnw")

in the console of rstudio, then it successfully generates the file dw.tex, which I can compile with pdflatex and finally generate dw.pdf.

So what's wrong with my knitr or rstudio?

回答1:

Although I still do not understand why, but from the comments above, the problem seems to come from ~/.Rprofile (in particular, setwd()). Clearing it up solves the problem.

Note you may have other startup profile files. .Rprofile is only one possibility. See ?Startup for more information. For example, if you are on Windows, you may need to take a look at C:\Program Files\R\etc\Rprofile.site if it exists.