「r<-配置」Rprofile:R 全局设置

与文章 【r<-安装|方案】Windows R安装与配置 相关。

存储在 ~/.Rprofile 中的内容是一个 R 脚本,它会在 R 启动时自动运行,所以可以作为全局的配置使用。

根据我的 R 使用经验,结合 Y 叔和 Yihui 提供的一些便利函数,我构建了一个比较合适的 R 配置。推荐大家结合自己需求使用和自定义。

目前设定涉及的功能:

  1. 设置 UTF-8 编码。
  2. 设置 R 提示用英文显示,方便查看和搜索 R 代码错误信息。
  3. 设置 CRAN 下载源为同济大学;Bioconductor 下载源为清华大学。
  4. 设定自定义 R 包存储路径,这个非常有用,便于以后 R 版本涉及不影响 R 包。
  5. 设置 R 临时文件目录,这个在一些场景下也有用(比如快速查看临时文件)。
  6. 设置载入 pacman 用来管理 R 包。
  7. 设置 VSCode r 插件需要进行的设置。
  8. 提供一些工具函数,目前主要来自 Y 叔的 rvcheck 包和 Yihui 的 xfun 包。

该脚本配置我会长期维护博客地址)。如果大家有问题或建议、增加新的工具函数,欢迎通过邮件或 https://github.com/ShixiangWang/MessageBoard/issues 进行反馈和交流。

#------------------------------------------------------------
#                  |R Global Setting|
#
# You can (un)comment any code you dislike.
#   Any Question, please email to
#       Shixiang Wang <w_shixiang@163.com>
#   or file an issue to
#       <https://github.com/ShixiangWang/MessageBoard/issues>
#------------------------------------------------------------


# Global options ----------------------------------------------------------
options(encoding = "UTF-8") # Set file encoding
Sys.setenv("LANGUAGE" = "EN") # Set language of R message

# Package download mirrors ------------------------------------------------
## For Bioconductor packages
options(BioC_mirror = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
## For CRAN packages
## Full list see mirrors on <https://cran.r-project.org/>
options("repos" = c(CRAN = "https://mirrors.tongji.edu.cn/CRAN/"))

# Set local custom R library path -----------------------------------------
.CUSTOM_LIB <- "~/Library/R" # Set your custom library location
# Please do not add '/' at the end !!!

if (!dir.exists(.CUSTOM_LIB)) {
  dir.create(.CUSTOM_LIB, recursive = TRUE)
}

.libPaths(c(.CUSTOM_LIB, .libPaths()))
message("Using library: ", .libPaths()[1])

# Set R temp directory ----------------------------------------------------
## Uncomment the following code if you want to set R temp directory
# .TMP = "~/Rtmp"
# if(dirname(tempdir()) != .TMP){
#   if(!dir.exists(.TMP)) dir.create(.TMP)
#   cat(paste0("TMPDIR = ", .TMP), file="~/.Renviron", sep = "\n")
# }
# message("Using temp directory: ", .TMP)


# Use pacman to manage R packages -----------------------------------------
# Reference: <https://www.jianshu.com/p/cb16ded75672>
if (!requireNamespace("pacman", quietly = TRUE)) {
  install.packages("pacman", dependencies = TRUE)
}

library(pacman)

# VSCode plugin setting ---------------------------------------------------
# Only use when you code R with VSCode
# Reference: <https://github.com/REditorSupport/vscode-r-lsp>
if (!requireNamespace("languageserver", quietly = TRUE)) {
  pacman::p_install(languageserver)
}

source(file.path(
  if (.Platform$OS.type == "windows") {
    file.path(Sys.getenv("HOMEDRIVE"), Sys.getenv("HOMEPATH"))
  } else {
    Sys.getenv("HOME")
  }, ".vscode-R", "init.R"
))

# Global utilities --------------------------------------------------------

## Try installing R packages again and again
## It may be useful when install GitHub R packages
.loop_install <- function(pkgs, force = FALSE) {
  ## If force=TRUE,
  ## force installation when packages already exist on local system.
  Sys.sleep(1)
  tryCatch(
    {
      message("=> Try installing ", paste(pkgs, collapse = ", "))
      gh_pkg <- pkgs[grepl("/", pkgs)]
      ot_pkg <- setdiff(pkgs, gh_pkg)
      if (length(ot_pkg) > 0) {
        pacman::p_install(ot_pkg, character.only = TRUE, force = force)
      } else {
        if (!requireNamespace("remotes", quietly = TRUE)) {
          pacman::p_install(remotes)
        }
        remotes::install_github(gh_pkg, force = force)
      }
    },
    error = function(e) {
      .loop_install(pkgs, force)
    }
  )
}

## Utilities from rvcheck, xfun and other R package
## Thanks to the authors
if (!requireNamespace("rvcheck", quietly = TRUE)) {
  pacman::p_install(rvcheck)
}
if (!requireNamespace("xfun", quietly = TRUE)) {
  pacman::p_install(xfun)
}

## Load function from package
.get_fun <- rvcheck::get_fun_from_pkg
## Open any directory in any operating system
.open <- rvcheck::o
## Check whether packages were installed
.is_installed <- rvcheck::is.installed
## Check if on a R server
.is_rserver <- suppressMessages(.get_fun("rvcheck", "is.rserver"))
## Simpler function to download file
.download_file <- xfun::download_file
.upload_ftp <- xfun::upload_ftp
## Functions to obtain, remove, and change extensions in filenames
.fl_ext <- xfun::file_ext
.rm_ext <- xfun::sans_ext
.ch_ext <- xfun::with_ext
## Provide the "file" version of gsub(),
## i.e., they perform searching and replacement in files via gsub().
.gsub_file <- xfun::gsub_file
.gsub_files <- xfun::gsub_files
.gsub_dir <- xfun::gsub_dir
.gsub_ext <- xfun::gsub_ext
## Change the working directory, evaluate the expression,
## and restore the working directory
.move_run <- xfun::in_dir
## Install a source package from a directory
.install_dir <- xfun::install_dir
## Check OS
.is_linux <- xfun::is_linux
.is_windows <- xfun::is_windows
.is_macos <- xfun::is_macos
.is_unix <- xfun::is_unix
## Number to words, e.g. 1 to one, 10 to ten
.numbers_to_words <- xfun::numbers_to_words
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269