当前位置: 首页 > 工具软件 > knitr > 使用案例 >

knitr生成文档时报错: “trying to use CRAN without setting a mirror“

通啸
2023-12-01

下载包后:

# Define the variable pkgs that contatins all the required packages
pkgs <- c("readr", "readxl", "haven", "dplyr", "tidyr", "VIM")

# Installing packages
install.packages(pkgs)

# loading package
library(dplyr)
library(readr)

在kint生成html文档时报错:

Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: … eval -> eval -> install.packages -> grep -> contrib.url
Execution halted

原因:Knitr生成一个R会话,没有默认的cran镜像,除非特别要求.当使用Rstudio时,我需要 for each R会话设置CRAN。

将镜像指定为install.packages参数:

# Installing packages
install.packages(pkgs, repos = "http://cran.us.r-project.org")

或者在.Rprofile中设置默认的CRAN mirror.

if(!require(pkgs)) install.packages(pkgs, repos = "http://cran.us.r-project.org")
 类似资料: