「r<-配置」Rprofile:R 全局設(shè)置

與文章 【r<-安裝|方案】Windows R安裝與配置 相關(guān)。

存儲在 ~/.Rprofile 中的內(nèi)容是一個 R 腳本,它會在 R 啟動時自動運(yùn)行夏哭,所以可以作為全局的配置使用。

根據(jù)我的 R 使用經(jīng)驗,結(jié)合 Y 叔和 Yihui 提供的一些便利函數(shù)又兵,我構(gòu)建了一個比較合適的 R 配置。推薦大家結(jié)合自己需求使用和自定義卒废。

目前設(shè)定涉及的功能:

  1. 設(shè)置 UTF-8 編碼沛厨。
  2. 設(shè)置 R 提示用英文顯示乘盼,方便查看和搜索 R 代碼錯誤信息。
  3. 設(shè)置 CRAN 下載源為同濟(jì)大學(xué)俄烁;Bioconductor 下載源為清華大學(xué)绸栅。
  4. 設(shè)定自定義 R 包存儲路徑,這個非常有用页屠,便于以后 R 版本涉及不影響 R 包粹胯。
  5. 設(shè)置 R 臨時文件目錄,這個在一些場景下也有用(比如快速查看臨時文件)辰企。
  6. 設(shè)置載入 pacman 用來管理 R 包风纠。
  7. 設(shè)置 VSCode r 插件需要進(jìn)行的設(shè)置。
  8. 提供一些工具函數(shù)牢贸,目前主要來自 Y 叔的 rvcheck 包和 Yihui 的 xfun 包竹观。

該腳本配置我會長期維護(hù)博客地址)。如果大家有問題或建議潜索、增加新的工具函數(shù)臭增,歡迎通過郵件或 https://github.com/ShixiangWang/MessageBoard/issues 進(jìn)行反饋和交流。

#------------------------------------------------------------
#                  |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.#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: <http://www.reibang.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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末竹习,一起剝皮案震驚了整個濱河市誊抛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌整陌,老刑警劉巖拗窃,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異泌辫,居然都是意外死亡随夸,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門震放,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宾毒,“玉大人,你說我怎么就攤上這事澜搅∥榉” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵勉躺,是天一觀的道長癌瘾。 經(jīng)常有香客問我,道長饵溅,這世上最難降的妖魔是什么妨退? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上咬荷,老公的妹妹穿的比我還像新娘冠句。我一直安慰自己,他們只是感情好幸乒,可當(dāng)我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布懦底。 她就那樣靜靜地躺著,像睡著了一般罕扎。 火紅的嫁衣襯著肌膚如雪聚唐。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天腔召,我揣著相機(jī)與錄音杆查,去河邊找鬼。 笑死臀蛛,一個胖子當(dāng)著我的面吹牛亲桦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浊仆,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼客峭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了氧卧?” 一聲冷哼從身側(cè)響起桃笙,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沙绝,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鼠锈,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡闪檬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了购笆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片粗悯。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖同欠,靈堂內(nèi)的尸體忽然破棺而出样傍,到底是詐尸還是另有隱情,我是刑警寧澤铺遂,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布衫哥,位于F島的核電站,受9級特大地震影響襟锐,放射性物質(zhì)發(fā)生泄漏撤逢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蚊荣。 院中可真熱鬧初狰,春花似錦、人聲如沸互例。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽媳叨。三九已至腥光,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肩杈,已是汗流浹背柴我。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留扩然,地道東北人艘儒。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像夫偶,于是被迫代替她去往敵國和親界睁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,066評論 2 355