美文网首页
R自定义启动环境(.Rprofile)

R自定义启动环境(.Rprofile)

作者: 见龙在田007er2770 | 来源:发表于2019-05-20 15:57 被阅读0次

1. 三个目录

R_HOMEHOMEworking directory

R_HOME: the directory in which R is installed. Find out where your R_HOME is with the R.home() .
是R软件的安装目录。可以通过R.home() 得到。

HOME, the user’s home directory. Can ask R where this is with path.expand("~")
是R软件的家目录(默认工作目录)。可以通过path.expand("~")获得。

Current working directory. This is reported by getwd().
是R软件的当前工作目录。可以通过getwd()获得。

2. R.profile的配置

2.1 优先级

Current project > Home > R_Home

2.2 编辑方法

你可以通过编辑 .Rprofile 文件

file.edit(file.path("~", ".Rprofile")) 
# edit .Rprofile in HOME
file.edit(".Rprofile") 
# edit project specific .Rprofile 

结果如图


Rprofile.png
2.3 编辑内容

你可以添加以下参数,在启动和结束的时候显示时间。

> .First <- function(){
  cat("\nHi, Welcome at", date(), "\n")
}

>.Last <- function(){
  cat("\n Goodbye,", date(), "\n")

3. 案例示范

#--------------------------------------------
# Set custom library and temp directory for R
# NOTE: please only change following 2 paths
#   Any Question, please email to 
#       Shixiang Wang <w_shixiang@163.com>
#--------------------------------------------
.CUSTOM_LIB = "D:/tools/R/R_Library" # set your custom library location
.TMP = "D:/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)
library(ggplot2)
#----------------------------------------------------

# set mirror
local({r <- getOption("repos")
r["CRAN"] <- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"
options(repos=r)})
# custome star_up setting
#----------------------------------------------------

.First <- function(){
  
  cat("\nHi, Dr_zhou, Welcome at", date(), "\n")
}

.Last <- function(){
  cat("\nGoodbye, Dr_zhou,", date(), "\n")
}

#----------------------------------------------------

好了,这期就先写到这,欢迎交流。


reference

相关文章

网友评论

      本文标题:R自定义启动环境(.Rprofile)

      本文链接:https://www.haomeiwen.com/subject/kypkzqtx.html