R/R語言簡介

維基教科書,自由的教學讀本
< R

R語言的起源歷史[編輯]

R語言是S語言的一種方言,是「GNU S」, 一個自由的、有效的、用於統計計算和繪圖的語言和環境,它提供了廣泛的統計分析和繪圖技術:包括線性和非線性模型、統計檢驗、時間序列、分類、聚類等方法。

什麼是S[編輯]

S語言是由Bell實驗室的John Chambers等人開發的。 S語言1976年開始作為內部統計分析環境--最初作為Fortran庫的實現。 最初語言版本不包括統計模型函數。 1988年該系統用C語言重新編寫,與我們現在看到的系統類似(這是該語言的第3版)。Chambers和Hastie的《Statistical Models in S》(白皮書)描述了S語言的統計分析功能。 S語言的第4版在1988年發行,這就是我們今天所用的版本。John Chambers的《Programming with Data》(綠皮書)描述這個版本的S語言。

S歷史大事紀[編輯]

1993年,Bell實驗室授權StatSci(現在的Insightful Corp.)專用的license來開發和出售S語言。 2004年,Insightful花費200萬美元從Lucent購買了S語言,是S語言現在的所有者。 2006年,Alcatel購買了Lucent Technologies,並改名為Alcatel-Lucent。 Insightful銷售基於S語言的產品S-PLUS,S-PLUS在S語言基礎建立許多可視化的特徵,因此稱為「PLUS」。 2008年,TIBCO以2500萬美元控股Insightful。 S語言自從1998年後沒有過重大更新。 1998年,S語言贏得了Association for Computing Machinery’s Software System Award。

S哲學[編輯]

在「S進化的階段」中,John Chanbers寫到:「我們希望用戶能夠在一個交互性的環境,就像他們不認為自己在編程一樣。當他們的需求逐漸變得清晰,熟練度逐步提高,他們就可以逐漸地進入編程,這時語言和系統特性會變得更加重要。http://www.stat.bell-labs.com/S/history.html 」

回到R[編輯]

1991:紐西蘭奧克蘭大學的 Ross Ihaka 和 Robert Gentleman 兩人共同創建,其詞法和語法分別源自 Scheme 和 S 語言。他們開發R的經驗被發表在1996的JCGS。 1993:第一個R聲明向公眾發布。 1995:Martin Mächler說服Ross和Robert使用 GNU General Public License使得稱為開源免費軟體。 1996:公共郵件系統被創建(R-help和R-devel)。 1997:R Core Group成立(包括部分S-PLUS的人員)。核心小組控制著R原始碼。 2000:R 1.0.0發布。 2013:R 3.0.2在12月發布。 2014:R 3.1.2於10月31日發布。 2019:R 3.5.3 (Great Truth)於2019-03-11發布。 2019:R 3.6.0於2019-04-26即將發布.

小結[編輯]

R是S語言的一種實現。S語言是由AT&T貝爾實驗開發的一種用來進行數據探索、統計分析、作圖的解釋型語言。最初S語言的實現版本主要是S-PLUS。 S-PLUS是一個商業軟體,它基於S語言,並由MathSoft公司的統計科學不進一步完善。後來Auckland大學的Ross Ihaka 和 Robert Gentleman 及其他志願者開發了R系統。 R語言的使用和S-PLUS有很多類似之處,兩個軟體有一定的兼容性。

參考資料[編輯]

https://statstudy.github.io/doc/R_base_1.html https://github.com/jtleek/modules/blob/master/02_RProgramming/OverviewHistoryR/index.md

https://en.wikipedia.org/wiki/R_(programming_language)

R語言定義與特徵[編輯]

R是一個開放的統計編程環境,是一門用於統計計算和作圖的語言。 R is a language and environment for statistical computing and graphics. http://www.r-project.org/about.html

R特徵[編輯]

  • 語法接近S語言,方便S_PLUS用戶使用。
  • 語義表面上與S類似,但實際上是不同的。
  • 全平台/系統運行。 (包括PlayStation 3)
  • 定期更新(每年修復bug);開發積極。
  • 極其精簡,就像一個軟體;函數封裝在不同的擴展包中。
  • 繪圖能力十分強大,強於大多數統計學軟體。
  • 便於交互工作,但包含開發新工具的強大程式語言
  • 積極活躍的用戶社區;R-help和R-devel mailing lists、Stack Overflow
  • 免費

免費軟體[編輯]

With free software, you are granted

  • The freedom to run the program, for any purpose (freedom 0).
  • The freedom to study how the program works, and adapt it to your needs (freedom 1). Access to the source code is a precondition for this.
  • The freedom to redistribute copies so you can help your neighbor (freedom 2).
  • The freedom to improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this. http://www.fsf.org

