简体   繁体   中英

CRAN mirror in Rmarkdown

I am very new to Rmarkdown, and I am having problems with setting up libraries that I will use later in my document.

My .Rmd file:

# Rmarkdown for tree
#### 
#### 

### load packages
```{r}
library(ctv)
install.views('Phylogenetics')
update.views('Phylogenetics')
library(ape)
library(adegenet)
library(phangorn)
```

The error message that I will get is

error in available.views(repos = repos) : trying to use CRAN without setting a mirror Calls: 
<Anonymous> ... install.views -> .get_pkgs_from_ctv_or_repos -> available.views

How do I install packages successfully so that my downstream analyses will work?

Thanks!

This solution is a combination of two elements. The first, do not install packages through code chunks in R Markdown without first checking if they are installed.

You can define your mirror programmatically using @Frank 's answer.

Your setup chunk would look like this (repo and packages are just for illustration, change accordingly):

```{r setup, include=FALSE, echo=FALSE}
r <- getOption("repos")
r["CRAN"] <- "http://cran.cnr.berkeley.edu/"
options(repos = r)

if(!require(gridExtra)){
  install.packages("gridExtra")
}

if(!require(autocrop)){
  devtools::install_github("jhollist/autocrop")
}

library("gridExtra")
library("autocrop")

```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM