Cannnot Knit R Markdown

I am not able to knit my markdown. When I try to, I get this Error:

Line 9 Error in contrib.url(repos, "source") : trying to use CRAN without 
setting a mirror Calls: <Anonymous> ... with Visible -> eval -> 
install.packages -> contrib.url Execution halted

Currently, all I have in my markdown is:

8   ```{r import data}
9   install.packages("readxl")
10  library(readxl)
11  passengers <- read_excel("titanic.xlsx")
12  ```

Thank you,
Anisha

My suggestion is to not write code that includes the installation of a package. That action only needs to be done once on your system, so including it in the markdown document results in needlessly executing it many times or writing flow control code to prevent that. If others will use the code, it is not polite to install a package on their system. Just run

install.packages("readxl")

in the console or use RStudio's menu Tools -> Install Packages.

I think the cause of the error is that the knitting of a document occurs in an environment that does not have access to all of the settings in your normal environment and the install.packages() function cannot find the on line repository. I may be wrong about that.

Francis

1 Like

If you want to keep the install command for future reference comment it with a "#" that way doesn't get executed, also a good practice is putting all your library calls into a "setup" chunk at the beginning of your .Rmd file, something like this

```{r setup, include=FALSE}
#install.packages("readxl")
library(readxl)
```
```{r import data}
passengers <- read_excel("titanic.xlsx")
```
4 Likes

Thank you for your help! I was able to get it to knit to word, but for some reason it does not want to knit to PDF. This is the message that I am getting:

output file: Titanic.knit.md

Error: Failed to compile Titanic.tex.
In addition: Warning message:
In system2(..., stdout = FALSE, stderr = FALSE) : error in running command
Execution halted

No TeX installation detected (TeX is required to create PDF output). You should install a recommended TeX distribution for your platform:

  Windows: MiKTeX (Complete) - http://miktex.org/2.9/setup
  (NOTE: Be sure to download the Complete rather than Basic installation)

  Mac OS X: TexLive 2013 (Full) - http://tug.org/mactex/
  (NOTE: Download with Safari rather than Chrome _strongly_ recommended)

  Linux: Use system package manager

I tried following these instructions, but still only the Word document works for me when I knit.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.