Error: could not find function "chooseCRANmirror"

Error: could not find function "chooseCRANmirror"

我在 macOS Catalina 上通过终端使用 R。我 运行 遇到安装库的问题,例如

> install.packages("caret")
Warning: unable to access index for repository http://cran.revolutionanalytics.com/src/contrib:
  cannot open URL 'http://cran.revolutionanalytics.com/src/contrib/PACKAGES'
Warning: unable to access index for repository http://cran.revolutionanalytics.com/bin/macosx/el-capitan/contrib/3.6:
  cannot open URL 'http://cran.revolutionanalytics.com/bin/macosx/el-capitan/contrib/3.6/PACKAGES'
Warning message:
package ‘caret’ is not available (for R version 3.6.0) 

这个问题可以通过以下方式解决:

chooseCRANmirror(12)

我将该行添加到我的 ~/.Rprofile 以便它在每个会话开始时自动加载它。但是,returns:

Error: could not find function "chooseCRANmirror" [Previously saved workspace restored]

,并且不加载 R 配置文件,即使当我将它直接放入我的控制台时它仍然有效。为什么是这样?是否有解决方案可以自动选择 CRANmirror(12) 而无需在每个会话中手动执行?

这是我剩下的 ~/.Rprofile:

local({r <- getOption("repos")
      r["CRAN"] <- "http://cran.revolutionanalytics.com"
      options(repos=r)})
chooseCRANmirror(12)

options(stringsAsFactors=FALSE)
 
options(max.print=100)
 
options(scipen=10)
 
options(editor="vim")
 
# options(show.signif.stars=FALSE)
 
options(menu.graphics=FALSE)
 
options(prompt="> ")
options(continue="... ")
 
options(width = 80)
 
q <- function (save="no", ...) {
  quit(save=save, ...)
}
 
utils::rc.settings(ipck=TRUE)
 
.First <- function(){
  if(interactive()){
    library(utils)
    timestamp(,prefix=paste("##------ [",getwd(),"] ",sep=""))
 
  }
}
 
.Last <- function(){
  if(interactive()){
    hist_file <- Sys.getenv("R_HISTFILE")
    if(hist_file=="") hist_file <- "~/.RHistory"
    savehistory(hist_file)
  }
}
 
if(Sys.getenv("TERM") == "xterm-256color")
  library("colorout")
 
sshhh <- function(a.package){
  suppressWarnings(suppressPackageStartupMessages(
    library(a.package, character.only=TRUE)))
}
 
auto.loads = c("viridis", "tidyverse", "data.table")
 
if(interactive()){
  invisible(sapply(auto.loads, sshhh))
}
 
.env <- new.env()
attach(.env)
 
.env$unrowname <- function(x) {
  rownames(x) <- NULL
  x
}
 
.env$unfactor <- function(df){
  id <- sapply(df, is.factor)
  df[id] <- lapply(df[id], as.character)
  df
}

您需要指定包。

utils::chooseCRANmirror(12)

.Rprofile 只加载基础包。参见 ?Startup

Note that when the site and user profile files are sourced only the base package is loaded, so objects in other packages need to be referred to by e.g. utils::dump.frames or after explicitly loading the package concerned.