R语言是用于统计分析,图形表示和报告的编程语言和软件环境。本篇博客记录自学 R 的笔记和心得。

1. R Environment Install

R 环境的安装有多种方式,这儿用 conda 安装:

1
2
3
4
5
6
7
$ conda activate
$ conda config --add channels r
$ conda config --add channels bioconda
$ conda install r

运行(检测)是否安装成功:

1
$ R

写第一个 R 程序:

1
2
3
4
5
> myString <- "Hello, World!"
> print ( myString)
[1] "Hello, World!"
[ctrl+D]退出

2. Run R script

运行 R 脚本:gedit or vim test.R,写入下面语句:

1
2
3
4
# My first program in R Programming
myString <- "Hello, World!"
print ( myString)

用 Rscript 运行:

1
2
$ Rscript test.R
[1] "Hello, World!"

R 命令查询,例如不知道 hist 含义和用法:

1
2
3
4
$ R
> ?hist
...
>q

3. Rstudio

gedit 或者 vim 有相应的 R 插件,但 Rstudio 是普遍认可最适合 R 的 IDE.

https://www.rstudio.com/products/rstudio/download 下载合适的版本(e.g. rstudio-1.4.1106-amd64.deb),然后:

1
2
3
4
5
$ sudo apt-get install unixodbc unixodbc-dev libsasl2-dev gdebi
$ sudo gdebi rstudio-1.4.1106-amd64.deb
$ rstudio

Rstudio 因为图形桌面,可直接上手。选中内容用 ctrl + [enter] 可直接运行,tab 键支持补全。

R 语法高亮设置:Tools -> Global Options -> Code -> Display -> Highlight R function calls

Something interesting:

1
2
3
4
5
> co2
> plot(co2)
> summary(co2)

4. References