181-书籍推荐--R语言你不知道的那些事

刘小泽写于2020.4.22 看到一本很薄的教程,和你分享:What They Forgot to Teach You About R (Jennifer Bryan, Jim Hester) 链接在:https://rstats.wtf/index.html

作者和我们分享了一些他们认为比较使用的R语言技巧,我也刚开始粗略地看了一下,一起学习吧

image-20200422233531912

主要章节

有的章节也不完整或者不太重要(字体非加粗)

Chapter 1 Saving source and blank slates

  • 1.1 Save source, not the workspace
  • 1.2 Use an IDE
  • 1.3 Always start R with a blank slate
  • 1.4 Restart R often during development
  • 1.5 What’s wrong with rm(list = ls())?
  • 1.6 Objects that take a long time to create:大型数据及时保存成Rdata
  • 1.7 Automated workflows

Chapter 2 Project-oriented workflow

主要讲解项目管理方式,包括setwd.Rproj,但现在一般都是直接用后者来管理【看: R-project管理多个R工作目录

Chapter 3 Practice safe paths

Chapter 4 How to name files

内容来自https://speakerdeck.com/jennybc/how-to-name-files

另外关于各种文件的命名,还有东西和你分享: 看到大佬在召唤相关的知识,下面的评论区一片答案: image-20200422231532761

  • 博文:https://datacarpentry.org/spreadsheet-ecology-lesson/02-common-mistakes/
  • PPT:http://www2.stat.duke.edu/~rcs46/lectures_2015/01-markdown-git/slides/naming-slides/naming-slides.pdf

Chapter 5 API for an analysis

内容来自:https://speakerdeck.com/jennybc/zen-and-the-art-of-workflow-maintenance?slide=57

Chapter 6 Get to know your R installation

Chapter 7 R Startup

讲了两个重要的配置文件

  • 7.1 .Renviron
  • 7.2 .Rprofile
  • 7.3 Disabling startup files

Chapter 8 Maintaining R

  • 升级或者降级R包版本;

  • R大版本升级后,怎么移动R library

Chapter 9 Set up an R dev environment

就是讲的R语言的安装和基本开发环境的配置

Chapter 10 Install a source package

介绍了几种利用devtools安装的方法

Chapter 11 Debugging R code

  • Use traceback() to determine where a given error is occurring.
  • Output diagnostic information in code with print(), cat() or message() statements.
  • Use browser() to open an interactive debugger before the error
  • Use debug() to automatically open a debugger at the start of a function call.
  • Use trace() to start a debugger at a location inside a function.
  • 最后还有Rmarkdown的 debug

Chapter 12 Read the source

Chapter 13 Reproduce the problem


学习笔记

1 Think of your R processes as livestock, not pets

二者区别在于:

  • Livestock is managed in herds and there is little fuss when individuals are lost or must be sacrificed
  • A pet, on the other hand, is unique and precious.

强调代码的粗犷度和可重复性,不要太重视细枝末节(比如某个变量的命名=》因为在重复性代码中,所有变量名称都只是一个中间过程,不要纠结一个脚本中的一个名称)一切以实现目的和可重复为基础

2 可视化界面推荐

3 如何关闭自动保存/加载rdata

  • Rstudio设置image-20200423085142683
  • 使用usethis::use_blank_slate(),看帮助文档
  • shell运行:R --no-save --no-restore-data,或者直接加到.bash_profile中:alias R='R --no-save --no-restore-data'

4 最省时的调试方法——重启

快捷键: Ctrl+Shift+F10 (Windows and Linux) or Command+Shift+F10 (Mac OS)

R脚本重新运行至当前行:Ctrl+Alt+B (Windows and Linux) or Command+Option+B (Mac OS)

Rmd重新运行至当前行:Ctrl+Alt+P (Windows and Linux) or Command+Option+P (Mac OS)

命令行退出:Ctrl+D or q()

5 关于rm(list = ls())

这个命令很多都是用在开头,用于重置环境,它会把当前全局环境的所有对象都删除,但不会取消已加载的包。如果是单独运行某个脚本是可以的,但是如果是自动化运行大批量命令,推荐使用 callr package 。真正重置运行环境最好还是重启R