R缺點[編輯]

  • 基於40年前的技術開發。
  • 對動態或3D圖形支持少(「old days」版本後有所提升)。
  • 函數開發處於用戶需求和貢獻。如果沒人和你有相同需求,那麼這可能就是你的工作(或者付費找人開發)。
  • 對象必須儲存在物理內存中,但現在有很多新技術可以實現。
  • 不能應對所有可能的情況 (這也是所有軟體的弊端)。

R系統的設計[編輯]

R系統分成兩個概念模塊:

  1. 從CRAN下載的「base」 R系統。
  2. 其他。 R函數分別儲存在不同的packages中。
  • 「base」 R系統包括:  base package是運行R所必須的,包含了多多樹基礎函數。
  • 「base」系統中的其他packages:utils, stats, datasets, graphics, grDevices, grid, methods, tools, parallel, compiler, splines, tcltk, stats4
  • 建議安裝的packages: boot, class, cluster, codetools, foreign, KernSmooth, lattice, mgcv, nlme, rpart, survival, MASS, spatial, nnet, Matrix.

還有很多其他的擴展包:

  • 14086 CRAN packages.
  • Bioconductor project packages (http://bioconductor.org).
  • 還有一些自己開發的擴展包存放在github或其他代碼託管平台,甚至是個人網站上,沒有辦法可靠統計這些包的數量。

R學習資源[編輯]

在線資料[編輯]

CRAN資料

各種cheatsheet[編輯]

R視頻教程[編輯]

【生信技能樹】生信人應該這樣學R語言

【生信技能樹】公共資料庫挖掘實例(基於R語言)

R語言入門生信應用基礎課程

R語言入門數據分析

生物信息學入門基礎講座的R部分


R書籍[編輯]

bookdown寫書

bookdown寫的公開書資源

初級入門[編輯]

《An Introduction to R》

《R4Beginners》

153 分鐘學會 R

《統計建模與R軟體》

《R Cookbook》

R語言實戰(第2版)《R in action》

《R in a Nutshell》

高級入門[編輯]

《Statistics with R》

《The R book》

繪圖與可視化[編輯]

《R Graphics》

《R Graphics Cookbook》

《Lattice:Multivariate Data Visualization with R》

《ggplot2:Elegant Graphics for Data Analysis》

《Data Mining with Rattle and R》

《Interactive and Dynamic Graphics for Data Analysis With R and GGobi》

《現代統計圖形》

時間序列分析[編輯]

《Time Series Analysis and Its Applications :with R examples》

《Analysis of financial time series》

《Analysis of Integrated and Cointegrated Time Series with R》

《Wavelet Methods in Statistics with R》

計量經濟學[編輯]

《Econometrics In R》

《Applied Econometrics with R》

金融[編輯]

《Advanced Topics in Analysis of Economic and Financial Data Using R》

《Modelling Financial Time Series With S-plus》

《Simulation Inference Stochastic Differential Equations:with R examples》

《Simulation Techniques in Financial Risk Management》

《Modern Actuarial Risk Theory Using R》

《Quantitative Risk Management:Concepts, Techniques and Tools》

《Portfolio Optimization with R》

《Option Pricing and Estimation of Financial Models with R》。

數據挖掘[編輯]

《Data Mining with R:learing with case studies》

《Data Mining explain using R》

系列圖書[編輯]

Springer的Use R!系列圖書

R官方書單

《R for Data Science》的中譯版(英文版連結

《A Handbook of Statistical Analyses_Using_R》

《Modern Applied Statistics With S》

《Introduction to Scientific Programming and Simulation Using R》

《Mastering Scientific Computing with R》

《Practical Data Science with R》

《software for data analysis programming with R》

生物信息學資源[編輯]

R語言的運行環境[編輯]

CRAN的R語言環境 集成開發環境Rstudio Microsoft R Open 基於anaconda/Miniconda構建的R環境

CRAN的R語言環境[編輯]

官方網站:https://www.r-project.org/

windows下載後,雙擊安裝。

Mac系統自帶R,也可以下載安裝,和windows類似。

# Ubuntu:
apt-get update
apt-get install r-base r-base-dev
# CentOS
yum install epel-release
yum install R

# 源码编译安装,CentOS7系统测试过,不需要安装依赖
wget https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-3/R-3.5.3.tar.gz
tar zxvf R-3.5.3.tar.gz
cd R-3.5.3
./configure --enable-R-shlib=yes --with-tcltk --prefix=/opt/biosoft/R
make -j 4
sudo make install
# Linux:CentOS 7.5
echo 'export PATH=/opt/biosoft/R:$PATH' >> ~/.bashrc

最好用的R語言IDE--Rstudio[編輯]

Rstudio網站(Free版即可)下載RStudio Desktop Open Source License Windows和mac系統安裝比較容易。 Linux伺服器RStudio Server Open Source License的,需要進行一些網絡的設置,安裝好後,可以通過伺服器ip地址通過網頁使用伺服器版的RStudio。

Microsoft R Open[編輯]

基於anaconda/Miniconda構建的R環境[編輯]

軟體包下載:windwos和mac都是可視化安裝,比較簡單。 Linux系統下載安裝包後,需要使用命令行命令bash安裝:

# 注意
bash Anaconda2-5.3.1-Linux-x86_64.sh
# 设置好安装路径,并同意将路径添加到系统PATH中,否则需要自己添加。
# 安装配置好conda后
conda install -y r

如此,可以開始R之旅了!!!

R語言擴展包[編輯]

utils包中的*.packages函數管理CRAN源的擴展包

BiocManager管理bioconductor中的擴展包

devtools安裝開發中擴展包

github_install安裝github上的擴展包

utils包中的*.packages函數[編輯]

## 修改CRAN的镜像源(官方的软件源在国外,速度会受影响)
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))

