與文章 【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è)定涉及的功能:
- 設(shè)置
UTF-8
編碼沛厨。 - 設(shè)置 R 提示用英文顯示乘盼,方便查看和搜索 R 代碼錯誤信息。
- 設(shè)置 CRAN 下載源為同濟(jì)大學(xué)俄烁;Bioconductor 下載源為清華大學(xué)绸栅。
- 設(shè)定自定義 R 包存儲路徑,這個非常有用页屠,便于以后 R 版本涉及不影響 R 包粹胯。
- 設(shè)置 R 臨時文件目錄,這個在一些場景下也有用(比如快速查看臨時文件)辰企。
- 設(shè)置載入 pacman 用來管理 R 包风纠。
- 設(shè)置 VSCode r 插件需要進(jìn)行的設(shè)置。
- 提供一些工具函數(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