geek-r-tutorial
geek-r-tutorial copied to clipboard
Rprofile
trafficstars
options(encoding = 'UTF-8')
# options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
# options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
#Sys.setenv(XML_CONFIG="/usr/local/include/libxml2")
Sys.setenv(LANGUAGE = 'en')
options(languageserver.formatting_style = function(options) {
style <- styler::tidyverse_style(indent_by = options$tabSize)
style$token$force_assignment_op <- NULL
style
})
#--------------------------------------------
# Set custom library and temp directory for R
# NOTE: please only change following 2 paths
# Any Question, please email to
# Shixiang Wang <[email protected]>
#--------------------------------------------
.CUSTOM_LIB = "/Users/wsx/R_library" # set your custom library location
#.TMP = "C:/Tools/R/Rtmp" # set a temp dir for R running
# please do not add '/' at the end !!!
if (!dir.exists(.CUSTOM_LIB)){
dir.create(.CUSTOM_LIB)
}
.libPaths(c(.CUSTOM_LIB, .libPaths()))
message("Using library: ", .libPaths()[1])
# if(dirname(tempdir()) != .TMP){
# if(!dir.exists(.TMP)) dir.create(.TMP)
# cat(paste0("TMPDIR = ", .TMP), file="~/.Renviron", sep = "\n")
# }
# message("Using temp directory: ", .TMP)
#---------------------------------------------------
# pacman is optional, you can delete following code
# If you wanna use pacman, please read:
# <https://www.jianshu.com/p/cb16ded75672>
# Basically,
# #1, you can use 'p_load' to load multiple package into R
# like p_load(data.table, dplyr)
# #2, you can use 'p_get' just to install package
# #3, you can use 'p_update' to update all packages
#---------------------------------------------------
if(!require(pacman)){
install.packages("pacman", dependencies = TRUE)
}
library(pacman)
# https://github.com/mikldk/roxytest
library(roxytest)
source(file.path(if (.Platform$OS.type == "windows") file.path(Sys.getenv("HOMEDRIVE"), Sys.getenv("HOMEPATH")) else Sys.getenv("HOME"), ".vscode-R", "init.R"))
# 使用清华镜像源
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
.loopInstall = function(pkg) {
Sys.sleep(1)
tryCatch(
{
message("=> Try installing ", pkg)
remotes::install_github(pkg)
},
error = function(e) {
.loopInstall(pkg)
}
)
}
.browseOnce <- function() {
old <- getOption("error")
function() {
options(error = old)
browser()
}
}
options(error = .browseOnce)
来自 y 叔 https://github.com/GuangchuangYu/rvcheck/blob/master/R/utilities.R
##' load function from package
##'
##'
##' @title get_fun_from_pkg
##' @param pkg package
##' @param fun function
##' @return function
##' @export
##' @author guangchuang yu
get_fun_from_pkg <- function(pkg, fun) {
## requireNamespace(pkg)
## eval(parse(text=paste0(pkg, "::", fun)))
require(pkg, character.only = TRUE)
eval(parse(text = fun))
}
##' check whether packages were installed
##'
##' check whether selected packages were installed
##' @title is.installed
##' @param packages selected packagtes
##' @return logical vector
##' @export
##' @author Guangchuang Yu
is.installed <- function(packages) {
vapply(packages, function(pkg) {
system.file(package = pkg) != ""
}, logical(1))
}
is.rserver <- function(){
RStudio.Version = tryCatch(get("RStudio.Version"), error = function(e) NULL)
if(is.null(RStudio.Version)) return(FALSE)
if(!is.function(RStudio.Version)) return(FALSE)
RStudio.Version()$mode == 'server'
}
`%||%` <- function(a, b) ifelse(is.null(a), b, a)
##' open working directory
##'
##'
##' @title o
##' @param file to be open; open workding directory by default
##' @return NULL
##' @author Guangchuang Yu
##' @export
o <- function(file=".") {
os <- Sys.info()[1]
if (is.rserver()) {
if (dir.exists(file)) {
stop("open directory in RStudio Server is not supported.")
}
rserver_ip <- getOption("rserver_ip")
if (!is.null(rserver_ip)) {
rserver_port <- getOption("rserver_port") %||% '8787'
if (!startsWith(rserver_ip, "http")) {
rserver_ip = paste0("http://", rserver_ip)
}
utils::browseURL(
paste0(
paste(rserver_ip, rserver_port, sep=":"),
"/file_show?path=",
file
))
} else {
file.edit <- get("file.edit")
file.edit(file)
}
} else if (os == "Darwin") {
cmd <- paste("open", file)
system(cmd)
} else if (os == "Linux") {
cmd <- paste("xdg-open", file, "&")
system(cmd)
} else if (os == "Windows") {
## wd <- sub("/", "\\", getwd())
## cmd <- paste("explorer", wd)
## suppressWarnings(shell(cmd))
cmd <- paste("start", file)
shell(cmd)
}
}