高效使用R笔记3

一、 R启动文件

每次R语言启动读入.Renviron和.Rprofile两个文件,前者主要是环境变量,程序位置和API密钥等;后者是启动进需要运行的几行R代码。启动时先找.Renviron,然后是.Rprofile,它们出现在3个目录中,安装目录(R_HOME,R.home()),家目录(HOME, Sys.getenv("HOME"))和当前工作目录(getwd()),顺序是从后往前的优先级,也就是有第三个就忽略第二个的。应该尽量在项目中使用特别的文件,防止对默认文件进行修改,更保险。

来看一个启动过程举例:

example(Startup)

Startp> ## Not run: 
Startp> ##D ## Example ~/.Renviron on Unix
Startp> ##D R_LIBS=~/R/library
Startp> ##D PAGER=/usr/local/bin/less
Startp> ##D 
Startp> ##D ## Example .Renviron on Windows
Startp> ##D R_LIBS=C:/R/library
Startp> ##D MY_TCLTK="c:/Program Files/Tcl/bin"
Startp> ##D 
Startp> ##D ## Example of setting R_DEFAULT_PACKAGES (from R CMD check)
Startp> ##D R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'
Startp> ##D # this loads the packages in the order given, so they appear on
Startp> ##D # the search path in reverse order.
Startp> ##D 
Startp> ##D ## Example of .Rprofile
Startp> ##D options(width=65, digits=5)
Startp> ##D options(show.signif.stars=FALSE)
Startp> ##D setHook(packageEvent("grDevices", "onLoad"),
Startp> ##D         function(...) grDevices::ps.options(horizontal=FALSE))
Startp> ##D set.seed(1234)
Startp> ##D .First <- function() cat("n   Welcome to R!nn")
Startp> ##D .Last <- function()  cat("n   Goodbye!nn")
Startp> ##D 
Startp> ##D ## Example of Rprofile.site
Startp> ##D local({
Startp> ##D   # add MASS to the default packages, set a CRAN mirror
Startp> ##D   old <- getOption("defaultPackages"); r <- getOption("repos")
Startp> ##D   r["CRAN"] <- "http://my.local.cran"
Startp> ##D   options(defaultPackages = c(old, "MASS"), repos = r)
Startp> ##D   ## (for Unix terminal users) set the width from COLUMNS if set
Startp> ##D   cols <- Sys.getenv("COLUMNS")
Startp> ##D   if(nzchar(cols)) options(width = as.integer(cols))
Startp> ##D   # interactive sessions get a fortune cookie (needs fortunes package)
Startp> ##D   if (interactive())
Startp> ##D     fortunes::fortune()
Startp> ##D })
Startp> ##D 
Startp> ##D ## if .Renviron contains
Startp> ##D FOOBAR="coobar"dohex"abc"def'"
Startp> ##D 
Startp> ##D ## then we get
Startp> ##D # > cat(Sys.getenv("FOOBAR"), "n")
Startp> ##D # coobardohexabc"def'
Startp> ## End(Not run)
Startp> 
Startp> 

1、 .Rprofile

可以使用这个命令编辑这个文件,file.edit("./.Rprofile")这里是编辑本项目的。

小插曲:关于不同操作系统的路径冲突问题,经常看到说反斜杠或者双斜杠解决,这里看到了另外两种解决方案:file.path(".","test","a.csv"), 另外一个是normalizedPath(“./test/a/csv”),会根据系统自动切换相符的路径,不错。

pathological包里的os_path()函数可以查找.Rprofile和.Renviron文件file.exits()检查文件是否存在。

1)  .Rprofile文件历程

其实它只是一个普通R脚本改了个名字而已,Ctrl+Shift+F10可以重启R会话。options()是我们熟悉的常用的默默默认选项设置函数。

# 欢迎语
message("Awesome!")
# 自定提示符
options(prompt='zR> ')
# 去除多行代码缩近提示符+,方便命令模式下的代码复制
options(continue='')

作者认为options(stringAsFactors=FALSE),让文本转换成字符而不是因子,放在启动脚本是有问题的,减少了代码可移植性。

2) 设置CRAN镜像

其实可以在Rstudio中简单完成,这里是配置文件进行。

# local创建新的空空间,避免对象r影响全局.GlobalEnv
local({
r <- getOption("repos")
r["CRAN'] <- "https://mirrors.bfsu.edu.cn/CRAN/
options(repos <- r)
})

3) fortunes包

这只是为了好玩而已,有趣的灵魂呀!这是一个R语录包,添加如下两行,每次输出一条语录:

if(interactive())
  try(fortunes::fortune(), silent=T)

使用try避免报错,::直接调用函数避免引入包。.Last()函数总在最后运行,require加载包也是避免报错。

.Last <- function(){
cond <- suppressWarnings(!reauire(fortunes,quietly = TRUE)
if(cond)
try(install.packages("fortunes"),silent=T)
message("Goodbye at ",date(), "n")
}

4) .Rprofile隐藏空间

rm(list=ls())会删除所有,使用.开头的ls()不会显示,可以避免。

2、 .Renviron

R_LIBS是这个文件的典型应用,Sys.getenv(“R_LIBS”)

二、 Rstudio

Rstudio我们都很熟悉了,看下有什么更值得学习和记录的东西,杰出的调试支持。作者是老程序员,所以希望少用鼠标,于是快捷键必不可少。

1)自动完成功能

R语言里有一些基本的自动完成,Rstudio更好用。最近很火的kite号称可以人工智能自动补全,应该是更更好用了。

2)快捷键

Alt+H+U更新Rstudio,应该是浏览器server版本不好用。Alt+T访问工具菜单,方便使用。可以指定不同的R版本,脚本的git版本控制,Restore .Rdata,编码风格,诊断(硬件太老有用)以及字体大小等外观。Ctrl+Z/Shift+Z 撤销/恢复,以前后面不知道呢 Ctrl+Enter 运行当前或选中命令 Ctrl+Alt+R 运行打开文档中的所有代码 Alt+Shift+Up/Down 向上向下复制当前行 Ctrl+D 删除当前行 当然,快捷键可以根据喜好修改。

3)推荐的目录结构

Project
- Readme.RMD # 项目描述
- setup.r # 必须的包
- R/ #代码
- input # 数据文件
- graphics/ #图
- output/ #输出 

压箱底的,一般不用

基本线性代数系统框架BLAS,切换到ATLAS或者OenBLAS,Intel MKL。

library("benchmarkme")
get_linear_alegebra()
res <- benchmark_std()

其他的改进策略

Microsoft R Open, 多线程的数学库,Rho(以前叫CXXR), pqR(2.15,过时了?), Renjin(可运行在JVM上),Tibco(C++为基础的解释器)以及Oracle的。



本篇文章来源于微信公众号: 微因

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注