6 升级/降级R包

使用:devtools::update_packages("pkgname")如果不存在这个包,会帮助安装【适用于CRAN或者GitHub开发版本的包】

如果要升级所有的CRAN包:devtools::update_packages(TRUE)

如果要降级:devtools::install_version("devtools", "1.13.3")

获取全部安装的R包名称:pkgs <- fs::path_file(fs::dir_ls("~/Library/R/3.6/library"))

7 R编程环境的设置

for Windows

需要使用Rtools,注意他不是一个R包,因此不是使用install.packages()安装。安装方法看:http://cran.r-project.org/bin/windows/Rtools/

在安装过程中,注意:

  • 不要选择Add rtools to system PATH这个安装选项

  • 不要选择Save version information to registry(默认会勾选,要取消勾选)

for Mac

需要安装xcode,检查方法是:devtools::has_devel()

image-20200423093017189

如果没安装,可以:

  • 命令行:xcode-select --install
  • 从app store安装

8 通过源文件安装R包

我们一般安装的CRAN包都是编译好的二进制文件,如果没有编译,接下来有几种方法:

  • devtools::install_dev():安装最新的开发版CRAN
  • devtools::install_github():安装GitHub包(不限于CRAN)
  • evtools::install_version():安装之前发布的CRAN版本包

例如:

  • devtools::install_dev("dplyr")
  • devtools::install_github("jimhester/lookup")
  • devtools::install_version("readr", "1.0.0")

9 安装到一个临时目录

library(devtools)

tmp_lib <- "~/tmp/tmp_library"
dir.create(tmp_lib)

devtools::install_github("dill/beyonce", lib = tmp_lib)

## 重启

## 加载的时候也要指定加载目录
library(beyonce, lib.loc = tmp_lib)

## your experimentation goes here

## done? clean up!
unlink(tmp_lib, recursive = TRUE)

10 R启动时的配置

R启动时需要做的事情很复杂(https://twitter.com/thomasp85/status/961553618196418560/photo/1):

image-20200423095717991

不过我们只需要重点关注两个文件即可:

.Renviron :R session中环境变量设置

可以设置:API keys(例如GitHub或者twitter)、R特定的环境变量

最简单的编辑方法是:usethis::edit_r_environ()

例如:

# 历史命令存储条数
R_HISTSIZE=100000
# GitHub personal access token(PAT)
GITHUB_PAT=abc123
# 默认R包安装位置
R_LIBS_USER=~/R/%p/%v
.Rprofile:每个R session需要配置的脚本
  • R在启动时提前运行的命令,它是在激活.Renviron中的环境变量之后进行的。一般存放在:~/.Rprofile 【当然可以通过修改R_PROFILE_USER环境变量修改这个位置】

  • 最简单的编辑方法是:usethis::edit_r_profile()

  • 里面存放些什么?

    • 默认镜像,如CRAN、Bioc
    • 写一段欢迎使用语
    • 修改R的命令提示符
    • 修改界面宽度、数值显示等等
    • 加载常用的包(就像电脑的默认启动项)
    • 设置别名alias,也即是命令的快捷方式(相信linux中这个不陌生)
注意

.Renviron.Rprofile文件的结尾都必须有换行符,否则最后一行不会被识别导致warning或error

11 如何debug

利用traceback()来回溯报错是哪里发生的

两种方法:

  • 报错后直接运行traceback(),例如:

    # 运行几行代码
    f <- function(x) x + 1
    g <- function(x) f(x)
    g("a")
    # 发现了有报错
    # > Error in x + 1 : non-numeric argument to binary operator
    # 然后运行
    traceback()
    # 会看到
    #> 2: f(x) at #1
    #> 1: g("a")
    
  • 或者直接在开头设定好,一有报错就提示

    options(error = traceback)
    g("a")
    #> Error in x + 1 : non-numeric argument to binary operator
    #> 2: f(x) at #1
    #> 1: g("a")
    
看看当前环境有哪些对象 =》ls()
及时检查数据结构=>str()
Yunze Liu
Yunze Liu
Bioinformatics Sharer

Co-founder of Bioinfoplanet(生信星球)

Next
Previous

Related