## 列出所有当前可用扩展包
> available.packages()
                              Package                         Version         
A3                            "A3"                            "1.0.0"         
abbyyR                        "abbyyR"                        "0.5.4"         
abc                           "abc"                           "2.1"           
......
## 函数返回一个矩阵形式的扩展包列表,包括软件包名称及当前最新版本

## 安装扩展包
> install.packages("PackageName")

## 查看已安装的软件包
> installed.packages("PackageName")

## 更新软件包
## 更新特定扩展包
> update.packages("PackageName")
## 更新所有软件包
> update.packages()

## 移除扩展包
> remove.packages("PackageName")

BiocManager:管理bioconductor中的擴展包[編輯]

if (!"BiocManager" %in% rownames(installed.packages()))
     install.packages("BiocManager")
## 修改biocondutor的镜像
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")

## 列出所有可以安装的R扩展包
> BiocManager::available()

## 安装扩展包
> BiocManager::install("PackageName")
## 由于许多biocondutor扩展包是依赖CRAN上的扩展包的,因此,BiocManager::install()也可以用来安装CRAN的扩展包
## 同时安装多个扩展包
pkgs <- c("PackageName1", "PackageName2", "PackageName3",...)
BiocManager::install(pkgs)

devtools安裝開發中擴展包[編輯]

https://cran.r-project.org/package=devtools devtools包含了一系列用來開發R包的工具。

# install.packages("devtools")
library(devtools)
install_github('hadlley/dplyr')

githubinstall安裝github上的擴展包[編輯]

https://cran.r-project.org/package=githubinstall

#install.packages('githubinstall') 
library(githubinstall)
githubinstall('AnomalyDetection')

R擴展包儲存的鏡像地址[編輯]

R包幫助系統[編輯]

在線幫助系統[編輯]

本地幫助系統[編輯]

#### 关于R的基础知识 ####
help.start()


#### 关于R中的函数或关键字符 ####
## 查看sum函数的帮助文档
help(sum)
?sum
## 查看heatmap函数的帮助文档
help(heatmap)
?heatmap
## 查找所有包含histogram函数的扩展包的帮助文档
help("histogram",try.all.packages=T)
## 查找lattice包中的histogram帮助文档
help("histogram",package="lattice")

## R site search
Search help files, manuals, and mailing list archives.
## 搜索http://search.r-project.org网站的帮助页面
RSiteSearch("matlab")

## 找出名字中含有“plot”的函数
apropos("plot")
apropos("plot", mod="function")
## 列出所有在帮助页面含有字符“plot”的函数帮助文档
help.search("plot")
## 得到名为“plot”函数所在的程序包
find("plot")
## 得到名为“plot”函数的自变量列表
args("plot")

## 展示mean函数使用示例
example(mean)
## 显示hist函数使用示例
example("hist")
## 展示数据处理和绘图实例
example("example")
## 展示R语言的各种类型的绘图示例
demo(graphics)
## R 3D绘图示例
demo(persp)

## 查看Biostrings软件包自带的帮助文档
browseVignettes(package = "Biostrings")
system.file(package = "Biostrings")
## 将软件包Biostrings中Rnw格式转换成纯R代码格式
library("tools")
vigSrc = list.files(pattern = "Rnw$",
                    system.file("doc", package = "Biostrings"),
                    full.names = TRUE)
vigSrc
for (v in vigSrc) Stangle(v)