├── .Rprofile ├── .gitignore ├── 00-author.Rmd ├── 01-prepare.Rmd ├── 02-base.Rmd ├── 03-data-import.Rmd ├── 04-data-clean.Rmd ├── 05-data-visualization.Rmd ├── 06-data-model.Rmd ├── 07-result-report.Rmd ├── 08-bio-application.Rmd ├── 09-supp.Rmd ├── 10-references.Rmd ├── README.md ├── _bookdown.yml ├── _common.R ├── _output.yml ├── book.bib ├── bookdown.log ├── bookdown.tex ├── cover.png ├── css └── style.css ├── data └── data-import │ ├── example.json │ ├── mtcars.RData │ ├── mtcars.csv │ ├── mtcars.rds │ ├── mtcars.tsv │ └── mtcars.xlsx ├── docs ├── author.html ├── base.html ├── bioapp.html ├── bookdown_files │ └── figure-html │ │ ├── unnamed-chunk-146-1.png │ │ ├── unnamed-chunk-147-1.png │ │ ├── unnamed-chunk-148-1.png │ │ ├── unnamed-chunk-149-1.png │ │ ├── unnamed-chunk-151-1.png │ │ ├── unnamed-chunk-153-1.png │ │ ├── unnamed-chunk-158-1.png │ │ ├── unnamed-chunk-159-1.png │ │ ├── vector-add-1.png │ │ ├── vector-align-1.png │ │ └── vector-construction-1.png ├── clean.html ├── cover.png ├── css │ └── style.css ├── expand-reading.html ├── fig │ ├── ch00-data-science.png │ ├── ch01-download-rstudio.PNG │ ├── ch01-rstudio-overview.PNG │ ├── ch01-rtools-mac.PNG │ ├── ch01-rtools-win.PNG │ ├── ch01-select-64-bit.PNG │ ├── ch01-set-install-path-win.PNG │ ├── ch02-fun-env.png │ └── ch02-rstudio-env-panel.PNG ├── import.html ├── index.html ├── libs │ ├── gitbook │ │ ├── css │ │ │ ├── fontawesome │ │ │ │ └── fontawesome-webfont.ttf │ │ │ ├── plugin-bookdown.css │ │ │ ├── plugin-clipboard.css │ │ │ ├── plugin-fontsettings.css │ │ │ ├── plugin-highlight.css │ │ │ ├── plugin-search.css │ │ │ ├── plugin-table.css │ │ │ └── style.css │ │ └── js │ │ │ ├── app.min.js │ │ │ ├── clipboard.min.js │ │ │ ├── jquery.highlight.js │ │ │ ├── lunr.js │ │ │ ├── plugin-bookdown.js │ │ │ ├── plugin-clipboard.js │ │ │ ├── plugin-fontsettings.js │ │ │ ├── plugin-search.js │ │ │ └── plugin-sharing.js │ └── jquery │ │ └── jquery.min.js ├── model.html ├── prepare.html ├── references.html ├── report.html ├── search-table.html ├── search_index.json └── visualization.html ├── fig ├── ch00-data-science.png ├── ch01-download-rstudio.PNG ├── ch01-rstudio-overview.PNG ├── ch01-rtools-mac.PNG ├── ch01-rtools-win.PNG ├── ch01-select-64-bit.PNG ├── ch01-set-install-path-win.PNG ├── ch02-fun-env.png └── ch02-rstudio-env-panel.PNG ├── geek-r-tutorial.Rproj ├── index.Rmd ├── latex ├── after_body.tex ├── before_body.tex ├── preamble.tex └── template.tex ├── packages.bib └── render.R /.Rprofile: -------------------------------------------------------------------------------- 1 | source("~/.Rprofile") 2 | if (.Platform$OS.type == 'windows') Sys.setlocale(, 'Chinese') 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | _book 6 | _bookdown_files 7 | -------------------------------------------------------------------------------- /00-author.Rmd: -------------------------------------------------------------------------------- 1 | # 作者简介 {#author .unnumbered} 2 | 3 | [王诗翔](https://shixiangwang.github.io/home/cn/),生信技能树成员,R 语言爱好者。对编程、数据分析都有比较浓厚的兴趣,喜欢开源与开放的科学。长期在简书发文,连载文集[《极客RrR<<-数据分析之道》](https://www.jianshu.com/nb/22007361),并运营微信公众号[《优雅R》](https://shixiangwang.github.io/home/logo/qrcode.jpg)用于推送数据分析相关知识。 4 | -------------------------------------------------------------------------------- /01-prepare.Rmd: -------------------------------------------------------------------------------- 1 | \mainmatter 2 | 3 | # 准备工作 {#prepare} 4 | 5 | 想要在本地计算机上运行 R 语言代码,读者需要安装 R。为了方便 R 语言环境的使用和代码编写,我推荐读者使用 RStudio 公司开发的 R 集成开发环境(IDE)RStudio。 6 | 7 | 如果读者已经在计算机上安装好 R 和 RStudio,可以跳过本节内容。 8 | 9 | 如果读者倾向于使用像 VS Code 和 Idea 这样的流行编辑器,请自行安装对应的 R 语言拓展插件。 10 | 11 | ## R 的下载和安装 12 | 13 | 点击链接 进入 CRAN 的清华镜像源,选择与自己操作系统对应的 R 进行下载。 14 | 15 | - Windows 系统用户进入 [base 子目录](https://mirrors.tuna.tsinghua.edu.cn/CRAN/),点击 **Download R x.x.x for Windows**。 16 | - MacOS 系统用户通过镜像地址进入 **R for Mac OS X** 页面后,点击 `R-x.x.x.pkg` 进行下载。 17 | - Linux 系统发行版众多,且需要一些额外的技术知识,请相应用户请阅读本章【**常见问题与方案**】一节学习 R 在 Linux 发行版下的安装。 18 | 19 | > 上述的 `x.x.x` 指代 R 的版本号,目前是 `3.6.3`(2020-03),以用户下载时的实际版本号为准。 20 | 21 | 对于 MacOS 系统用户,下载后直接双击一路向下进行傻瓜式操作即可安装成功。 22 | 23 | 对于 Windows 系统用户,根据笔者几年的安装和使用经验,安装时有一些额外的注意事项: 24 | 25 | 1. Windows 一般包含多个盘符,请读者尽量不要将 R 安装在 `Program files` 这样有空格的目录或中文目录下。读者可以自行创建一个专门的工具用于安装 R 以及放置 R 的三方包,如 `C:/Tools`,见图 \@ref(fig:set-install-path)。 26 | 2. 读者的电脑如果有 SSD 固态硬盘,最好将 R 安装到固态硬盘目录里,因为读写数据快。 27 | 3. 一般现在电脑是 64 位的,如果你确定是这样,在安装时有关于 32 位的选项都可以不勾选,如图 \@ref(fig:select-64bit)。 28 | 4. 如果涉及到添加环境变量/路径之类的步骤,勾选添加即可。 29 | 5. 以上没提到的,一路点下一步。 30 | 31 | ```{r set-install-path, fig.align="center", echo=FALSE, fig.cap="设置安装路径"} 32 | knitr::include_graphics("fig/ch01-set-install-path-win.PNG") 33 | ``` 34 | 35 | ```{r select-64bit, fig.align="center", echo=FALSE, fig.cap="只选择 64 位"} 36 | knitr::include_graphics("fig/ch01-select-64-bit.PNG") 37 | ``` 38 | 39 | 在 Windows 或 MacOS 下读者如果想要安装含有像 C++ 这样的源码的包,需要安装编译工具如 g++,这些编译工具都被 R 语言团队打包成了 Rtools,如果读者有这方面的需求,请阅读本章【**常见问题与方案**】一节学习 Rtools 的安装。 40 | 41 | ## RStudio 的下载和安装 42 | 43 | 点击链接 进入 RStudio 下载界面,根据自己的操作系统选择适合的安装文件进行安装即可,如图 \@ref(fig:install-rstudio)。 44 | 45 | ```{r install-rstudio, fig.align="center", echo=FALSE, fig.cap="下载 RStudio"} 46 | knitr::include_graphics("fig/ch01-download-rstudio.PNG") 47 | ``` 48 | 49 | RStudio 的安装非常简单,没有特别的注意事项,读者可以直接一路点击向下。另外,读者可以自定义 RStudio 的安装路径,这不会影响 R 的使用。 50 | 51 | 52 | RStudio 安装完成后可以像普通软件一样搜索和双击打开,如图 \@ref(fig:rstudio-overview)。 53 | 54 | ```{r rstudio-overview, fig.align="center", echo=FALSE, fig.cap="RStudio 界面"} 55 | knitr::include_graphics("fig/ch01-rstudio-overview.PNG") 56 | ``` 57 | 58 | 下面对 RStudio 的界面进行简单介绍,读者在后续的使用中将会逐渐熟悉。 59 | 60 | - 最上方是菜单栏,所有的功能都可以通过它们找到,没事可以多点一点。 61 | - 左上方是代码编辑窗口,平时写代码的地方。 62 | - 左下方是 R 控制台(还有终端和任务设定),我们可以通过控制台键入命令并观察 R 的输出(在代码编辑窗口中通过 Ctrl + Enter 快捷键可以将光标行代码发送到控制台,**非常有用**)。 63 | - 整个右方是一些辅助窗口,最重要的是右上方的环境 Environment 窗口(用于展示当前环境中的对象:包括变量、函数等)和下方的 5 个窗口:Files、Plots、Packages、Help 以及 Viewer。 64 | 65 | 66 | ## 配置(可选) 67 | 68 | > 下面的说明针对的是 Windows 系统,但对于其他系统也可以进行类似的设定。 69 | 70 | Windows 下的 R 默认使用用户文档目录作为家目录(等同于 Linux 中的`~`),使用系统指定的临时目录作为临时目录,使用安装路径下的 `R版本/library` 目录作为 R 包存储目录。 71 | 72 | 如果读者什么都选择默认的, 73 | 74 | * 当你一时安装包过多,或者装了电脑管家之类的管理软件时,系统的临时目录经常会把 RStudio 锁死,导致不能进行读写。 75 | * 当你想要更新 R 版本时,有时你不得不面临重装所有包的举动(如果你使用几个月,装了几百个包...),或者想其他办法解决。 76 | 77 | 下面介绍如何创建自定义的临时目录与包目录,这样上面情况都不会发生了。 78 | 79 | 读者在安装好 R 和 RStudio 后,打开 RStudio,在 R 控制台键入: 80 | 81 | ```r 82 | file.edit("~/.Rprofile") 83 | ``` 84 | 85 | 在启动RStudio时,RStudio会首先执行里面的 R 代码,所以我们可以在这里用 R 代码进行配置。 86 | 87 | 首先在该文档内添加内容: 88 | 89 | ```r 90 | #-------------------------------------------- 91 | # Set custom library and temp directory for R 92 | # NOTE: please only change following 2 paths 93 | # Any Question, please email to 94 | # Shixiang Wang 95 | #-------------------------------------------- 96 | .CUSTOM_LIB = "C:/Tools/R/R_Library" # set your custom library location 97 | .TMP = "C:/Tools/R/Rtmp" # set a temp dir for R running 98 | # please do not add '/' at the end !!! 99 | 100 | if(!dir.exists(.CUSTOM_LIB)){ 101 | dir.create(.CUSTOM_LIB, recursive = TRUE) 102 | } 103 | 104 | .libPaths(c(.CUSTOM_LIB, .libPaths())) 105 | message("Using library: ", .libPaths()[1]) 106 | 107 | 108 | if(dirname(tempdir()) != .TMP){ 109 | if(!dir.exists(.TMP)) dir.create(.TMP, recursive = TRUE) 110 | cat(paste0("TMPDIR = ", .TMP), file="~/.Renviron", sep = "\n") 111 | } 112 | message("Using temp directory: ", .TMP) 113 | 114 | #--------------------------------------------------- 115 | # pacman is optional, you can delete following code 116 | # If you wanna use pacman, please read: 117 | # 118 | # Basically, 119 | # #1, you can use 'p_load' to load multiple package into R 120 | # like p_load(data.table, dplyr) 121 | # #2, you can use 'p_get' just to install package 122 | # #3, you can use 'p_update' to update all packages 123 | #--------------------------------------------------- 124 | if(!require(pacman)){ 125 | install.packages("pacman", dependencies = TRUE) 126 | } 127 | library(pacman) 128 | #---------------------------------------------------- 129 | ``` 130 | 131 | 然后根据情况对上述内容中的目录设定进行修改即可。 132 | 133 | ```r 134 | .CUSTOM_LIB = "C:/Tools/R/R_Library" # set your custom library location 135 | .TMP = "C:/Tools/R/Rtmp" # set a temp dir for R running 136 | # please do not add '/' at the end !!! 137 | ``` 138 | 139 | **pacman** 那段代码是可选的,该包是 `library()` 函数的替代品,使用它安装和管理 R 包更简单。具体的使用方法可以[点击阅读](https://www.jianshu.com/p/cb16ded75672)我之前的简书文章。 140 | 141 | 这里为了方便大家使用,我添加了一些必要注释,如果上述配置存在问题,读者可以发邮件给我。 142 | 143 | 保存后重启 RStudio 或者点击菜单栏 **Session** 下的 **Restart R**。 144 | 145 | 以后 RStudio 每次启动后都会输出读者计算机中 R 包的存储路径和它使用的临时路径。读者如果以后升级 R,只要重装下 R 安装文件就可以了,R 包的目录并不会改动,键入下面的命令可以更新所有的 R 包: 146 | 147 | ```r 148 | p_update() 149 | ``` 150 | 151 | 152 | ## 常见问题与方案 153 | 154 | 除了本节目前罗列的问题,读者在学习本章内容时遇到的其他问题都可以通过 [GitHub Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 提出和进行讨论。如果读者提出的是通性问题,将增补到该节。 155 | 156 | ### R 在 Linux 系统下的安装 157 | 158 | #### Ubuntu 从源安装 R 159 | 160 | 下述操作基于在 Ubuntu 18 系统上安装 R 3.5 的实践。 161 | 162 | > `$` 前缀指明命令在 `shell` 终端中进行。 163 | 164 | 安装依赖: 165 | 166 | ```sh 167 | $ sudo apt-get install xorg-dev libreadline-dev 168 | $ sudo apt-get install libcurl4-openssl-dev 169 | $ sudo apt-get install libbz2-dev 170 | $ sudo apt-get install libcairo2-dev libgtk2.0-dev 171 | $ sudo apt-get install texinfo texlive 172 | $ wget http://mirrors.ctan.org/fonts/inconsolata.zip 173 | $ sudo cp -Rfp inconsolata/* /usr/share/texmf/ 174 | $ # 或者 sudo cp -r inconsolata/ /usr/share/texlive/texmf-dist/tex/latex/ 175 | $ sudo mktexlsr # 刷新 176 | 177 | # 如果没有java解释器,安装下 178 | $ sudo apt-get install default-jdk 179 | ``` 180 | 181 | 下载与安装 R: 182 | 183 | ```sh 184 | $ curl -O http://cran.utstat.utoronto.ca/src/base/R-3/R-3.5.0.tar.gz 185 | $ tar -zxvf R-3.5.0.tar.gz 186 | $ cd R-3.5.0/ 187 | $ ./configure --prefix=$HOME/local/R --enable-R-shlib --with-cairo=yes 188 | $ # ./configure --with-cairo --with-libpng --with-libtiff --with-jpeglib --enable-R-shlib --prefix=$HOME/local/R 189 | $ make 190 | $ make install 191 | ``` 192 | 193 | 创建符号链接: 194 | 195 | ```sh 196 | $ cd /usr/bin/ 197 | $ sudo ln -s $HOME/local/R/bin/Rscript Rscript 198 | $ sudo ln -s $HOME/local/R/bin/R R 199 | ``` 200 | 201 | 检查: 202 | 203 | ```sh 204 | $ ls -l R* 205 | lrwxrwxrwx 1 root root 23 6月 28 17:01 R -> /home/wsx/local/R/bin/R 206 | lrwxrwxrwx 1 root root 29 6月 28 17:01 Rscript -> /home/wsx/local/R/bin/Rscript 207 | ``` 208 | 209 | 使用: 210 | 211 | ```sh 212 | $ R 213 | 214 | R version 3.5.0 (2018-04-23) -- "Joy in Playing" 215 | Copyright (C) 2018 The R Foundation for Statistical Computing 216 | Platform: x86_64-pc-linux-gnu (64-bit) 217 | 218 | R is free software and comes with ABSOLUTELY NO WARRANTY. 219 | You are welcome to redistribute it under certain conditions. 220 | Type 'license()' or 'licence()' for distribution details. 221 | 222 | Natural language support but running in an English locale 223 | 224 | R is a collaborative project with many contributors. 225 | Type 'contributors()' for more information and 226 | 'citation()' on how to cite R or R packages in publications. 227 | 228 | Type 'demo()' for some demos, 'help()' for on-line help, or 229 | 'help.start()' for an HTML browser interface to help. 230 | Type 'q()' to quit R. 231 | 232 | > 233 | 234 | ``` 235 | 236 | 如果遇到编译问题和报错,读者可以参考下面两篇博文: 237 | 238 | * [ubuntu 上安装 R 的时候遇到的问题总结](https://www.cnblogs.com/wing1995/p/4635371.html) 239 | * [CentOS 下 R 的安装](http://m.blog.itpub.net/9036/viewspace-2122686/) 240 | 241 | 242 | #### CentOS 从源安装 R 243 | 244 | 245 | 下述操作基于在 CentOS 系统上安装 R 3.6 的实践。 246 | 247 | > `$` 前缀指明命令在 `shell` 终端中进行。 248 | 249 | 首先切入 root 账户。 250 | 251 | 安装 R 编译依赖: 252 | 253 | ```sh 254 | $ yum-builddep R 255 | ``` 256 | 257 | 消灭界面configure警告:`configure: WARNING: neither inconsolata.sty nor zi4.sty found: PDF vignettes and package manuals will not be rendered optimally` 258 | 259 | ```sh 260 | $ wget http://mirrors.ctan.org/fonts/inconsolata.zip 261 | $ Unzip inconsolata.zip 262 | $ cp -Rfp inconsolata/* /usr/share/texmf/ 263 | $ mktexlsr 264 | ``` 265 | 266 | 下载 R 并解压: 267 | 268 | ```sh 269 | $ wget -c https://cran.r-project.org/src/base/R-3/R-3.6.0.tar.gz 270 | $ tar zxvf R-3.6.0.tar.gz 271 | ``` 272 | 273 | 编译安装 274 | 275 | ```sh 276 | $ cd R-3.6.0/ 277 | $ ./configure --prefix=/home/public/R/R-base --enable-R-shlib --with-cairo=yes 278 | ``` 279 | 280 | 这里 `prefix` 后面的路径可以自己指定。 281 | 282 | 然后 283 | 284 | ```sh 285 | $ make 286 | $ make install 287 | ``` 288 | 289 | 最后可以把它链接到常用软件位置上去。 290 | 291 | ```sh 292 | $ sudo ln -s /home/public/R/R-basebin/R /bin/R 293 | ``` 294 | 295 | 296 | ### Rtools 安装 297 | 298 | #### Windows 系统 Rtools 299 | 300 | 点击链接 进入 CRAN 页面,依次点击【Download R for Windows】、【Rtools】,然后选择推荐的版本下载和安装,如图 \@ref(fig:rtools-win)。 301 | 302 | 303 | ```{r rtools-win, fig.align="center", echo=FALSE, fig.cap="Windows 下 Rtools 的下载"} 304 | knitr::include_graphics("fig/ch01-rtools-win.PNG") 305 | ``` 306 | 307 | 安装时一路点击向下,建议不要修改安装路径,因为 R 的很多编译环境变量设定都指向默认的安装路径,读者如果修改它后续很可能引起不必要的麻烦。 308 | 309 | #### MacOS 系统 Rtools 310 | 311 | 点击链接 进入 CRAN 页面,依次点击【Download R for Mac OS X】、【tools】,然后选择最新版的 `clang-x.x.x.pkg` 和 `gfortran-x.x.x.pkg` 下载和安装即可,如图 \@ref(fig:rtools-mac)。 312 | 313 | > `x.x.x` 指代版本号,如 `8.0.0`。 314 | 315 | 316 | ```{r rtools-mac, fig.align="center", echo=FALSE, fig.cap="MacOS 下 Rtools 的下载"} 317 | knitr::include_graphics("fig/ch01-rtools-mac.PNG") 318 | ``` 319 | 320 | ### RStudio Server 安装 321 | 322 | RStudio Server 比较好用,适用于安装在小型服务器上以方便多人同时利用服务器的计算资源。需要注意,RStudio Server 的安装需要管理员(root)权限。 323 | 324 | 点击链接 进入 RStudio Server 下载页面,针对不同的 Linux 发行版,RStudio 公司已经有良好的文档支持,所以我不再赘述。 325 | 326 | 在使用 RStudio Server 时我有一个小小的建议,如我在前面的配置一节所说,读者可以自己创建一个专门的目录管理所有的 R 包。并且针对该目录,建议新建一个 Linux 群组以管理它的访问权限。 327 | 328 | 这里简介一下安装后添加其他人,以增加用户 rotation 为例: 329 | 330 | ```sh 331 | $ sudo useradd rotation -d /home/rotation -m -g wsx # 将新增用户添加到你所在群组 332 | $ sudo passwd rotation # 设定初始密码 333 | $ sudo adduser rotation sudo # 给用户添加sudo权限,以方便一些操作 334 | ``` -------------------------------------------------------------------------------- /03-data-import.Rmd: -------------------------------------------------------------------------------- 1 | # 数据导入 {#import} 2 | 3 | 在掌握一定的 R 编程技能后,我们开始迈入数据分析的殿堂。大部分数据分析事务的数据都不是通过 R 创建,而是来自于各种数据收集软硬件、渠道,包括 Excel、网络等。本章聚焦于如何将数据导入 R 中以便于开始数据分析。对于本章的内容,读者既可以系统地从头到尾深入阅读学习,也可以根据自己实际工作需要或时间限制选择一些重点或感兴趣内容进行掌握。 4 | 5 | 本章的重点内容包括符号分隔文件、Excel 文件、JSON 文件以及 R 支持的数据格式 RData 和 RDS,其他格式的数据将放到本章【**常见问题与方案**】一节作为补充介绍。 6 | 7 | ## 符号分隔文件 8 | 9 | 符号分隔文件是**最最常用**的数据文件格式,知道如何导入它是读者的必备技能。这里的**符号**泛指一切可以用作数据内容分隔的符号,常见的包括逗号(`,`),制表符(`\t`),我们常称它们为 CSV 文件和 TSV 文件。 10 | 11 | ### CSV 12 | 13 | CSV 文件常以 `.csv` 作为文件拓展名,比如接下来我们会导入的 `mtcars.csv`。注意,文件拓展名并不会影响文件本身的内容,它只是用来方便帮助人们快速的了解内容格式,另外支持其他一些程序的自动解读(在你的计算机上,不同的文件拓展名系统软件可以会对它们使用不同的图标,如 Word 文档和 PPT)。 14 | 15 | 一般规整的数据以行作为观测,列作为记录(变量、域),如一个班级同学的成绩。 16 | 17 | ``` 18 | student,chinese,math,english 19 | stu1,99,100,98 20 | stu2,60,50,88 21 | ``` 22 | 23 | R 内置了 `read.table()` 函数用于便捷导入各类分隔符的文件。下面我们直接将这个成绩记录信息以文本的形式传入,结果会返回一个数据框: 24 | 25 | ```{r} 26 | stu <- read.table(text = " 27 | student,chinese,math,english 28 | stu1,99,100,98 29 | stu2,60,50,88 30 | ", header = TRUE, sep = ",") 31 | stu 32 | class(stu) 33 | ``` 34 | 35 | 实际上要处理的数据并不会这么的少,一般会以文件的形式存储在计算机磁盘中。下面我们依旧使用 `read.table()` 函数完成 CSV 文件数据的导入。 36 | 37 | 38 | ```{r} 39 | cars <- read.table(file = "data/data-import/mtcars.csv", header = TRUE, sep = ",") 40 | ``` 41 | 42 | 操作完成了,我们检查下头几行: 43 | 44 | ```{r} 45 | head(cars) 46 | ``` 47 | 48 | 除了使用 `read.table()`,我们还可以使用内置的 `read.csv()` 函数完成 CSV 文件的读入。与 `read.table()` 不同的时,我们无需再指定分隔符,因为该函数本身就是为了 CSV 文件设计的。另外函数默认将 `header` 选项设定为 `TRUE`,即有列名,所以我们也无需指定它,因而读取操作就被简化了: 49 | 50 | ```{r} 51 | cars2 <- read.csv(file = "data/data-import/mtcars.csv") 52 | head(cars2) 53 | ``` 54 | 55 | 上述的读取操作基于 R 内置的函数实现,无需载入任何其他三方包就可以完成数据的读入,这在针对小型数据(集)或者计算机条件受限时(无法安装和使用三方包)非常有用。在这种常规以符号分隔的文件数据读取方面,我必须提及 2 个三方包:**readr** 和 **data.table**。它们都能以极快的速度读取大内存数据,推荐读者作为常规导入操作的解决方案。 56 | 57 | [**tidyverse**](https://github.com/tidyverse/) 是 R 语言大神 [Hadley](https://github.com/hadley)(以 **ggplot2** 作者闻名于世) 组织构建的一整套数据分析生态系统,包括读入、处理、建模与可视化等, [**readr**](https://github.com/tidyverse/readr) 包是 **tidyverse** 的一部分,用于完成数据的导入工作。 58 | 59 | [**data.table**](https://github.com/Rdatatable/data.table) 包以 R 社区最快的数据读取和处理操作而著名,它主要是提供了一个增强版的数据框 `data.table`。 60 | 61 | 根据 **readr** 包官方文档介绍,**readr** 包通常比 **data.table** 包慢大概 1.2~2 倍左右。不过它们各有特点,**readr** 包被设计用于更为常规的数据读取操作,而 **data.table** 的目标则是尽量的快。 62 | 63 | 为了体现上述两个包和内置函数的差别,这里我们构造一个较大的 CSV 文件进行简单的测试: 64 | 65 | ```{r} 66 | huge_car <- cars[rep(1:32, 10000), ] 67 | ``` 68 | 69 | 把这个数据先保存下来,然后再利用不同的工具进行导入。 70 | 71 | ```{r} 72 | temp_csv <- tempfile(fileext = ".csv") 73 | readr::write_csv(huge_car, path = temp_csv) 74 | ``` 75 | 76 | 现在我们分别使用 `system.time()` 测试下 R 内置的 `read.csv()` 函数与 **readr** 提供的 `read_csv()` 以及 **data.table** 提供的 `fread()` 的读取效率。 77 | 78 | ```{r} 79 | time1 <- system.time( 80 | z1 <- read.csv(temp_csv) 81 | ) 82 | time1 83 | ``` 84 | 85 | ```{r, message=FALSE} 86 | library(readr) 87 | 88 | time2 <- system.time( 89 | z2 <- read_csv(temp_csv) 90 | ) 91 | time2 92 | ``` 93 | 94 | ```{r, message=FALSE} 95 | library(data.table) 96 | 97 | time3 <- system.time( 98 | z3 <- fread(temp_csv) 99 | ) 100 | time3 101 | ``` 102 | 103 | 上面我们使用了 `r nrow(huge_car)` 行数据进行测试,在我的计算机上,内置的函数 `read.csv()` 总共花费了 `r time1[3]`s,**readr** 的 `read_csv()` 花费了 `r time2[3]`s,而 **data.table** 的 `fread()` 仅用了 `r time3[3]`s 左右。**好的、适合的工具可以帮助我们极大地提升工作效率**。 104 | 105 | 如果我们进一步观察几种不同方式导入的数据格式,就会发现它们有些不太相同。 106 | 107 | ```{r} 108 | z1 109 | z2 110 | z3 111 | ``` 112 | 113 | 这个所谓的不同与结构中存储的数据信息无关,而是在不同的设计上。我们检查一下它们的类属性: 114 | 115 | ```{r} 116 | class(z1) 117 | class(z2) 118 | class(z3) 119 | ``` 120 | 121 | 不难看到这 3 个对象存在共有的类名 `data.frame`。我们使用内置函数读入数据仅包含该类名,而后两者还存在其他的类名,这是因为后两者继承了 `data.frame`。简单地说,后两者是增强版的 `data.frame`,它们赋予了不同于 R 内置数据框的特性,读者可以观察到的最明显的区别就是它们打印信息的方式不同。 122 | 123 | 通常地说,我们将对象 `z2` 称为 `tibble`,因为它是由 [**tibble**](https://github.com/tidyverse/tibble) 包创建的类,是 **tidyverse** 系列包的数据结构基础,设计者称它为现代的 `data.frame`,在基础的使用方式上与 `data.frame` 并无不同,读者可以通过官方文档阅读更为详细的介绍。 124 | 125 | 对象 `z3` 则常被称为 `data.table`,因为它的类名和包名都是 `data.table`. 126 | 127 | `tibble` 和 `data.table` 都有一系列强大高效的数据处理方法和操作,它们将在第 \@ref(clean) 章进行介绍。 128 | 129 | ### TSV 与其他 CSV 变体 130 | 131 | 另一种流行的数据存储格式是 TSV,与 CSV 唯一不同的是 TSV 使用制表符 `\t` 作为内容的分隔符。TSV 文件除了以 `.tsv` 作为文件拓展名,也常用 `.txt` 作为文件拓展名(并不是所有的 `.txt` 文件都是以制表符分隔)。 132 | 133 | 通过将 `read.table()` 函数中的 `sep` 参数设定为制表符,我们可以轻松完成该格式文件内容的读取: 134 | 135 | ```{r} 136 | mt <- read.table("data/data-import/mtcars.tsv", sep = "\t", header = TRUE) 137 | mt 138 | ``` 139 | 140 | 而 **readr** 包提供了一系列的 `read_*()` 函数,方便用户将常见数据文件导入为 `tibble`: 141 | 142 | ```{r} 143 | mt2 <- read_tsv("data/data-import/mtcars.tsv") 144 | mt2 145 | ``` 146 | 147 | 使用 **data.table** 则更为轻松,因为所有分隔格式的数据都可以通过 `fread()` 读取完成: 148 | 149 | ```{r} 150 | mt3 <- fread("data/data-import/mtcars.tsv") 151 | mt3 152 | ``` 153 | 154 | 为什么 `fread()` 没有设置分隔符却可以导入 CSV 和 TSV 文件?其中的巧妙在于该函数的内部会自动检测数据文件内的分隔符号。 155 | 156 | 通过查看该函数的参数,我们可以看到 `sep = "auto"`,这个参数我们可以人为指定,以适应不同的需求场景。 157 | 158 | ```{r} 159 | args(fread) 160 | ``` 161 | 162 | 上面展示出 `fread()` 存在众多的参数设置。R 的易用与友好在于此,众多 R 包提供的函数大多已经为用户恰当地设置好了默认选项,用户无需经过繁琐的操作即可快速获取所需的结果。 163 | 164 | 在学习了如何导入 TSV 文件后,我们应该能够归纳出 CSV、TSV 本质上是一类数据格式文件。例如,我们也可以使用分号 `;`(西方不少国家的逗号分隔文件就是以分号分隔的文件)。我们统一以 CSV 作为代表,将其他类似格式文件称为 CSV 变体。 165 | 166 | ## Excel 167 | 168 | **Excel** 是知名的微软 Office 套件之一,提供了日常的表格数据处理方案。尽管大多数数据分析人员不会产生和不想处理 Excel 文件,但由于它随着微软系统的流行而被广泛使用,因此读入 Excel 中的数据成为数据处理无法避免的日常工作任务之一。 169 | 170 | R 中有诸多拓展包可以导入 Excel 中的数据,其中最为推荐的就是本部分介绍的 [**readxl**](https://github.com/tidyverse/readxl) 包。 171 | 172 | 使用该包,导入 Excel 中的数据可以像读入 CSV 文件一样简单。 173 | 174 | ```{r} 175 | library(readxl) 176 | mt_excel <- read_excel("data/data-import/mtcars.xlsx") 177 | head(mt_excel) 178 | ``` 179 | 180 | `read_excel()` 同时支持 `.xls` 和 `xlsx` 两者文件格式。 181 | 182 | Excel 文件支持多个表格(Sheet),这种情况下默认第 1 个 Sheet 的内容会被读入。通过简单的设置,读者也可以轻松导入任意 Sheet 的内容。 183 | 184 | 下面通过 **readxl** 包的官方示例展示这一过程。 185 | 186 | 我们先查看下该包提供的示例数据: 187 | 188 | ```{r} 189 | readxl_example() 190 | ``` 191 | 192 | 选一个文件并查看其所在的路径: 193 | 194 | ```{r} 195 | readxl_example("datasets.xlsx") 196 | # 将路径保存备用 197 | excel_path <- readxl_example("datasets.xlsx") 198 | ``` 199 | 200 | 如果将路径传入 `excel_sheets()` 函数,读者可以获取文件中存在的 Sheets。 201 | 202 | ```{r} 203 | excel_sheets(excel_path) 204 | ``` 205 | 206 | 然后依旧是使用 `read_excel()` 函数,设定想要读入的 Sheet 名字即可。 207 | 208 | ```{r} 209 | iris <- read_excel(excel_path, sheet = "iris") 210 | head(iris) 211 | ``` 212 | 213 | 关于函数的更多用法读者可以通过 `?read_excel()` 查看。 214 | 215 | 有时候人们会在同一个 Excel Sheet 中放置多个表格信息,这种情况无法通过简单地使用 **readxl** 包读入数据。只要数据是有规律的,读者可以尝试下通过 [**tidycells**](https://github.com/r-rudra/tidycells) 包导入数据。 216 | 217 | ```{r, echo=FALSE, fig.cap="tidycells 包示例数据格式", fig.align="center"} 218 | knitr::include_graphics("https://github.com/r-rudra/tidycells/raw/master/vignettes/ext/marks.png") 219 | ``` 220 | 221 | 具体的使用请读者参照 [README](https://github.com/r-rudra/tidycells/blob/master/README.md) 进行学习。 222 | 223 | ## JSON 224 | 225 | JSON 是目前使用最广泛的网络数据交换格式,它非常轻量,易于阅读和编写。与 R 不同,它有自己的数据结构存储数值、字符串、布尔值、数组等信息。关于它的介绍请读者阅读[官网文档](https://www.json.org/json-zh.html),这里我们仅介绍如何将其导入 R 中以便于分析。 226 | 227 | R 中最流行的 JSON 数据解析包是 **jsonlite**,它非常轻量,但功能强大:支持 JSON 格式数据的解析以及将 R 中的数据结构编码为 JSON 格式。 228 | 229 | 下面的例子展示了 R 中常见数据结构解析为 JSON 格式后的结果: 230 | 231 | ```{r} 232 | jsonlite::toJSON(letters) 233 | jsonlite::toJSON(c(a = 1L, b = 2.0)) 234 | jsonlite::toJSON(data.frame(a = 1:3, b = 2:4)) 235 | jsonlite::toJSON(list(a = 1L, b = 2:5, c = c(TRUE, FALSE), d = NULL)) 236 | 237 | ``` 238 | 239 | 使用 `write_json()` 函数即可将 JSON 数据保存到本地: 240 | 241 | ```{r} 242 | jsonlite::write_json(list(a = 1L, b = 2:5, c = c(TRUE, FALSE), d = NULL), 243 | path = "data/data-import/example.json") 244 | ``` 245 | 246 | 有了示例的 JSON 文件,接下来我们看如何将其导入 R 中: 247 | 248 | ```{r} 249 | jsonlite::read_json("data/data-import/example.json") 250 | ``` 251 | 252 | 不难发现,JSON 文件中的信息被保存到了列表之中,但结构过于复杂,我们可以用 `simplifyVector` 选项进行简化: 253 | 254 | ```{r} 255 | jsonlite::read_json("data/data-import/example.json", simplifyVector = TRUE) 256 | ``` 257 | 258 | 如果 JSON 文件结构非常复杂,那么导入 R 以后形成的列表也会极其复杂,读者需要良好的列表处理功底才能进行处理。 259 | 260 | ## R 数据文件 261 | 262 | 使用 R 自身提供的数据存储格式是一种有效且常见的方式,非常适合数据分析项目。相比于通用格式如 CSV,它可以避免外部程序的删改,且有效利用磁盘存储空间。 263 | R 内置的格式有 RData 和 RDS 两种。 264 | 265 | ### RData 266 | 267 | RData 可能是读者最常见的一种 R 存储格式,它用于保存用户空间一个或多个对象。存储时使用 `save()` 函数,导入时使用 `load()` 函数。 268 | 269 | ```{r} 270 | ## 清除空间所有对象 271 | rm(list = ls()) 272 | ## 创建两个对象 273 | d1 <- mtcars 274 | d2 <- mtcars 275 | ls() 276 | ``` 277 | 278 | 上面我们在空间创建了 2 个示例对象,我们将其保存如下: 279 | 280 | ```{r} 281 | save(d1, d2, file = "data/data-import/mtcars.RData") 282 | ``` 283 | 284 | 285 | 这里注意一定要写明 `file = `,这是一个新手常犯的错误,如果不指明参数名称,路径将被看作一个对象,这会导致报错: 286 | 287 | 288 | ```{r, error=TRUE} 289 | save(d1, d2, "data/data-import/mtcars.RData") 290 | ``` 291 | 292 | 导入时这一操作则是非必需的: 293 | 294 | ```{r} 295 | rm(list = ls()) 296 | 297 | load("data/data-import/mtcars.RData") 298 | 299 | ls() 300 | ``` 301 | 302 | 这里我们之前保存的两个对象都会被直接导入环境空间,对象名称也不会改变。如果读者想要迅速保存当前环境所有的对象,有效的方式是使用 `save.image()` 函数。 303 | 304 | ### RDS 305 | 306 | 有时候我们想要保存单个的对象,且在导入时可以直接重命名该对象,这时我们可以用 RDS 格式。存储时使用 `saveRDS()` 函数,导入时使用 `readRDS()` 函数。 307 | 308 | ```{r} 309 | saveRDS(mtcars, file = "data/data-import/mtcars.rds") 310 | mtcars_rename <- readRDS("data/data-import/mtcars.rds") 311 | 312 | head(mtcars_rename) 313 | ``` 314 | 315 | 316 | ## 常见问题与方案 317 | 318 | 除了本节目前罗列的问题,读者在学习本章内容时遇到的其他问题都可以通过 [GitHub Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 提出和进行讨论。如果读者提出的是通性问题,将增补到该节。 319 | 320 | ### 通过键盘和剪贴板载入数据 321 | 322 | 如果读者想要从键盘读取数据,我们需要使用到标准输入 `stdin()` 函数进行辅助。 323 | 324 | ```{r, eval=FALSE} 325 | data <- read.table(stdin()) 326 | ## 运行后输入文本 327 | ``` 328 | 329 | 这种操作并不好用,稍微好一点的是先将数据拷贝到剪切板,然后使用下面的命令读入: 330 | 331 | ```{r, eval=FALSE} 332 | data <- read.table('clipboard', header=TRUE) 333 | ``` 334 | 335 | 336 | ### 逐行读取数据 337 | 338 | 如果读者想要简单地从用户输入交互地读取一些信息,`readline()` 是一个好办法,例如: 339 | 340 | ```r 341 | > readline("输入年龄:") 342 | 输入年龄:22 343 | [1] "22" 344 | ``` 345 | 346 | 如果是想要将文件的内容按行的方式读取进来,需要使用 `readLines()` 函数。 347 | 348 | 下面是一个来自 `readLines()` 的官方示例: 349 | 350 | ```{r} 351 | fil <- tempfile(fileext = ".data") 352 | cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = fil, 353 | sep = "\n") 354 | readLines(fil, n = -1) 355 | unlink(fil) # 清理文件 356 | ``` 357 | 358 | ### 读取等宽格式数据 359 | 360 | 有时候我们会碰到数据的列宽度是固定的,我们称它为定宽文本文件,像下面这样: 361 | 362 | ``` 363 | First Last Sex Number 364 | Currer Bell F 2 365 | Dr. Seuss M 49 366 | "" Student NA 21 367 | ``` 368 | 369 | 导入的方法这里推荐使用 2 种: 370 | 371 | 1. R 内置的 `read.fwf()` 函数。 372 | 2. **readr** 包提供的 `read_fwf()`。 373 | 374 | 详细的使用方法请读者参考文档学习。 375 | -------------------------------------------------------------------------------- /04-data-clean.Rmd: -------------------------------------------------------------------------------- 1 | # 数据清洗 {#clean} 2 | 3 | 数据的清洗是数据分析最核心的技能,读者需要长时间的学习和实战方能掌握。复杂的操作往往由基础的操作堆砌组合而成,本章的重点就是帮助读者快速学习和掌握数据清理最核心的操作单元。我将逐步讲解如何处理向量、单个数据集、两个数据集和多个数据集最基础且实用的数据操作方式,其他相关的内容将放到本章**【常见问题与方案】**一节作为补充介绍。。 4 | 5 | R 丰富的特性支持以多种方式完成相同的操作,对于大部分的操作,我将介绍 3 种不同的解决方案: 6 | 7 | 1. tidyverse 方式。 8 | 2. R 内置操作方式。 9 | 3. data.table 方式。 10 | 11 | 读者可以根据自己的需求选择性的学习,也可以进行全面的了解和学习。**tidyverse** 方式目前已经成为 R 处理数据的主流方案,我推荐读者重点掌握。注意,读者切勿画地为牢,数据分析重在**解决**,而不在于**手段**,实际处理数据时应当灵活使用可以运用的工具方法,上述 3 种方式有时也可以有机地组合运用,并非相互排斥。 12 | 13 | ## 向量 14 | 15 | ## 单个数据集 16 | 17 | ### 选择列 18 | 19 | ### 过滤行 20 | 21 | ### 新增列 22 | 23 | ### 聚合运算 24 | 25 | ### 分组计算 26 | 27 | ### 缺失值填充 28 | 29 | ## 两个数据集 30 | 31 | ## 多个数据集 32 | 33 | ## 常见问题与方案 34 | 35 | 除了本节目前罗列的问题,读者在学习本章内容时遇到的其他问题都可以通过 [GitHub Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 提出和进行讨论。如果读者提出的是通性问题,将增补到该节。 -------------------------------------------------------------------------------- /05-data-visualization.Rmd: -------------------------------------------------------------------------------- 1 | # 数据可视化 {#visualization} 2 | 3 | 瞎扯几句。 4 | 5 | ## 常见问题与方案 {-} 6 | 7 | 除了本节目前罗列的问题,读者在学习本章内容时遇到的其他问题都可以通过 [GitHub Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 提出和进行讨论。如果读者提出的是通性问题,将增补到该节。 -------------------------------------------------------------------------------- /06-data-model.Rmd: -------------------------------------------------------------------------------- 1 | # 统计建模 {#model} 2 | 3 | 瞎扯几句。 4 | 5 | ## 常见问题与方案 {-} 6 | 7 | 除了本节目前罗列的问题,读者在学习本章内容时遇到的其他问题都可以通过 [GitHub Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 提出和进行讨论。如果读者提出的是通性问题,将增补到该节。 -------------------------------------------------------------------------------- /07-result-report.Rmd: -------------------------------------------------------------------------------- 1 | # 结果展示 {#report} 2 | 3 | 瞎扯几句。 4 | 5 | ## 图形 6 | 7 | ## 表格 8 | 9 | ### Excel 10 | 11 | https://github.com/ropensci/writexl 12 | 13 | https://github.com/ycphs/openxlsx 14 | 15 | ## RMarkdown 16 | 17 | ## Shiny 18 | 19 | ## 常见问题与方案 20 | 21 | 除了本节目前罗列的问题,读者在学习本章内容时遇到的其他问题都可以通过 [GitHub Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 提出和进行讨论。如果读者提出的是通性问题,将增补到该节。 22 | 23 | ### export 包:集才华与一身 24 | 25 | https://github.com/tomwenseleers/export -------------------------------------------------------------------------------- /08-bio-application.Rmd: -------------------------------------------------------------------------------- 1 | # 生物信息学应用 {#bioapp} 2 | 3 | 瞎扯几句。 4 | 5 | -------------------------------------------------------------------------------- /09-supp.Rmd: -------------------------------------------------------------------------------- 1 | \cleardoublepage 2 | 3 | # (APPENDIX) 附录 {-} 4 | 5 | # 速查表 {#search-table} 6 | 7 | ## 数值计算函数 8 | 9 | ## 模型符号与函数 10 | 11 | ## 统计分析与检验 12 | 13 | # 拓展阅读 {#expand-reading} 14 | 15 | 呐,到这里本教程的内容差不多写完了。下面是一些推荐的学习资料,希望能够帮到想要进一步学习的读者。 16 | 大部分的资料是英文的,大家一定要根据自己的实际情况选择合适的资料,建立切实可行的学习计划。 17 | 18 | ## 生信技能树语雀知识库 19 | 20 | - [生物信息入门需要了解的十个专题](https://www.yuque.com/biotrainee/org3dm) 21 | - [生物统计从理论到实践](https://www.yuque.com/biotrainee/biostat) 22 | - [肿瘤外显子数据分析指南](https://www.yuque.com/biotrainee/wes) 23 | - [TCGA数据分析指南](https://www.yuque.com/biotrainee/tcga) 24 | - [生信菜鸟团-每周文献分享](https://www.yuque.com/biotrainee/weeklypaper) 25 | 26 | ## 图书 27 | 28 | ### 问题与方案 29 | 30 | - [Cookbook for R 中文版](https://openbiox.github.io/Cookbook-for-R-Chinese/) 31 | 32 | ### 统计建模 33 | 34 | - [Data Science Live Book](https://livebook.datascienceheroes.com/) 35 | - [Statistical Inference via Data Science - A ModernDive into R and the tidyverse](https://moderndive.com/) 36 | 37 | ### 核心集合 38 | 39 | - [R for Data Science](https://r4ds.had.co.nz/) 40 | - [Anvanced R](https://adv-r.hadley.nz/) 41 | - [R Packages](https://r-pkgs.org/) 42 | 43 | ### 生物信息学 44 | 45 | - [生物信息学生 R 入门教程](http://qiubio.com/new/book/) 46 | 47 | ### R 48 | 49 | - [R 语言教程](http://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/index.html) 50 | - [统计计算](http://www.math.pku.edu.cn/teachers/lidf/docs/statcomp/html/_statcompbook/index.html) 51 | 52 | ## 视频 53 | 54 | - [生信技能树 B 站视频集](https://space.bilibili.com/338686099) 55 | 56 | ## 公众号 57 | 58 | - 生信菜鸟团 59 | - 生信技能树 60 | - biobabble 61 | - 优雅R 62 | 63 | ## 其他资料 64 | 65 | - [Jimmy - GEO 流程](https://github.com/jmzeng1314/GEO) 66 | - [Jimmy - NGS 流程](https://github.com/jmzeng1314/NGS-pipeline) -------------------------------------------------------------------------------- /10-references.Rmd: -------------------------------------------------------------------------------- 1 | `r if (knitr:::is_html_output()) '# 参考文献 {#references .unnumbered}'` 2 | 3 | ```{r include=FALSE} 4 | # 自动生成 R 包的参考文献 5 | knitr::write_bib(c( 6 | .packages(), 'bookdown', 'knitr', 'rmarkdown' 7 | ), 'packages.bib') 8 | ``` 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 极客R:数据分析之道 2 | 3 | [![HitCount](http://hits.dwyl.com/ShixiangWang/geek-r-tutorial.svg)](http://hits.dwyl.com/ShixiangWang/geek-r-tutorial) 4 | 5 | [GitHub 仓库](https://github.com/ShixiangWang/geek-r-tutorial) | [Gitee 仓库](https://gitee.com/ShixiangWang/geek-r-tutorial) 6 | 7 | 这是生信技能树的 R 入门教程,由 [**bookdown**](https://bookdown.org/yihui/bookdown) 驱动编写而成。 8 | 9 | - 阅读请点击:,Gitee 有一个镜像 ,对国内读者更加友好。 10 | - 问题和交流请通过 [issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 进行。 11 | 12 | # 许可 13 | 14 | 本网站(永久)免费阅读,以 [CC 署名-非商业性使用-禁止演绎 4.0 国际协议](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh)发行。 15 | 16 | Copyright © 2020 王诗翔 17 | 18 | 19 | -------------------------------------------------------------------------------- /_bookdown.yml: -------------------------------------------------------------------------------- 1 | book_filename: bookdown 2 | clean: [packages.bib, bookdown.bbl] 3 | language: 4 | label: 5 | fig: "图 " 6 | tab: "表 " 7 | ui: 8 | edit: "编辑" 9 | chapter_name: ["第 ", " 章"] 10 | 11 | before_chapter_script: "_common.R" -------------------------------------------------------------------------------- /_common.R: -------------------------------------------------------------------------------- 1 | knitr::opts_chunk$set( 2 | fig.align = 'center', 3 | fig.width = 4, 4 | fig.height = 4, 5 | collapse = TRUE, 6 | comment = "#>", 7 | message = FALSE 8 | ) -------------------------------------------------------------------------------- /_output.yml: -------------------------------------------------------------------------------- 1 | bookdown::gitbook: 2 | css: css/style.css 3 | config: 4 | toc: 5 | collapse: section 6 | before: | 7 |
  • 数据分析之道
  • 8 | # download: [pdf, epub] 9 | edit: https://github.com/ShixiangWang/geek-r-tutorial/edit/master/%s 10 | sharing: 11 | github: yes 12 | twitter: yes 13 | bookdown::pdf_book: 14 | includes: 15 | in_header: latex/preamble.tex 16 | before_body: latex/before_body.tex 17 | after_body: latex/after_body.tex 18 | keep_tex: yes 19 | dev: "cairo_pdf" 20 | latex_engine: xelatex 21 | citation_package: natbib 22 | template: latex/template.tex 23 | pandoc_args: "--top-level-division=chapter" 24 | toc_depth: 3 25 | toc_unnumbered: no 26 | toc_appendix: yes 27 | quote_footer: ["\\begin{flushright}", "\\end{flushright}"] 28 | bookdown::epub_book: 29 | stylesheet: css/style.css 30 | -------------------------------------------------------------------------------- /book.bib: -------------------------------------------------------------------------------- 1 | @Book{xie2015, 2 | title = {Dynamic Documents with {R} and knitr}, 3 | author = {Yihui Xie}, 4 | publisher = {Chapman and Hall/CRC}, 5 | address = {Boca Raton, Florida}, 6 | year = {2015}, 7 | edition = {2nd}, 8 | note = {ISBN 978-1498716963}, 9 | url = {http://yihui.name/knitr/}, 10 | } 11 | -------------------------------------------------------------------------------- /bookdown.log: -------------------------------------------------------------------------------- 1 | This is XeTeX, Version 3.14159265-2.6-0.999991 (TeX Live 2019/W32TeX) (preloaded format=xelatex 2019.12.14) 6 MAR 2020 17:56 2 | entering extended mode 3 | restricted \write18 enabled. 4 | %&-line parsing enabled. 5 | **bookdown.tex 6 | (./bookdown.tex 7 | LaTeX2e <2019-10-01> patch level 3 8 | 9 | ! LaTeX Error: File `ctexbook.cls' not found. 10 | 11 | Type X to quit or to proceed, 12 | or enter new name. (Default extension: cls) 13 | 14 | Enter file name: 15 | ! Emergency stop. 16 | 17 | 18 | l.2 \usepackage 19 | {lmodern}^^M 20 | Here is how much of TeX's memory you used: 21 | 10 strings out of 494458 22 | 119 string characters out of 6171841 23 | 58050 words of memory out of 5000000 24 | 4470 multiletter control sequences out of 15000+600000 25 | 3640 words of font info for 14 fonts, out of 8000000 for 9000 26 | 14 hyphenation exceptions out of 8191 27 | 14i,0n,8p,53b,10s stack positions out of 5000i,500n,10000p,200000b,80000s 28 | 29 | No pages of output. 30 | 31 | 32 | ! kpathsea: Running mktexfmt xelatex.fmt 33 | 34 | ! The command name is C:\Users\Shixiang\AppData\Roaming\TinyTeX\bin\win32\mktexfmt 35 | -------------------------------------------------------------------------------- /bookdown.tex: -------------------------------------------------------------------------------- 1 | \documentclass[]{ctexbook} 2 | \usepackage{lmodern} 3 | \usepackage{amssymb,amsmath} 4 | \usepackage{ifxetex,ifluatex} 5 | \usepackage{fixltx2e} % provides \textsubscript 6 | \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex 7 | \usepackage[T1]{fontenc} 8 | \usepackage[utf8]{inputenc} 9 | \else % if luatex or xelatex 10 | \ifxetex 11 | \usepackage{xltxtra,xunicode} 12 | \else 13 | \usepackage{fontspec} 14 | \fi 15 | \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase} 16 | \fi 17 | % use upquote if available, for straight quotes in verbatim environments 18 | \IfFileExists{upquote.sty}{\usepackage{upquote}}{} 19 | % use microtype if available 20 | \IfFileExists{microtype.sty}{% 21 | \usepackage{microtype} 22 | \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts 23 | }{} 24 | \usepackage[b5paper,tmargin=2.5cm,bmargin=2.5cm,lmargin=3.5cm,rmargin=2.5cm]{geometry} 25 | \usepackage[unicode=true]{hyperref} 26 | \PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref 27 | \hypersetup{ 28 | pdftitle={麻麻说我们可以用 R Markdown 写书了}, 29 | pdfauthor={张三}, 30 | colorlinks=true, 31 | linkcolor=Maroon, 32 | citecolor=Blue, 33 | urlcolor=Blue, 34 | breaklinks=true} 35 | \urlstyle{same} % don't use monospace font for urls 36 | \usepackage{natbib} 37 | \bibliographystyle{apalike} 38 | \usepackage{color} 39 | \usepackage{fancyvrb} 40 | \newcommand{\VerbBar}{|} 41 | \newcommand{\VERB}{\Verb[commandchars=\\\{\}]} 42 | \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} 43 | % Add ',fontsize=\small' for more characters per line 44 | \usepackage{framed} 45 | \definecolor{shadecolor}{RGB}{248,248,248} 46 | \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}} 47 | \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}} 48 | \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} 49 | \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}} 50 | \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} 51 | \newcommand{\BuiltInTok}[1]{#1} 52 | \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} 53 | \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}} 54 | \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} 55 | \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} 56 | \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} 57 | \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}} 58 | \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} 59 | \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} 60 | \newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}} 61 | \newcommand{\ExtensionTok}[1]{#1} 62 | \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} 63 | \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} 64 | \newcommand{\ImportTok}[1]{#1} 65 | \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} 66 | \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} 67 | \newcommand{\NormalTok}[1]{#1} 68 | \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}} 69 | \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}} 70 | \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}} 71 | \newcommand{\RegionMarkerTok}[1]{#1} 72 | \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} 73 | \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} 74 | \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} 75 | \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} 76 | \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} 77 | \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} 78 | \usepackage{longtable,booktabs} 79 | % Fix footnotes in tables (requires footnote package) 80 | \IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{long table}}{} 81 | \IfFileExists{parskip.sty}{% 82 | \usepackage{parskip} 83 | }{% else 84 | \setlength{\parindent}{0pt} 85 | \setlength{\parskip}{6pt plus 2pt minus 1pt} 86 | } 87 | \setlength{\emergencystretch}{3em} % prevent overfull lines 88 | \providecommand{\tightlist}{% 89 | \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} 90 | \setcounter{secnumdepth}{5} 91 | % Redefines (sub)paragraphs to behave more like sections 92 | \ifx\paragraph\undefined\else 93 | \let\oldparagraph\paragraph 94 | \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}} 95 | \fi 96 | \ifx\subparagraph\undefined\else 97 | \let\oldsubparagraph\subparagraph 98 | \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}} 99 | \fi 100 | 101 | % set default figure placement to htbp 102 | \makeatletter 103 | \def\fps@figure{htbp} 104 | \makeatother 105 | 106 | \usepackage{booktabs} 107 | \usepackage{longtable} 108 | 109 | \usepackage{framed,color} 110 | \definecolor{shadecolor}{RGB}{248,248,248} 111 | 112 | \renewcommand{\textfraction}{0.05} 113 | \renewcommand{\topfraction}{0.8} 114 | \renewcommand{\bottomfraction}{0.8} 115 | \renewcommand{\floatpagefraction}{0.75} 116 | 117 | \let\oldhref\href 118 | \renewcommand{\href}[2]{#2\footnote{\url{#1}}} 119 | 120 | \makeatletter 121 | \newenvironment{kframe}{% 122 | \medskip{} 123 | \setlength{\fboxsep}{.8em} 124 | \def\at@end@of@kframe{}% 125 | \ifinner\ifhmode% 126 | \def\at@end@of@kframe{\end{minipage}}% 127 | \begin{minipage}{\columnwidth}% 128 | \fi\fi% 129 | \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep 130 | \colorbox{shadecolor}{##1}\hskip-\fboxsep 131 | % There is no \\@totalrightmargin, so: 132 | \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}% 133 | \MakeFramed {\advance\hsize-\width 134 | \@totalleftmargin\z@ \linewidth\hsize 135 | \@setminipage}}% 136 | {\par\unskip\endMakeFramed% 137 | \at@end@of@kframe} 138 | \makeatother 139 | 140 | \makeatletter 141 | \@ifundefined{Shaded}{ 142 | }{\renewenvironment{Shaded}{\begin{kframe}}{\end{kframe}}} 143 | \@ifpackageloaded{fancyvrb}{% 144 | % https://github.com/CTeX-org/ctex-kit/issues/331 145 | \RecustomVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},formatcom=\xeCJKVerbAddon}% 146 | }{} 147 | \makeatother 148 | 149 | \usepackage{makeidx} 150 | \makeindex 151 | 152 | \urlstyle{tt} 153 | 154 | \usepackage{amsthm} 155 | \makeatletter 156 | \def\thm@space@setup{% 157 | \thm@preskip=8pt plus 2pt minus 4pt 158 | \thm@postskip=\thm@preskip 159 | } 160 | \makeatother 161 | 162 | \frontmatter 163 | 164 | \title{麻麻说我们可以用 R Markdown 写书了} 165 | \author{张三} 166 | \date{2020-03-06} 167 | 168 | \let\BeginKnitrBlock\begin \let\EndKnitrBlock\end 169 | \begin{document} 170 | \maketitle 171 | 172 | 173 | \thispagestyle{empty} 174 | 175 | \begin{center} 176 | 献给…… 177 | 178 | 呃,爱谁谁吧 179 | \end{center} 180 | 181 | \setlength{\abovedisplayskip}{-5pt} 182 | \setlength{\abovedisplayshortskip}{-5pt} 183 | 184 | { 185 | \setcounter{tocdepth}{2} 186 | \tableofcontents 187 | } 188 | \listoftables 189 | \listoffigures 190 | \hypertarget{ux524dux8a00}{% 191 | \chapter*{前言}\label{ux524dux8a00}} 192 | 193 | 194 | 你好,世界。我写了一本书。这本书是这样的,第 \ref{intro} 章介绍了啥啥,第 \ref{wind} 章说了啥啥,然后是啥啥\ldots\ldots{} 195 | 196 | 我用了两个 R 包编译这本书,分别是 \textbf{knitr}\index{knitr} \citep{xie2015} 和 \textbf{bookdown}\index{bookdown} \citep{R-bookdown}。以下是我的 R 进程信息: 197 | 198 | \begin{Shaded} 199 | \begin{Highlighting}[] 200 | \KeywordTok{sessionInfo}\NormalTok{()} 201 | \end{Highlighting} 202 | \end{Shaded} 203 | 204 | \begin{verbatim} 205 | ## R version 3.6.2 (2019-12-12) 206 | ## Platform: x86_64-w64-mingw32/x64 (64-bit) 207 | ## Running under: Windows 10 x64 (build 18362) 208 | ## 209 | ## Matrix products: default 210 | ## 211 | ## locale: 212 | ## [1] LC_COLLATE=Chinese (Simplified)_China.936 213 | ## [2] LC_CTYPE=Chinese (Simplified)_China.936 214 | ## [3] LC_MONETARY=Chinese (Simplified)_China.936 215 | ## [4] LC_NUMERIC=C 216 | ## [5] LC_TIME=Chinese (Simplified)_China.936 217 | ## 218 | ## attached base packages: 219 | ## [1] stats graphics grDevices utils datasets 220 | ## [6] methods base 221 | ## 222 | ## other attached packages: 223 | ## [1] pacman_0.5.1 224 | ## 225 | ## loaded via a namespace (and not attached): 226 | ## [1] Rcpp_1.0.3 bookdown_0.17 227 | ## [3] later_1.0.0 digest_0.6.25 228 | ## [5] mime_0.9 R6_2.4.1 229 | ## [7] jsonlite_1.6.1 magrittr_1.5.0.9000 230 | ## [9] evaluate_0.14 highr_0.8 231 | ## [11] rlang_0.4.4 stringi_1.4.6 232 | ## [13] promises_1.1.0 rstudioapi_0.11 233 | ## [15] rmarkdown_2.1 tools_3.6.2 234 | ## [17] servr_0.15 stringr_1.4.0 235 | ## [19] tinytex_0.20 httpuv_1.5.2 236 | ## [21] xfun_0.12 yaml_2.2.1 237 | ## [23] compiler_3.6.2 htmltools_0.4.0 238 | ## [25] knitr_1.28 239 | \end{verbatim} 240 | 241 | \hypertarget{ux81f4ux8c22}{% 242 | \section*{致谢}\label{ux81f4ux8c22}} 243 | 244 | 245 | 非常感谢谁谁以及谁谁对我的帮助。艾玛,要不是他们神一样的队友,我两年前就写完这本书了。 246 | 247 | \BeginKnitrBlock{flushright} 248 | 张三\\ 249 | 于 A 村某角落 250 | \EndKnitrBlock{flushright} 251 | 252 | \hypertarget{author}{% 253 | \chapter*{作者简介}\label{author}} 254 | 255 | 256 | 上不了厅堂,下得了厨房。敲得了代码,逮得住蟑螂。 257 | 258 | \mainmatter 259 | 260 | \hypertarget{intro}{% 261 | \chapter{牛刀小试}\label{intro}} 262 | 263 | 现在我们可以试试 \textbf{bookdown} 的一些初级功能了,例如图表。图 \ref{fig:hello} 是一幅无趣的散点图,表 \ref{tab:iris} 是一份枯燥的数据。 264 | 265 | \begin{Shaded} 266 | \begin{Highlighting}[] 267 | \KeywordTok{par}\NormalTok{(}\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{1}\NormalTok{, }\FloatTok{.1}\NormalTok{))} 268 | \KeywordTok{plot}\NormalTok{(cars, }\DataTypeTok{pch =} \DecValTok{19}\NormalTok{)} 269 | \end{Highlighting} 270 | \end{Shaded} 271 | 272 | \begin{figure} 273 | \includegraphics[width=0.9\linewidth]{bookdown_files/figure-latex/hello-1} \caption{雷猴啊,散点图!}\label{fig:hello} 274 | \end{figure} 275 | 276 | \begin{Shaded} 277 | \begin{Highlighting}[] 278 | \NormalTok{knitr}\OperatorTok{::}\KeywordTok{kable}\NormalTok{(} 279 | \KeywordTok{head}\NormalTok{(iris), }\DataTypeTok{caption =} \StringTok{\textquotesingle{}雷猴啊,iris 数据!\textquotesingle{}}\NormalTok{,} 280 | \DataTypeTok{booktabs =} \OtherTok{TRUE} 281 | \NormalTok{)} 282 | \end{Highlighting} 283 | \end{Shaded} 284 | 285 | \begin{table} 286 | 287 | \caption{\label{tab:iris}雷猴啊,iris 数据!} 288 | \centering 289 | \begin{tabular}[t]{rrrrl} 290 | \toprule 291 | Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\ 292 | \midrule 293 | 5.1 & 3.5 & 1.4 & 0.2 & setosa\\ 294 | 4.9 & 3.0 & 1.4 & 0.2 & setosa\\ 295 | 4.7 & 3.2 & 1.3 & 0.2 & setosa\\ 296 | 4.6 & 3.1 & 1.5 & 0.2 & setosa\\ 297 | 5.0 & 3.6 & 1.4 & 0.2 & setosa\\ 298 | \addlinespace 299 | 5.4 & 3.9 & 1.7 & 0.4 & setosa\\ 300 | \bottomrule 301 | \end{tabular} 302 | \end{table} 303 | 304 | 就这样,你可以一直编下去,直到编不下去。 305 | 306 | \hypertarget{wind}{% 307 | \chapter{白苹风末}\label{wind}} 308 | 309 | 瞎扯几句。 310 | 311 | \hypertarget{ux5f20ux8001ux7237ux5b50}{% 312 | \section{张老爷子}\label{ux5f20ux8001ux7237ux5b50}} 313 | 314 | 话说张老爷子写了一首诗: 315 | 316 | \begin{quote} 317 | 姑苏开遍碧桃时,邂逅河阳女画师。\\ 318 | 红豆江南留梦影,白苹风末唱秋词。 319 | \end{quote} 320 | 321 | \hypertarget{ux5f6dux5927ux5c06ux9886}{% 322 | \section{彭大将领}\label{ux5f6dux5927ux5c06ux9886}} 323 | 324 | 貌似大家都喜欢用白萍风这个意境。又如彭玉麟的对联: 325 | 326 | \begin{quote} 327 | 凭栏看云影波光,最好是红蓼花疏、白苹秋老;\\ 328 | 把酒对琼楼玉宇,莫孤负天心月到、水面风来。 329 | \end{quote} 330 | 331 | 嘿,玛尼玛尼哄。 332 | 333 | \cleardoublepage 334 | 335 | \hypertarget{appendix-ux9644ux5f55}{% 336 | \appendix \addcontentsline{toc}{chapter}{\appendixname}} 337 | 338 | 339 | \hypertarget{sound}{% 340 | \chapter{余音绕梁}\label{sound}} 341 | 342 | 呐,到这里朕的书差不多写完了,但还有几句话要交待,所以开个附录,再啰嗦几句,各位客官稍安勿躁、扶稳坐好。 343 | 344 | \bibliography{book.bib,packages.bib} 345 | 346 | \backmatter 347 | \printindex 348 | 349 | \end{document} 350 | -------------------------------------------------------------------------------- /cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/cover.png -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | p.caption { 2 | color: #777; 3 | margin-top: 10px; 4 | } 5 | p code { 6 | white-space: inherit; 7 | } 8 | pre { 9 | word-break: normal; 10 | word-wrap: normal; 11 | } 12 | pre code { 13 | white-space: inherit; 14 | } 15 | p.flushright { 16 | text-align: right; 17 | } 18 | blockquote > p:last-child { 19 | text-align: right; 20 | } 21 | blockquote > p:first-child { 22 | text-align: inherit; 23 | } 24 | -------------------------------------------------------------------------------- /data/data-import/example.json: -------------------------------------------------------------------------------- 1 | {"a":[1],"b":[2,3,4,5],"c":[true,false],"d":{}} 2 | -------------------------------------------------------------------------------- /data/data-import/mtcars.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/data/data-import/mtcars.RData -------------------------------------------------------------------------------- /data/data-import/mtcars.csv: -------------------------------------------------------------------------------- 1 | mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb 2 | 21,6,160,110,3.9,2.62,16.46,0,1,4,4 3 | 21,6,160,110,3.9,2.875,17.02,0,1,4,4 4 | 22.8,4,108,93,3.85,2.32,18.61,1,1,4,1 5 | 21.4,6,258,110,3.08,3.215,19.44,1,0,3,1 6 | 18.7,8,360,175,3.15,3.44,17.02,0,0,3,2 7 | 18.1,6,225,105,2.76,3.46,20.22,1,0,3,1 8 | 14.3,8,360,245,3.21,3.57,15.84,0,0,3,4 9 | 24.4,4,146.7,62,3.69,3.19,20,1,0,4,2 10 | 22.8,4,140.8,95,3.92,3.15,22.9,1,0,4,2 11 | 19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4 12 | 17.8,6,167.6,123,3.92,3.44,18.9,1,0,4,4 13 | 16.4,8,275.8,180,3.07,4.07,17.4,0,0,3,3 14 | 17.3,8,275.8,180,3.07,3.73,17.6,0,0,3,3 15 | 15.2,8,275.8,180,3.07,3.78,18,0,0,3,3 16 | 10.4,8,472,205,2.93,5.25,17.98,0,0,3,4 17 | 10.4,8,460,215,3,5.424,17.82,0,0,3,4 18 | 14.7,8,440,230,3.23,5.345,17.42,0,0,3,4 19 | 32.4,4,78.7,66,4.08,2.2,19.47,1,1,4,1 20 | 30.4,4,75.7,52,4.93,1.615,18.52,1,1,4,2 21 | 33.9,4,71.1,65,4.22,1.835,19.9,1,1,4,1 22 | 21.5,4,120.1,97,3.7,2.465,20.01,1,0,3,1 23 | 15.5,8,318,150,2.76,3.52,16.87,0,0,3,2 24 | 15.2,8,304,150,3.15,3.435,17.3,0,0,3,2 25 | 13.3,8,350,245,3.73,3.84,15.41,0,0,3,4 26 | 19.2,8,400,175,3.08,3.845,17.05,0,0,3,2 27 | 27.3,4,79,66,4.08,1.935,18.9,1,1,4,1 28 | 26,4,120.3,91,4.43,2.14,16.7,0,1,5,2 29 | 30.4,4,95.1,113,3.77,1.513,16.9,1,1,5,2 30 | 15.8,8,351,264,4.22,3.17,14.5,0,1,5,4 31 | 19.7,6,145,175,3.62,2.77,15.5,0,1,5,6 32 | 15,8,301,335,3.54,3.57,14.6,0,1,5,8 33 | 21.4,4,121,109,4.11,2.78,18.6,1,1,4,2 34 | -------------------------------------------------------------------------------- /data/data-import/mtcars.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/data/data-import/mtcars.rds -------------------------------------------------------------------------------- /data/data-import/mtcars.tsv: -------------------------------------------------------------------------------- 1 | mpg cyl disp hp drat wt qsec vs am gear carb 2 | 21 6 160 110 3.9 2.62 16.46 0 1 4 4 3 | 21 6 160 110 3.9 2.875 17.02 0 1 4 4 4 | 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 5 | 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 6 | 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2 7 | 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1 8 | 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4 9 | 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2 10 | 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2 11 | 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4 12 | 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4 13 | 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3 14 | 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3 15 | 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3 16 | 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4 17 | 10.4 8 460 215 3 5.424 17.82 0 0 3 4 18 | 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4 19 | 32.4 4 78.7 66 4.08 2.2 19.47 1 1 4 1 20 | 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 21 | 33.9 4 71.1 65 4.22 1.835 19.9 1 1 4 1 22 | 21.5 4 120.1 97 3.7 2.465 20.01 1 0 3 1 23 | 15.5 8 318 150 2.76 3.52 16.87 0 0 3 2 24 | 15.2 8 304 150 3.15 3.435 17.3 0 0 3 2 25 | 13.3 8 350 245 3.73 3.84 15.41 0 0 3 4 26 | 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2 27 | 27.3 4 79 66 4.08 1.935 18.9 1 1 4 1 28 | 26 4 120.3 91 4.43 2.14 16.7 0 1 5 2 29 | 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2 30 | 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4 31 | 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6 32 | 15 8 301 335 3.54 3.57 14.6 0 1 5 8 33 | 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2 34 | -------------------------------------------------------------------------------- /data/data-import/mtcars.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/data/data-import/mtcars.xlsx -------------------------------------------------------------------------------- /docs/author.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 作者简介 | 极客R:数据分析之道 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 |
    125 | 126 |
    127 | 258 |
    259 | 260 |
    261 |
    262 | 267 | 268 |
    269 |
    270 | 271 |
    272 |
    273 |

    作者简介

    274 |

    王诗翔,生信技能树成员,R 语言爱好者。对编程、数据分析都有比较浓厚的兴趣,喜欢开源与开放的科学。长期在简书发文,连载文集《极客RrR<<-数据分析之道》,并运营微信公众号《优雅R》用于推送数据分析相关知识。

    275 | 276 | 277 |
    278 |
    279 | 280 |
    281 |
    282 |
    283 | 284 | 285 |
    286 |
    287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 333 | 334 | 335 | 348 | 349 | 350 | 351 | -------------------------------------------------------------------------------- /docs/bioapp.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 第 8 章 生物信息学应用 | 极客R:数据分析之道 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 |
    125 | 126 |
    127 | 258 |
    259 | 260 |
    261 |
    262 | 267 | 268 |
    269 |
    270 | 271 |
    272 |
    273 |

    第 8 章 生物信息学应用

    274 |

    瞎扯几句。

    275 | 276 | 277 |
    278 | 279 | 280 | 281 |
    282 | 283 |
    284 |
    285 |
    286 | 287 | 288 |
    289 |
    290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 336 | 337 | 338 | 351 | 352 | 353 | 354 | -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-146-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-146-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-147-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-147-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-148-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-148-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-149-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-149-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-151-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-151-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-153-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-153-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-158-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-158-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/unnamed-chunk-159-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/unnamed-chunk-159-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/vector-add-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/vector-add-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/vector-align-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/vector-align-1.png -------------------------------------------------------------------------------- /docs/bookdown_files/figure-html/vector-construction-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/bookdown_files/figure-html/vector-construction-1.png -------------------------------------------------------------------------------- /docs/cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/cover.png -------------------------------------------------------------------------------- /docs/css/style.css: -------------------------------------------------------------------------------- 1 | p.caption { 2 | color: #777; 3 | margin-top: 10px; 4 | } 5 | p code { 6 | white-space: inherit; 7 | } 8 | pre { 9 | word-break: normal; 10 | word-wrap: normal; 11 | } 12 | pre code { 13 | white-space: inherit; 14 | } 15 | p.flushright { 16 | text-align: right; 17 | } 18 | blockquote > p:last-child { 19 | text-align: right; 20 | } 21 | blockquote > p:first-child { 22 | text-align: inherit; 23 | } 24 | -------------------------------------------------------------------------------- /docs/fig/ch00-data-science.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch00-data-science.png -------------------------------------------------------------------------------- /docs/fig/ch01-download-rstudio.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch01-download-rstudio.PNG -------------------------------------------------------------------------------- /docs/fig/ch01-rstudio-overview.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch01-rstudio-overview.PNG -------------------------------------------------------------------------------- /docs/fig/ch01-rtools-mac.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch01-rtools-mac.PNG -------------------------------------------------------------------------------- /docs/fig/ch01-rtools-win.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch01-rtools-win.PNG -------------------------------------------------------------------------------- /docs/fig/ch01-select-64-bit.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch01-select-64-bit.PNG -------------------------------------------------------------------------------- /docs/fig/ch01-set-install-path-win.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch01-set-install-path-win.PNG -------------------------------------------------------------------------------- /docs/fig/ch02-fun-env.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch02-fun-env.png -------------------------------------------------------------------------------- /docs/fig/ch02-rstudio-env-panel.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/fig/ch02-rstudio-env-panel.PNG -------------------------------------------------------------------------------- /docs/libs/gitbook/css/fontawesome/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/docs/libs/gitbook/css/fontawesome/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /docs/libs/gitbook/css/plugin-bookdown.css: -------------------------------------------------------------------------------- 1 | .book .book-header h1 { 2 | padding-left: 20px; 3 | padding-right: 20px; 4 | } 5 | .book .book-header.fixed { 6 | position: fixed; 7 | right: 0; 8 | top: 0; 9 | left: 0; 10 | border-bottom: 1px solid rgba(0,0,0,.07); 11 | } 12 | span.search-highlight { 13 | background-color: #ffff88; 14 | } 15 | @media (min-width: 600px) { 16 | .book.with-summary .book-header.fixed { 17 | left: 300px; 18 | } 19 | } 20 | @media (max-width: 1240px) { 21 | .book .book-body.fixed { 22 | top: 50px; 23 | } 24 | .book .book-body.fixed .body-inner { 25 | top: auto; 26 | } 27 | } 28 | @media (max-width: 600px) { 29 | .book.with-summary .book-header.fixed { 30 | left: calc(100% - 60px); 31 | min-width: 300px; 32 | } 33 | .book.with-summary .book-body { 34 | transform: none; 35 | left: calc(100% - 60px); 36 | min-width: 300px; 37 | } 38 | .book .book-body.fixed { 39 | top: 0; 40 | } 41 | } 42 | 43 | .book .book-body.fixed .body-inner { 44 | top: 50px; 45 | } 46 | .book .book-body .page-wrapper .page-inner section.normal sub, .book .book-body .page-wrapper .page-inner section.normal sup { 47 | font-size: 85%; 48 | } 49 | 50 | @media print { 51 | .book .book-summary, .book .book-body .book-header, .fa { 52 | display: none !important; 53 | } 54 | .book .book-body.fixed { 55 | left: 0px; 56 | } 57 | .book .book-body,.book .book-body .body-inner, .book.with-summary { 58 | overflow: visible !important; 59 | } 60 | } 61 | .kable_wrapper { 62 | border-spacing: 20px 0; 63 | border-collapse: separate; 64 | border: none; 65 | margin: auto; 66 | } 67 | .kable_wrapper > tbody > tr > td { 68 | vertical-align: top; 69 | } 70 | .book .book-body .page-wrapper .page-inner section.normal table tr.header { 71 | border-top-width: 2px; 72 | } 73 | .book .book-body .page-wrapper .page-inner section.normal table tr:last-child td { 74 | border-bottom-width: 2px; 75 | } 76 | .book .book-body .page-wrapper .page-inner section.normal table td, .book .book-body .page-wrapper .page-inner section.normal table th { 77 | border-left: none; 78 | border-right: none; 79 | } 80 | .book .book-body .page-wrapper .page-inner section.normal table.kable_wrapper > tbody > tr, .book .book-body .page-wrapper .page-inner section.normal table.kable_wrapper > tbody > tr > td { 81 | border-top: none; 82 | } 83 | .book .book-body .page-wrapper .page-inner section.normal table.kable_wrapper > tbody > tr:last-child > td { 84 | border-bottom: none; 85 | } 86 | 87 | div.theorem, div.lemma, div.corollary, div.proposition, div.conjecture { 88 | font-style: italic; 89 | } 90 | span.theorem, span.lemma, span.corollary, span.proposition, span.conjecture { 91 | font-style: normal; 92 | } 93 | div.proof:after { 94 | content: "\25a2"; 95 | float: right; 96 | } 97 | .header-section-number { 98 | padding-right: .5em; 99 | } 100 | -------------------------------------------------------------------------------- /docs/libs/gitbook/css/plugin-clipboard.css: -------------------------------------------------------------------------------- 1 | div.sourceCode { 2 | position: relative; 3 | } 4 | 5 | .copy-to-clipboard-button { 6 | position: absolute; 7 | right: 0; 8 | top: 0; 9 | visibility: hidden; 10 | } 11 | 12 | .copy-to-clipboard-button:focus { 13 | outline: 0; 14 | } 15 | 16 | div.sourceCode:hover > .copy-to-clipboard-button { 17 | visibility: visible; 18 | } 19 | -------------------------------------------------------------------------------- /docs/libs/gitbook/css/plugin-fontsettings.css: -------------------------------------------------------------------------------- 1 | /* 2 | * Theme 1 3 | */ 4 | .color-theme-1 .dropdown-menu { 5 | background-color: #111111; 6 | border-color: #7e888b; 7 | } 8 | .color-theme-1 .dropdown-menu .dropdown-caret .caret-inner { 9 | border-bottom: 9px solid #111111; 10 | } 11 | .color-theme-1 .dropdown-menu .buttons { 12 | border-color: #7e888b; 13 | } 14 | .color-theme-1 .dropdown-menu .button { 15 | color: #afa790; 16 | } 17 | .color-theme-1 .dropdown-menu .button:hover { 18 | color: #73553c; 19 | } 20 | /* 21 | * Theme 2 22 | */ 23 | .color-theme-2 .dropdown-menu { 24 | background-color: #2d3143; 25 | border-color: #272a3a; 26 | } 27 | .color-theme-2 .dropdown-menu .dropdown-caret .caret-inner { 28 | border-bottom: 9px solid #2d3143; 29 | } 30 | .color-theme-2 .dropdown-menu .buttons { 31 | border-color: #272a3a; 32 | } 33 | .color-theme-2 .dropdown-menu .button { 34 | color: #62677f; 35 | } 36 | .color-theme-2 .dropdown-menu .button:hover { 37 | color: #f4f4f5; 38 | } 39 | .book .book-header .font-settings .font-enlarge { 40 | line-height: 30px; 41 | font-size: 1.4em; 42 | } 43 | .book .book-header .font-settings .font-reduce { 44 | line-height: 30px; 45 | font-size: 1em; 46 | } 47 | .book.color-theme-1 .book-body { 48 | color: #704214; 49 | background: #f3eacb; 50 | } 51 | .book.color-theme-1 .book-body .page-wrapper .page-inner section { 52 | background: #f3eacb; 53 | } 54 | .book.color-theme-2 .book-body { 55 | color: #bdcadb; 56 | background: #1c1f2b; 57 | } 58 | .book.color-theme-2 .book-body .page-wrapper .page-inner section { 59 | background: #1c1f2b; 60 | } 61 | .book.font-size-0 .book-body .page-inner section { 62 | font-size: 1.2rem; 63 | } 64 | .book.font-size-1 .book-body .page-inner section { 65 | font-size: 1.4rem; 66 | } 67 | .book.font-size-2 .book-body .page-inner section { 68 | font-size: 1.6rem; 69 | } 70 | .book.font-size-3 .book-body .page-inner section { 71 | font-size: 2.2rem; 72 | } 73 | .book.font-size-4 .book-body .page-inner section { 74 | font-size: 4rem; 75 | } 76 | .book.font-family-0 { 77 | font-family: Georgia, serif; 78 | } 79 | .book.font-family-1 { 80 | font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 81 | } 82 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal { 83 | color: #704214; 84 | } 85 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal a { 86 | color: inherit; 87 | } 88 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h1, 89 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h2, 90 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h3, 91 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h4, 92 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h5, 93 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h6 { 94 | color: inherit; 95 | } 96 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h1, 97 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h2 { 98 | border-color: inherit; 99 | } 100 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal h6 { 101 | color: inherit; 102 | } 103 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal hr { 104 | background-color: inherit; 105 | } 106 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal blockquote { 107 | border-color: #c4b29f; 108 | opacity: 0.9; 109 | } 110 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre, 111 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code { 112 | background: #fdf6e3; 113 | color: #657b83; 114 | border-color: #f8df9c; 115 | } 116 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal .highlight { 117 | background-color: inherit; 118 | } 119 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal table th, 120 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal table td { 121 | border-color: #f5d06c; 122 | } 123 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal table tr { 124 | color: inherit; 125 | background-color: #fdf6e3; 126 | border-color: #444444; 127 | } 128 | .book.color-theme-1 .book-body .page-wrapper .page-inner section.normal table tr:nth-child(2n) { 129 | background-color: #fbeecb; 130 | } 131 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal { 132 | color: #bdcadb; 133 | } 134 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal a { 135 | color: #3eb1d0; 136 | } 137 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h1, 138 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h2, 139 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h3, 140 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h4, 141 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h5, 142 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h6 { 143 | color: #fffffa; 144 | } 145 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h1, 146 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h2 { 147 | border-color: #373b4e; 148 | } 149 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal h6 { 150 | color: #373b4e; 151 | } 152 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal hr { 153 | background-color: #373b4e; 154 | } 155 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal blockquote { 156 | border-color: #373b4e; 157 | } 158 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre, 159 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code { 160 | color: #9dbed8; 161 | background: #2d3143; 162 | border-color: #2d3143; 163 | } 164 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal .highlight { 165 | background-color: #282a39; 166 | } 167 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal table th, 168 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal table td { 169 | border-color: #3b3f54; 170 | } 171 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal table tr { 172 | color: #b6c2d2; 173 | background-color: #2d3143; 174 | border-color: #3b3f54; 175 | } 176 | .book.color-theme-2 .book-body .page-wrapper .page-inner section.normal table tr:nth-child(2n) { 177 | background-color: #35394b; 178 | } 179 | .book.color-theme-1 .book-header { 180 | color: #afa790; 181 | background: transparent; 182 | } 183 | .book.color-theme-1 .book-header .btn { 184 | color: #afa790; 185 | } 186 | .book.color-theme-1 .book-header .btn:hover { 187 | color: #73553c; 188 | background: none; 189 | } 190 | .book.color-theme-1 .book-header h1 { 191 | color: #704214; 192 | } 193 | .book.color-theme-2 .book-header { 194 | color: #7e888b; 195 | background: transparent; 196 | } 197 | .book.color-theme-2 .book-header .btn { 198 | color: #3b3f54; 199 | } 200 | .book.color-theme-2 .book-header .btn:hover { 201 | color: #fffff5; 202 | background: none; 203 | } 204 | .book.color-theme-2 .book-header h1 { 205 | color: #bdcadb; 206 | } 207 | .book.color-theme-1 .book-body .navigation { 208 | color: #afa790; 209 | } 210 | .book.color-theme-1 .book-body .navigation:hover { 211 | color: #73553c; 212 | } 213 | .book.color-theme-2 .book-body .navigation { 214 | color: #383f52; 215 | } 216 | .book.color-theme-2 .book-body .navigation:hover { 217 | color: #fffff5; 218 | } 219 | /* 220 | * Theme 1 221 | */ 222 | .book.color-theme-1 .book-summary { 223 | color: #afa790; 224 | background: #111111; 225 | border-right: 1px solid rgba(0, 0, 0, 0.07); 226 | } 227 | .book.color-theme-1 .book-summary .book-search { 228 | background: transparent; 229 | } 230 | .book.color-theme-1 .book-summary .book-search input, 231 | .book.color-theme-1 .book-summary .book-search input:focus { 232 | border: 1px solid transparent; 233 | } 234 | .book.color-theme-1 .book-summary ul.summary li.divider { 235 | background: #7e888b; 236 | box-shadow: none; 237 | } 238 | .book.color-theme-1 .book-summary ul.summary li i.fa-check { 239 | color: #33cc33; 240 | } 241 | .book.color-theme-1 .book-summary ul.summary li.done > a { 242 | color: #877f6a; 243 | } 244 | .book.color-theme-1 .book-summary ul.summary li a, 245 | .book.color-theme-1 .book-summary ul.summary li span { 246 | color: #877f6a; 247 | background: transparent; 248 | font-weight: normal; 249 | } 250 | .book.color-theme-1 .book-summary ul.summary li.active > a, 251 | .book.color-theme-1 .book-summary ul.summary li a:hover { 252 | color: #704214; 253 | background: transparent; 254 | font-weight: normal; 255 | } 256 | /* 257 | * Theme 2 258 | */ 259 | .book.color-theme-2 .book-summary { 260 | color: #bcc1d2; 261 | background: #2d3143; 262 | border-right: none; 263 | } 264 | .book.color-theme-2 .book-summary .book-search { 265 | background: transparent; 266 | } 267 | .book.color-theme-2 .book-summary .book-search input, 268 | .book.color-theme-2 .book-summary .book-search input:focus { 269 | border: 1px solid transparent; 270 | } 271 | .book.color-theme-2 .book-summary ul.summary li.divider { 272 | background: #272a3a; 273 | box-shadow: none; 274 | } 275 | .book.color-theme-2 .book-summary ul.summary li i.fa-check { 276 | color: #33cc33; 277 | } 278 | .book.color-theme-2 .book-summary ul.summary li.done > a { 279 | color: #62687f; 280 | } 281 | .book.color-theme-2 .book-summary ul.summary li a, 282 | .book.color-theme-2 .book-summary ul.summary li span { 283 | color: #c1c6d7; 284 | background: transparent; 285 | font-weight: 600; 286 | } 287 | .book.color-theme-2 .book-summary ul.summary li.active > a, 288 | .book.color-theme-2 .book-summary ul.summary li a:hover { 289 | color: #f4f4f5; 290 | background: #252737; 291 | font-weight: 600; 292 | } 293 | -------------------------------------------------------------------------------- /docs/libs/gitbook/css/plugin-search.css: -------------------------------------------------------------------------------- 1 | .book .book-summary .book-search { 2 | padding: 6px; 3 | background: transparent; 4 | position: absolute; 5 | top: -50px; 6 | left: 0px; 7 | right: 0px; 8 | transition: top 0.5s ease; 9 | } 10 | .book .book-summary .book-search input, 11 | .book .book-summary .book-search input:focus, 12 | .book .book-summary .book-search input:hover { 13 | width: 100%; 14 | background: transparent; 15 | border: 1px solid #ccc; 16 | box-shadow: none; 17 | outline: none; 18 | line-height: 22px; 19 | padding: 7px 4px; 20 | color: inherit; 21 | box-sizing: border-box; 22 | } 23 | .book.with-search .book-summary .book-search { 24 | top: 0px; 25 | } 26 | .book.with-search .book-summary ul.summary { 27 | top: 50px; 28 | } 29 | .with-search .summary li[data-level] a[href*=".html#"] { 30 | display: none; 31 | } 32 | -------------------------------------------------------------------------------- /docs/libs/gitbook/css/plugin-table.css: -------------------------------------------------------------------------------- 1 | .book .book-body .page-wrapper .page-inner section.normal table{display:table;width:100%;border-collapse:collapse;border-spacing:0;overflow:auto}.book .book-body .page-wrapper .page-inner section.normal table td,.book .book-body .page-wrapper .page-inner section.normal table th{padding:6px 13px;border:1px solid #ddd}.book .book-body .page-wrapper .page-inner section.normal table tr{background-color:#fff;border-top:1px solid #ccc}.book .book-body .page-wrapper .page-inner section.normal table tr:nth-child(2n){background-color:#f8f8f8}.book .book-body .page-wrapper .page-inner section.normal table th{font-weight:700} 2 | -------------------------------------------------------------------------------- /docs/libs/gitbook/js/clipboard.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * clipboard.js v2.0.4 3 | * https://zenorocha.github.io/clipboard.js 4 | * 5 | * Licensed MIT © Zeno Rocha 6 | */ 7 | !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ClipboardJS=e():t.ClipboardJS=e()}(this,function(){return function(n){var o={};function r(t){if(o[t])return o[t].exports;var e=o[t]={i:t,l:!1,exports:{}};return n[t].call(e.exports,e,e.exports,r),e.l=!0,e.exports}return r.m=n,r.c=o,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=function(){function o(t,e){for(var n=0;no;o++){for(var r=t[o],s=0;i>s&&(r=this._stack[s](r,o,t),void 0!==r);s++);void 0!==r&&e.push(r)}return e},t.Pipeline.prototype.reset=function(){this._stack=[]},t.Pipeline.prototype.toJSON=function(){return this._stack.map(function(e){return t.Pipeline.warnIfFunctionNotRegistered(e),e.label})},t.Vector=function(){this._magnitude=null,this.list=void 0,this.length=0},t.Vector.Node=function(t,e,n){this.idx=t,this.val=e,this.next=n},t.Vector.prototype.insert=function(e,n){this._magnitude=void 0;var i=this.list;if(!i)return this.list=new t.Vector.Node(e,n,i),this.length++;if(en.idx?n=n.next:(i+=e.val*n.val,e=e.next,n=n.next);return i},t.Vector.prototype.similarity=function(t){return this.dot(t)/(this.magnitude()*t.magnitude())},t.SortedSet=function(){this.length=0,this.elements=[]},t.SortedSet.load=function(t){var e=new this;return e.elements=t,e.length=t.length,e},t.SortedSet.prototype.add=function(){var t,e;for(t=0;t1;){if(r===t)return o;t>r&&(e=o),r>t&&(n=o),i=n-e,o=e+Math.floor(i/2),r=this.elements[o]}return r===t?o:-1},t.SortedSet.prototype.locationFor=function(t){for(var e=0,n=this.elements.length,i=n-e,o=e+Math.floor(i/2),r=this.elements[o];i>1;)t>r&&(e=o),r>t&&(n=o),i=n-e,o=e+Math.floor(i/2),r=this.elements[o];return r>t?o:t>r?o+1:void 0},t.SortedSet.prototype.intersect=function(e){for(var n=new t.SortedSet,i=0,o=0,r=this.length,s=e.length,a=this.elements,h=e.elements;;){if(i>r-1||o>s-1)break;a[i]!==h[o]?a[i]h[o]&&o++:(n.add(a[i]),i++,o++)}return n},t.SortedSet.prototype.clone=function(){var e=new t.SortedSet;return e.elements=this.toArray(),e.length=e.elements.length,e},t.SortedSet.prototype.union=function(t){var e,n,i;return this.length>=t.length?(e=this,n=t):(e=t,n=this),i=e.clone(),i.add.apply(i,n.toArray()),i},t.SortedSet.prototype.toJSON=function(){return this.toArray()},t.Index=function(){this._fields=[],this._ref="id",this.pipeline=new t.Pipeline,this.documentStore=new t.Store,this.tokenStore=new t.TokenStore,this.corpusTokens=new t.SortedSet,this.eventEmitter=new t.EventEmitter,this._idfCache={},this.on("add","remove","update",function(){this._idfCache={}}.bind(this))},t.Index.prototype.on=function(){var t=Array.prototype.slice.call(arguments);return this.eventEmitter.addListener.apply(this.eventEmitter,t)},t.Index.prototype.off=function(t,e){return this.eventEmitter.removeListener(t,e)},t.Index.load=function(e){e.version!==t.version&&t.utils.warn("version mismatch: current "+t.version+" importing "+e.version);var n=new this;return n._fields=e.fields,n._ref=e.ref,n.documentStore=t.Store.load(e.documentStore),n.tokenStore=t.TokenStore.load(e.tokenStore),n.corpusTokens=t.SortedSet.load(e.corpusTokens),n.pipeline=t.Pipeline.load(e.pipeline),n},t.Index.prototype.field=function(t,e){var e=e||{},n={name:t,boost:e.boost||1};return this._fields.push(n),this},t.Index.prototype.ref=function(t){return this._ref=t,this},t.Index.prototype.add=function(e,n){var i={},o=new t.SortedSet,r=e[this._ref],n=void 0===n?!0:n;this._fields.forEach(function(n){var r=this.pipeline.run(t.tokenizer(e[n.name]));i[n.name]=r,t.SortedSet.prototype.add.apply(o,r)},this),this.documentStore.set(r,o),t.SortedSet.prototype.add.apply(this.corpusTokens,o.toArray());for(var s=0;s0&&(i=1+Math.log(this.documentStore.length/n)),this._idfCache[e]=i},t.Index.prototype.search=function(e){var n=this.pipeline.run(t.tokenizer(e)),i=new t.Vector,o=[],r=this._fields.reduce(function(t,e){return t+e.boost},0),s=n.some(function(t){return this.tokenStore.has(t)},this);if(!s)return[];n.forEach(function(e,n,s){var a=1/s.length*this._fields.length*r,h=this,l=this.tokenStore.expand(e).reduce(function(n,o){var r=h.corpusTokens.indexOf(o),s=h.idf(o),l=1,u=new t.SortedSet;if(o!==e){var c=Math.max(3,o.length-e.length);l=1/Math.log(c)}return r>-1&&i.insert(r,a*s*l),Object.keys(h.tokenStore.get(o)).forEach(function(t){u.add(t)}),n.union(u)},new t.SortedSet);o.push(l)},this);var a=o.reduce(function(t,e){return t.intersect(e)});return a.map(function(t){return{ref:t,score:i.similarity(this.documentVector(t))}},this).sort(function(t,e){return e.score-t.score})},t.Index.prototype.documentVector=function(e){for(var n=this.documentStore.get(e),i=n.length,o=new t.Vector,r=0;i>r;r++){var s=n.elements[r],a=this.tokenStore.get(s)[e].tf,h=this.idf(s);o.insert(this.corpusTokens.indexOf(s),a*h)}return o},t.Index.prototype.toJSON=function(){return{version:t.version,fields:this._fields,ref:this._ref,documentStore:this.documentStore.toJSON(),tokenStore:this.tokenStore.toJSON(),corpusTokens:this.corpusTokens.toJSON(),pipeline:this.pipeline.toJSON()}},t.Index.prototype.use=function(t){var e=Array.prototype.slice.call(arguments,1);e.unshift(this),t.apply(this,e)},t.Store=function(){this.store={},this.length=0},t.Store.load=function(e){var n=new this;return n.length=e.length,n.store=Object.keys(e.store).reduce(function(n,i){return n[i]=t.SortedSet.load(e.store[i]),n},{}),n},t.Store.prototype.set=function(t,e){this.has(t)||this.length++,this.store[t]=e},t.Store.prototype.get=function(t){return this.store[t]},t.Store.prototype.has=function(t){return t in this.store},t.Store.prototype.remove=function(t){this.has(t)&&(delete this.store[t],this.length--)},t.Store.prototype.toJSON=function(){return{store:this.store,length:this.length}},t.stemmer=function(){var t={ational:"ate",tional:"tion",enci:"ence",anci:"ance",izer:"ize",bli:"ble",alli:"al",entli:"ent",eli:"e",ousli:"ous",ization:"ize",ation:"ate",ator:"ate",alism:"al",iveness:"ive",fulness:"ful",ousness:"ous",aliti:"al",iviti:"ive",biliti:"ble",logi:"log"},e={icate:"ic",ative:"",alize:"al",iciti:"ic",ical:"ic",ful:"",ness:""},n="[^aeiou]",i="[aeiouy]",o=n+"[^aeiouy]*",r=i+"[aeiou]*",s="^("+o+")?"+r+o,a="^("+o+")?"+r+o+"("+r+")?$",h="^("+o+")?"+r+o+r+o,l="^("+o+")?"+i,u=new RegExp(s),c=new RegExp(h),f=new RegExp(a),d=new RegExp(l),p=/^(.+?)(ss|i)es$/,m=/^(.+?)([^s])s$/,v=/^(.+?)eed$/,y=/^(.+?)(ed|ing)$/,g=/.$/,S=/(at|bl|iz)$/,w=new RegExp("([^aeiouylsz])\\1$"),x=new RegExp("^"+o+i+"[^aeiouwxy]$"),k=/^(.+?[^aeiou])y$/,b=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/,E=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/,_=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/,F=/^(.+?)(s|t)(ion)$/,O=/^(.+?)e$/,P=/ll$/,N=new RegExp("^"+o+i+"[^aeiouwxy]$"),T=function(n){var i,o,r,s,a,h,l;if(n.length<3)return n;if(r=n.substr(0,1),"y"==r&&(n=r.toUpperCase()+n.substr(1)),s=p,a=m,s.test(n)?n=n.replace(s,"$1$2"):a.test(n)&&(n=n.replace(a,"$1$2")),s=v,a=y,s.test(n)){var T=s.exec(n);s=u,s.test(T[1])&&(s=g,n=n.replace(s,""))}else if(a.test(n)){var T=a.exec(n);i=T[1],a=d,a.test(i)&&(n=i,a=S,h=w,l=x,a.test(n)?n+="e":h.test(n)?(s=g,n=n.replace(s,"")):l.test(n)&&(n+="e"))}if(s=k,s.test(n)){var T=s.exec(n);i=T[1],n=i+"i"}if(s=b,s.test(n)){var T=s.exec(n);i=T[1],o=T[2],s=u,s.test(i)&&(n=i+t[o])}if(s=E,s.test(n)){var T=s.exec(n);i=T[1],o=T[2],s=u,s.test(i)&&(n=i+e[o])}if(s=_,a=F,s.test(n)){var T=s.exec(n);i=T[1],s=c,s.test(i)&&(n=i)}else if(a.test(n)){var T=a.exec(n);i=T[1]+T[2],a=c,a.test(i)&&(n=i)}if(s=O,s.test(n)){var T=s.exec(n);i=T[1],s=c,a=f,h=N,(s.test(i)||a.test(i)&&!h.test(i))&&(n=i)}return s=P,a=c,s.test(n)&&a.test(n)&&(s=g,n=n.replace(s,"")),"y"==r&&(n=r.toLowerCase()+n.substr(1)),n};return T}(),t.Pipeline.registerFunction(t.stemmer,"stemmer"),t.stopWordFilter=function(e){return e&&t.stopWordFilter.stopWords[e]!==e?e:void 0},t.stopWordFilter.stopWords={a:"a",able:"able",about:"about",across:"across",after:"after",all:"all",almost:"almost",also:"also",am:"am",among:"among",an:"an",and:"and",any:"any",are:"are",as:"as",at:"at",be:"be",because:"because",been:"been",but:"but",by:"by",can:"can",cannot:"cannot",could:"could",dear:"dear",did:"did","do":"do",does:"does",either:"either","else":"else",ever:"ever",every:"every","for":"for",from:"from",get:"get",got:"got",had:"had",has:"has",have:"have",he:"he",her:"her",hers:"hers",him:"him",his:"his",how:"how",however:"however",i:"i","if":"if","in":"in",into:"into",is:"is",it:"it",its:"its",just:"just",least:"least",let:"let",like:"like",likely:"likely",may:"may",me:"me",might:"might",most:"most",must:"must",my:"my",neither:"neither",no:"no",nor:"nor",not:"not",of:"of",off:"off",often:"often",on:"on",only:"only",or:"or",other:"other",our:"our",own:"own",rather:"rather",said:"said",say:"say",says:"says",she:"she",should:"should",since:"since",so:"so",some:"some",than:"than",that:"that",the:"the",their:"their",them:"them",then:"then",there:"there",these:"these",they:"they","this":"this",tis:"tis",to:"to",too:"too",twas:"twas",us:"us",wants:"wants",was:"was",we:"we",were:"were",what:"what",when:"when",where:"where",which:"which","while":"while",who:"who",whom:"whom",why:"why",will:"will","with":"with",would:"would",yet:"yet",you:"you",your:"your"},t.Pipeline.registerFunction(t.stopWordFilter,"stopWordFilter"),t.trimmer=function(t){var e=t.replace(/^\W+/,"").replace(/\W+$/,"");return""===e?void 0:e},t.Pipeline.registerFunction(t.trimmer,"trimmer"),t.TokenStore=function(){this.root={docs:{}},this.length=0},t.TokenStore.load=function(t){var e=new this;return e.root=t.root,e.length=t.length,e},t.TokenStore.prototype.add=function(t,e,n){var n=n||this.root,i=t[0],o=t.slice(1);return i in n||(n[i]={docs:{}}),0===o.length?(n[i].docs[e.ref]=e,void(this.length+=1)):this.add(o,e,n[i])},t.TokenStore.prototype.has=function(t){if(!t)return!1;for(var e=this.root,n=0;n indicates arrow keys):', 82 | '/: navigate to previous/next page', 83 | 's: Toggle sidebar']; 84 | if (config.search !== false) info.push('f: Toggle search input ' + 85 | '(use //Enter in the search input to navigate through search matches; ' + 86 | 'press Esc to cancel search)'); 87 | if (config.info !== false) gitbook.toolbar.createButton({ 88 | icon: 'fa fa-info', 89 | label: 'Information about the toolbar', 90 | position: 'left', 91 | onClick: function(e) { 92 | e.preventDefault(); 93 | window.alert(info.join('\n\n')); 94 | } 95 | }); 96 | 97 | // highlight the current section in TOC 98 | var href = window.location.pathname; 99 | href = href.substr(href.lastIndexOf('/') + 1); 100 | // accentuated characters need to be decoded (#819) 101 | href = decodeURIComponent(href); 102 | if (href === '') href = 'index.html'; 103 | var li = $('a[href^="' + href + location.hash + '"]').parent('li.chapter').first(); 104 | var summary = $('ul.summary'), chaps = summary.find('li.chapter'); 105 | if (li.length === 0) li = chaps.first(); 106 | li.addClass('active'); 107 | chaps.on('click', function(e) { 108 | chaps.removeClass('active'); 109 | $(this).addClass('active'); 110 | gs.set('tocScrollTop', summary.scrollTop()); 111 | }); 112 | 113 | var toc = config.toc; 114 | // collapse TOC items that are not for the current chapter 115 | if (toc && toc.collapse) (function() { 116 | var type = toc.collapse; 117 | if (type === 'none') return; 118 | if (type !== 'section' && type !== 'subsection') return; 119 | // sections under chapters 120 | var toc_sub = summary.children('li[data-level]').children('ul'); 121 | if (type === 'section') { 122 | toc_sub.hide() 123 | .parent().has(li).children('ul').show(); 124 | } else { 125 | toc_sub.children('li').children('ul').hide() 126 | .parent().has(li).children('ul').show(); 127 | } 128 | li.children('ul').show(); 129 | var toc_sub2 = toc_sub.children('li'); 130 | if (type === 'section') toc_sub2.children('ul').hide(); 131 | summary.children('li[data-level]').find('a') 132 | .on('click.bookdown', function(e) { 133 | if (href === $(this).attr('href').replace(/#.*/, '')) 134 | $(this).parent('li').children('ul').toggle(); 135 | }); 136 | })(); 137 | 138 | // add tooltips to the 's that are truncated 139 | $('a').each(function(i, el) { 140 | if (el.offsetWidth >= el.scrollWidth) return; 141 | if (typeof el.title === 'undefined') return; 142 | el.title = el.text; 143 | }); 144 | 145 | // restore TOC scroll position 146 | var pos = gs.get('tocScrollTop'); 147 | if (typeof pos !== 'undefined') summary.scrollTop(pos); 148 | 149 | // highlight the TOC item that has same text as the heading in view as scrolling 150 | if (toc && toc.scroll_highlight !== false) (function() { 151 | // scroll the current TOC item into viewport 152 | var ht = $(window).height(), rect = li[0].getBoundingClientRect(); 153 | if (rect.top >= ht || rect.top <= 0 || rect.bottom <= 0) { 154 | summary.scrollTop(li[0].offsetTop); 155 | } 156 | // current chapter TOC items 157 | var items = $('a[href^="' + href + '"]').parent('li.chapter'), 158 | m = items.length; 159 | if (m === 0) { 160 | items = summary.find('li.chapter'); 161 | m = items.length; 162 | } 163 | if (m === 0) return; 164 | // all section titles on current page 165 | var hs = bookInner.find('.page-inner').find('h1,h2,h3'), n = hs.length, 166 | ts = hs.map(function(i, el) { return $(el).text(); }); 167 | if (n === 0) return; 168 | var scrollHandler = function(e) { 169 | var ht = $(window).height(); 170 | clearTimeout($.data(this, 'scrollTimer')); 171 | $.data(this, 'scrollTimer', setTimeout(function() { 172 | // find the first visible title in the viewport 173 | for (var i = 0; i < n; i++) { 174 | var rect = hs[i].getBoundingClientRect(); 175 | if (rect.top >= 0 && rect.bottom <= ht) break; 176 | } 177 | if (i === n) return; 178 | items.removeClass('active'); 179 | for (var j = 0; j < m; j++) { 180 | if (items.eq(j).children('a').first().text() === ts[i]) break; 181 | } 182 | if (j === m) j = 0; // highlight the chapter title 183 | // search bottom-up for a visible TOC item to highlight; if an item is 184 | // hidden, we check if its parent is visible, and so on 185 | while (j > 0 && items.eq(j).is(':hidden')) j--; 186 | items.eq(j).addClass('active'); 187 | }, 250)); 188 | }; 189 | bookInner.on('scroll.bookdown', scrollHandler); 190 | bookBody.on('scroll.bookdown', scrollHandler); 191 | })(); 192 | 193 | // do not refresh the page if the TOC item points to the current page 194 | $('a[href="' + href + '"]').parent('li.chapter').children('a') 195 | .on('click', function(e) { 196 | bookInner.scrollTop(0); 197 | bookBody.scrollTop(0); 198 | return false; 199 | }); 200 | 201 | var toolbar = config.toolbar; 202 | if (!toolbar || toolbar.position !== 'static') { 203 | var bookHeader = $('.book-header'); 204 | bookBody.addClass('fixed'); 205 | bookHeader.addClass('fixed') 206 | .css('background-color', bookBody.css('background-color')) 207 | .on('click.bookdown', function(e) { 208 | // the theme may have changed after user clicks the theme button 209 | bookHeader.css('background-color', bookBody.css('background-color')); 210 | }); 211 | } 212 | 213 | }); 214 | 215 | gitbook.events.bind("page.change", function(e) { 216 | // store TOC scroll position 217 | var summary = $('ul.summary'); 218 | gs.set('tocScrollTop', summary.scrollTop()); 219 | }); 220 | 221 | var bookBody = $('.book-body'), bookInner = bookBody.find('.body-inner'); 222 | var chapterTitle = function() { 223 | return bookInner.find('.page-inner').find('h1,h2').first().text(); 224 | }; 225 | var saveScrollPos = function(e) { 226 | // save scroll position before page is reloaded 227 | gs.set('bodyScrollTop', { 228 | body: bookBody.scrollTop(), 229 | inner: bookInner.scrollTop(), 230 | focused: document.hasFocus(), 231 | title: chapterTitle() 232 | }); 233 | }; 234 | $(document).on('servr:reload', saveScrollPos); 235 | 236 | // check if the page is loaded in an iframe (e.g. the RStudio preview window) 237 | var inIFrame = function() { 238 | var inIframe = true; 239 | try { inIframe = window.self !== window.top; } catch (e) {} 240 | return inIframe; 241 | }; 242 | if (inIFrame()) { 243 | $(window).on('blur unload', saveScrollPos); 244 | } 245 | 246 | $(function(e) { 247 | var pos = gs.get('bodyScrollTop'); 248 | if (pos) { 249 | if (pos.title === chapterTitle()) { 250 | if (pos.body !== 0) bookBody.scrollTop(pos.body); 251 | if (pos.inner !== 0) bookInner.scrollTop(pos.inner); 252 | } 253 | } 254 | if ((pos && pos.focused) || !inIFrame()) bookInner.find('.page-wrapper').focus(); 255 | // clear book body scroll position 256 | gs.remove('bodyScrollTop'); 257 | }); 258 | 259 | }); 260 | -------------------------------------------------------------------------------- /docs/libs/gitbook/js/plugin-clipboard.js: -------------------------------------------------------------------------------- 1 | gitbook.require(["gitbook", "jQuery"], function(gitbook, $) { 2 | 3 | var copyButton = ''; 4 | var clipboard; 5 | 6 | gitbook.events.bind("page.change", function() { 7 | 8 | if (!ClipboardJS.isSupported()) return; 9 | 10 | // the page.change event is thrown twice: before and after the page changes 11 | if (clipboard) { 12 | // clipboard is already defined 13 | // we can deduct that we are before page changes 14 | clipboard.destroy(); // destroy the previous events listeners 15 | clipboard = undefined; // reset the clipboard object 16 | return; 17 | } 18 | 19 | $(copyButton).prependTo("div.sourceCode"); 20 | 21 | clipboard = new ClipboardJS(".copy-to-clipboard-button", { 22 | text: function(trigger) { 23 | return trigger.parentNode.textContent; 24 | } 25 | }); 26 | 27 | }); 28 | 29 | }); 30 | -------------------------------------------------------------------------------- /docs/libs/gitbook/js/plugin-fontsettings.js: -------------------------------------------------------------------------------- 1 | gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) { 2 | var fontState; 3 | 4 | var THEMES = { 5 | "white": 0, 6 | "sepia": 1, 7 | "night": 2 8 | }; 9 | 10 | var FAMILY = { 11 | "serif": 0, 12 | "sans": 1 13 | }; 14 | 15 | // Save current font settings 16 | function saveFontSettings() { 17 | gitbook.storage.set("fontState", fontState); 18 | update(); 19 | } 20 | 21 | // Increase font size 22 | function enlargeFontSize(e) { 23 | e.preventDefault(); 24 | if (fontState.size >= 4) return; 25 | 26 | fontState.size++; 27 | saveFontSettings(); 28 | }; 29 | 30 | // Decrease font size 31 | function reduceFontSize(e) { 32 | e.preventDefault(); 33 | if (fontState.size <= 0) return; 34 | 35 | fontState.size--; 36 | saveFontSettings(); 37 | }; 38 | 39 | // Change font family 40 | function changeFontFamily(index, e) { 41 | e.preventDefault(); 42 | 43 | fontState.family = index; 44 | saveFontSettings(); 45 | }; 46 | 47 | // Change type of color 48 | function changeColorTheme(index, e) { 49 | e.preventDefault(); 50 | 51 | var $book = $(".book"); 52 | 53 | if (fontState.theme !== 0) 54 | $book.removeClass("color-theme-"+fontState.theme); 55 | 56 | fontState.theme = index; 57 | if (fontState.theme !== 0) 58 | $book.addClass("color-theme-"+fontState.theme); 59 | 60 | saveFontSettings(); 61 | }; 62 | 63 | function update() { 64 | var $book = gitbook.state.$book; 65 | 66 | $(".font-settings .font-family-list li").removeClass("active"); 67 | $(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")").addClass("active"); 68 | 69 | $book[0].className = $book[0].className.replace(/\bfont-\S+/g, ''); 70 | $book.addClass("font-size-"+fontState.size); 71 | $book.addClass("font-family-"+fontState.family); 72 | 73 | if(fontState.theme !== 0) { 74 | $book[0].className = $book[0].className.replace(/\bcolor-theme-\S+/g, ''); 75 | $book.addClass("color-theme-"+fontState.theme); 76 | } 77 | }; 78 | 79 | function init(config) { 80 | var $bookBody, $book; 81 | 82 | //Find DOM elements. 83 | $book = gitbook.state.$book; 84 | $bookBody = $book.find(".book-body"); 85 | 86 | // Instantiate font state object 87 | fontState = gitbook.storage.get("fontState", { 88 | size: config.size || 2, 89 | family: FAMILY[config.family || "sans"], 90 | theme: THEMES[config.theme || "white"] 91 | }); 92 | 93 | update(); 94 | }; 95 | 96 | 97 | gitbook.events.bind("start", function(e, config) { 98 | var opts = config.fontsettings; 99 | if (!opts) return; 100 | 101 | // Create buttons in toolbar 102 | gitbook.toolbar.createButton({ 103 | icon: 'fa fa-font', 104 | label: 'Font Settings', 105 | className: 'font-settings', 106 | dropdown: [ 107 | [ 108 | { 109 | text: 'A', 110 | className: 'font-reduce', 111 | onClick: reduceFontSize 112 | }, 113 | { 114 | text: 'A', 115 | className: 'font-enlarge', 116 | onClick: enlargeFontSize 117 | } 118 | ], 119 | [ 120 | { 121 | text: 'Serif', 122 | onClick: _.partial(changeFontFamily, 0) 123 | }, 124 | { 125 | text: 'Sans', 126 | onClick: _.partial(changeFontFamily, 1) 127 | } 128 | ], 129 | [ 130 | { 131 | text: 'White', 132 | onClick: _.partial(changeColorTheme, 0) 133 | }, 134 | { 135 | text: 'Sepia', 136 | onClick: _.partial(changeColorTheme, 1) 137 | }, 138 | { 139 | text: 'Night', 140 | onClick: _.partial(changeColorTheme, 2) 141 | } 142 | ] 143 | ] 144 | }); 145 | 146 | 147 | // Init current settings 148 | init(opts); 149 | }); 150 | }); 151 | 152 | 153 | -------------------------------------------------------------------------------- /docs/libs/gitbook/js/plugin-search.js: -------------------------------------------------------------------------------- 1 | gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) { 2 | var index = null; 3 | var $searchInput, $searchLabel, $searchForm; 4 | var $highlighted = [], hi, hiOpts = { className: 'search-highlight' }; 5 | var collapse = false, toc_visible = []; 6 | 7 | // Use a specific index 8 | function loadIndex(data) { 9 | // [Yihui] In bookdown, I use a character matrix to store the chapter 10 | // content, and the index is dynamically built on the client side. 11 | // Gitbook prebuilds the index data instead: https://github.com/GitbookIO/plugin-search 12 | // We can certainly do that via R packages V8 and jsonlite, but let's 13 | // see how slow it really is before improving it. On the other hand, 14 | // lunr cannot handle non-English text very well, e.g. the default 15 | // tokenizer cannot deal with Chinese text, so we may want to replace 16 | // lunr with a dumb simple text matching approach. 17 | index = lunr(function () { 18 | this.ref('url'); 19 | this.field('title', { boost: 10 }); 20 | this.field('body'); 21 | }); 22 | data.map(function(item) { 23 | index.add({ 24 | url: item[0], 25 | title: item[1], 26 | body: item[2] 27 | }); 28 | }); 29 | } 30 | 31 | // Fetch the search index 32 | function fetchIndex() { 33 | return $.getJSON(gitbook.state.basePath+"/search_index.json") 34 | .then(loadIndex); // [Yihui] we need to use this object later 35 | } 36 | 37 | // Search for a term and return results 38 | function search(q) { 39 | if (!index) return; 40 | 41 | var results = _.chain(index.search(q)) 42 | .map(function(result) { 43 | var parts = result.ref.split("#"); 44 | return { 45 | path: parts[0], 46 | hash: parts[1] 47 | }; 48 | }) 49 | .value(); 50 | 51 | // [Yihui] Highlight the search keyword on current page 52 | $highlighted = results.length === 0 ? [] : $('.page-inner') 53 | .unhighlight(hiOpts).highlight(q, hiOpts).find('span.search-highlight'); 54 | scrollToHighlighted(0); 55 | 56 | return results; 57 | } 58 | 59 | // [Yihui] Scroll the chapter body to the i-th highlighted string 60 | function scrollToHighlighted(d) { 61 | var n = $highlighted.length; 62 | hi = hi === undefined ? 0 : hi + d; 63 | // navignate to the previous/next page in the search results if reached the top/bottom 64 | var b = hi < 0; 65 | if (d !== 0 && (b || hi >= n)) { 66 | var path = currentPath(), n2 = toc_visible.length; 67 | if (n2 === 0) return; 68 | for (var i = b ? 0 : n2; (b && i < n2) || (!b && i >= 0); i += b ? 1 : -1) { 69 | if (toc_visible.eq(i).data('path') === path) break; 70 | } 71 | i += b ? -1 : 1; 72 | if (i < 0) i = n2 - 1; 73 | if (i >= n2) i = 0; 74 | var lnk = toc_visible.eq(i).find('a[href$=".html"]'); 75 | if (lnk.length) lnk[0].click(); 76 | return; 77 | } 78 | if (n === 0) return; 79 | var $p = $highlighted.eq(hi); 80 | $p[0].scrollIntoView(); 81 | $highlighted.css('background-color', ''); 82 | // an orange background color on the current item and removed later 83 | $p.css('background-color', 'orange'); 84 | setTimeout(function() { 85 | $p.css('background-color', ''); 86 | }, 2000); 87 | } 88 | 89 | function currentPath() { 90 | var href = window.location.pathname; 91 | href = href.substr(href.lastIndexOf('/') + 1); 92 | return href === '' ? 'index.html' : href; 93 | } 94 | 95 | // Create search form 96 | function createForm(value) { 97 | if ($searchForm) $searchForm.remove(); 98 | if ($searchLabel) $searchLabel.remove(); 99 | if ($searchInput) $searchInput.remove(); 100 | 101 | $searchForm = $('
    ', { 102 | 'class': 'book-search', 103 | 'role': 'search' 104 | }); 105 | 106 | $searchLabel = $('
    125 | 126 |
    127 | 258 |
    259 | 260 |
    261 |
    262 | 267 | 268 |
    269 |
    270 | 271 |
    272 |
    273 |

    参考文献

    274 | 275 |
    276 |
    277 | 278 |
    279 |
    280 |
    281 | 282 | 283 |
    284 |
    285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 331 | 332 | 333 | 346 | 347 | 348 | 349 | -------------------------------------------------------------------------------- /fig/ch00-data-science.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch00-data-science.png -------------------------------------------------------------------------------- /fig/ch01-download-rstudio.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch01-download-rstudio.PNG -------------------------------------------------------------------------------- /fig/ch01-rstudio-overview.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch01-rstudio-overview.PNG -------------------------------------------------------------------------------- /fig/ch01-rtools-mac.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch01-rtools-mac.PNG -------------------------------------------------------------------------------- /fig/ch01-rtools-win.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch01-rtools-win.PNG -------------------------------------------------------------------------------- /fig/ch01-select-64-bit.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch01-select-64-bit.PNG -------------------------------------------------------------------------------- /fig/ch01-set-install-path-win.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch01-set-install-path-win.PNG -------------------------------------------------------------------------------- /fig/ch02-fun-env.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch02-fun-env.png -------------------------------------------------------------------------------- /fig/ch02-rstudio-env-panel.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShixiangWang/geek-r-tutorial/bf5e393378c0d4bb563872fc72d832a098c21bf6/fig/ch02-rstudio-env-panel.PNG -------------------------------------------------------------------------------- /geek-r-tutorial.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Website 16 | -------------------------------------------------------------------------------- /index.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "极客R:数据分析之道" 3 | author: "王诗翔, 生信技能树" 4 | date: "上次修改 `r Sys.Date()`" 5 | documentclass: ctexbook 6 | bibliography: [book.bib, packages.bib] 7 | biblio-style: apalike 8 | link-citations: yes 9 | colorlinks: yes 10 | lot: yes 11 | lof: yes 12 | geometry: [b5paper, tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm] 13 | cover-image: cover.png 14 | site: bookdown::bookdown_site 15 | description: "一本极简 R 入门图书" 16 | github-repo: ShixiangWang/geek-r-tutorial 17 | --- 18 | 19 | ```{r setup, include=FALSE} 20 | options( 21 | htmltools.dir.version = FALSE, formatR.indent = 2, width = 55, digits = 4 22 | ) 23 | 24 | # 填上你需要用到的包,如 c('ggplot2', 'dplyr') 25 | lapply(c("tidyverse"), function(pkg) { 26 | if (system.file(package = pkg) == '') install.packages(pkg) 27 | }) 28 | ``` 29 | 30 | # 前言 {-} 31 | 32 | Cover image 33 | 34 | 这是一本 R 语言入门的教程书。它旨在为生物信息学相关方向的研究人员(包括学生)以及相关从业者提供 R 语言基础知识的学习指导。当然,由于本书内容的普适性,我相信对数据科学感兴趣的读者都能够从阅读中受益。如果读者已经掌握了一些 R 的编程知识,可以翻看目录挑选感兴趣的内容阅读;如果读者已经掌握的 R 编程基础知识,我推荐翻看本书的附录部分查找感兴趣的内容进一步深入学习。 35 | 36 | R 语言与知名的 C、Python 这类通用编程语言相比,它是一门领域特定语言(domain-specific language、DSL),专注于数据科学领域。由于 Python 的普及以及它在商业、科研分析中的流行,R 语言常常被用来与 Python 作比较。我在此无意做这样的比较,就我的个人使用经验来看,两者都有其独特和独到之处。作为一名数据科学家,我们常常需要掌握多门编程语言作为工具进行工作,其中主要包括 R、Python 和 Shell。R 和 Python 都是作为主要的数据探索、分析和可视化工具,读者任选其一熟练掌握即可。初学者同时学习 R 和 Python 是不可取,因为不同的编程语言都有相似的数据结构和运算控制逻辑,所以深度掌握一门语言能够更好地工作和更快地学习其他编程语言。另外,初学者往往困惑于 R 和 Python 两种语言中哪种更值得学习。就我的个人观点看,对于生物信息学方向的**大部分读者**,R 是更好的选择,有以下一些理由: 37 | 38 | - R 语言对于没有编程经验的读者更加友好,内置向量化运算机制和默认统计分析包可以快速开始分析和获取结果。 39 | - 由于 [Bioconductor](https://www.bioconductor.org/) 项目库和 [CRAN](https://cran.r-project.org/) 库中保存了上千个生物信息学软件包,涉及生物信息学领域的各个方面,读者在实际的研究工作中很难完全离开 R 语言环境。 40 | - [tidyverse 系列工具包](https://github.com/tidyverse/)的强力驱动让 R 在数据分析各个方面工作都非常强力。 41 | 42 | ## 内容简介 {-} 43 | 44 | 当前已经有不少 R 语言和数据分析相关的教程和书籍,本书无力像它们一样详尽地介绍 R 众多方面的内容,而是聚焦于指导读者学习和理解数据分析的核心要点,并能快速应用到自己实际的工作中。本书的创作哲学是结合必备基础知识的简要介绍与实际问题的讨论与方案。本书的核心内容包括 R 的基础语法和一系列数据分析编程知识,后者将根据 Hadley 在《R for Data Science》提供的数据分析流程概览图 \@ref(fig:r4ds-overview) 中指出的核心要点分别按章节进行介绍。最后,我们将通过一些 R 包学习 R 在生物信息学方向的应用。 45 | 46 | ```{r r4ds-overview, fig.align="center", echo=FALSE, fig.cap="数据分析概览 (图源:R for Data Science 第 1 章)"} 47 | knitr::include_graphics("fig/ch00-data-science.png") 48 | ``` 49 | 50 | 本书的章节概要如下: 51 | 52 | - 第 \@ref(prepare) 章介绍 R 与相应编程环境的安装和使用。 53 | - 第 \@ref(base) 章介绍 R 基础编程语法,包括数据结构、控制流程等。 54 | - 第 \@ref(import) 章介绍如何使用 R 导入常见的数据文件格式。 55 | - 第 \@ref(clean) 章介绍数据清洗操作在 R 中的实现。 56 | - 第 \@ref(visualization) 章介绍数据可视化。 57 | - 第 \@ref(model) 章介绍 R 在统计建模方面的和实践。 58 | - 第 \@ref(report) 章介绍 R 怎样导出结果和生成分析报告。 59 | - 第 \@ref(bioapp) 章通过一些包的使用示例介绍 R 在生物信息学方向的应用。 60 | 61 | ## 许可 {-} 62 | 63 | 本网站(永久)免费阅读,以 [CC 署名-非商业性使用-禁止演绎 4.0 国际协议](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh)发行。 64 | 65 | Copyright © 2020 王诗翔 66 | 67 | 68 | 69 | ## 建议与反馈 {-} 70 | 71 | 这是我第一次编写比较系统的 R 语言教程,由于个人能力有限,难免存在错误和不当,恳请读者批评指正。**目前我在业余时间对本书内容进行积极的开发,如果读者有任何建议,欢迎到 [GitHub 仓库 Issue](https://github.com/ShixiangWang/geek-r-tutorial/issues) 中进行讨论**。 72 | 73 | ## 致谢 {-} 74 | 75 | 非常感谢谁谁以及谁谁对我的帮助。 76 | 77 | ```{block2, type='flushright', html.tag='p'} 78 | 王诗翔 79 | 于 上海 80 | ``` 81 | 82 | -------------------------------------------------------------------------------- /latex/after_body.tex: -------------------------------------------------------------------------------- 1 | \backmatter 2 | \printindex 3 | -------------------------------------------------------------------------------- /latex/before_body.tex: -------------------------------------------------------------------------------- 1 | 2 | \thispagestyle{empty} 3 | 4 | \begin{center} 5 | 献给…… 6 | 7 | 呃,爱谁谁吧 8 | \end{center} 9 | 10 | \setlength{\abovedisplayskip}{-5pt} 11 | \setlength{\abovedisplayshortskip}{-5pt} 12 | -------------------------------------------------------------------------------- /latex/preamble.tex: -------------------------------------------------------------------------------- 1 | \usepackage{booktabs} 2 | \usepackage{longtable} 3 | 4 | \usepackage{framed,color} 5 | \definecolor{shadecolor}{RGB}{248,248,248} 6 | 7 | \renewcommand{\textfraction}{0.05} 8 | \renewcommand{\topfraction}{0.8} 9 | \renewcommand{\bottomfraction}{0.8} 10 | \renewcommand{\floatpagefraction}{0.75} 11 | 12 | \let\oldhref\href 13 | \renewcommand{\href}[2]{#2\footnote{\url{#1}}} 14 | 15 | \makeatletter 16 | \newenvironment{kframe}{% 17 | \medskip{} 18 | \setlength{\fboxsep}{.8em} 19 | \def\at@end@of@kframe{}% 20 | \ifinner\ifhmode% 21 | \def\at@end@of@kframe{\end{minipage}}% 22 | \begin{minipage}{\columnwidth}% 23 | \fi\fi% 24 | \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep 25 | \colorbox{shadecolor}{##1}\hskip-\fboxsep 26 | % There is no \\@totalrightmargin, so: 27 | \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}% 28 | \MakeFramed {\advance\hsize-\width 29 | \@totalleftmargin\z@ \linewidth\hsize 30 | \@setminipage}}% 31 | {\par\unskip\endMakeFramed% 32 | \at@end@of@kframe} 33 | \makeatother 34 | 35 | \makeatletter 36 | \@ifundefined{Shaded}{ 37 | }{\renewenvironment{Shaded}{\begin{kframe}}{\end{kframe}}} 38 | \@ifpackageloaded{fancyvrb}{% 39 | % https://github.com/CTeX-org/ctex-kit/issues/331 40 | \RecustomVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},formatcom=\xeCJKVerbAddon}% 41 | }{} 42 | \makeatother 43 | 44 | \usepackage{makeidx} 45 | \makeindex 46 | 47 | \urlstyle{tt} 48 | 49 | \usepackage{amsthm} 50 | \makeatletter 51 | \def\thm@space@setup{% 52 | \thm@preskip=8pt plus 2pt minus 4pt 53 | \thm@postskip=\thm@preskip 54 | } 55 | \makeatother 56 | 57 | \frontmatter 58 | -------------------------------------------------------------------------------- /latex/template.tex: -------------------------------------------------------------------------------- 1 | \documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(papersize)$$papersize$paper,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$} 2 | $if(beamerarticle)$ 3 | \usepackage{beamerarticle} % needs to be loaded first 4 | $endif$ 5 | $if(fontfamily)$ 6 | \usepackage[$for(fontfamilyoptions)$$fontfamilyoptions$$sep$,$endfor$]{$fontfamily$} 7 | $else$ 8 | \usepackage{lmodern} 9 | $endif$ 10 | $if(linestretch)$ 11 | \usepackage{setspace} 12 | \setstretch{$linestretch$} 13 | $endif$ 14 | \usepackage{amssymb,amsmath} 15 | \usepackage{ifxetex,ifluatex} 16 | \usepackage{fixltx2e} % provides \textsubscript 17 | \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex 18 | \usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc} 19 | \usepackage[utf8]{inputenc} 20 | $if(euro)$ 21 | \usepackage{eurosym} 22 | $endif$ 23 | \else % if luatex or xelatex 24 | \ifxetex 25 | \usepackage{xltxtra,xunicode} 26 | \else 27 | \usepackage{fontspec} 28 | \fi 29 | \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase} 30 | $for(fontfamilies)$ 31 | \newfontfamily{$fontfamilies.name$}[$fontfamilies.options$]{$fontfamilies.font$} 32 | $endfor$ 33 | $if(euro)$ 34 | \newcommand{\euro}{€} 35 | $endif$ 36 | $if(mainfont)$ 37 | \setmainfont[$for(mainfontoptions)$$mainfontoptions$$sep$,$endfor$]{$mainfont$} 38 | $endif$ 39 | $if(sansfont)$ 40 | \setsansfont[$for(sansfontoptions)$$sansfontoptions$$sep$,$endfor$]{$sansfont$} 41 | $endif$ 42 | $if(monofont)$ 43 | \setmonofont[Mapping=tex-ansi$if(monofontoptions)$,$for(monofontoptions)$$monofontoptions$$sep$,$endfor$$endif$]{$monofont$} 44 | $endif$ 45 | $if(CJKmainfont)$ 46 | \usepackage{xeCJK} 47 | \setCJKmainfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$} 48 | $endif$ 49 | \fi 50 | % use upquote if available, for straight quotes in verbatim environments 51 | \IfFileExists{upquote.sty}{\usepackage{upquote}}{} 52 | % use microtype if available 53 | \IfFileExists{microtype.sty}{% 54 | \usepackage{microtype} 55 | \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts 56 | }{} 57 | $if(geometry)$ 58 | \usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry} 59 | $endif$ 60 | \usepackage[unicode=true]{hyperref} 61 | $if(colorlinks)$ 62 | \PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref 63 | $endif$ 64 | \hypersetup{ 65 | $if(title-meta)$ 66 | pdftitle={$title-meta$}, 67 | $endif$ 68 | $if(author-meta)$ 69 | pdfauthor={$author-meta$}, 70 | $endif$ 71 | $if(keywords)$ 72 | pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$}, 73 | $endif$ 74 | $if(colorlinks)$ 75 | colorlinks=true, 76 | linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$, 77 | citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$, 78 | urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$, 79 | $else$ 80 | pdfborder={0 0 0}, 81 | $endif$ 82 | breaklinks=true} 83 | \urlstyle{same} % don't use monospace font for urls 84 | $if(lang)$ 85 | \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex 86 | \usepackage[shorthands=off,$for(babel-otherlangs)$$babel-otherlangs$,$endfor$main=$babel-lang$]{babel} 87 | $if(babel-newcommands)$ 88 | $babel-newcommands$ 89 | $endif$ 90 | \else 91 | \usepackage{polyglossia} 92 | \setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$} 93 | $for(polyglossia-otherlangs)$ 94 | \setotherlanguage[$polyglossia-otherlangs.options$]{$polyglossia-otherlangs.name$} 95 | $endfor$ 96 | \fi 97 | $endif$ 98 | $if(natbib)$ 99 | \usepackage{natbib} 100 | \bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$} 101 | $endif$ 102 | $if(biblatex)$ 103 | \usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex} 104 | $for(bibliography)$ 105 | \addbibresource{$bibliography$} 106 | $endfor$ 107 | $endif$ 108 | $if(listings)$ 109 | \usepackage{listings} 110 | $endif$ 111 | $if(lhs)$ 112 | \lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{} 113 | $endif$ 114 | $if(highlighting-macros)$ 115 | $highlighting-macros$ 116 | $endif$ 117 | $if(verbatim-in-note)$ 118 | \usepackage{fancyvrb} 119 | \VerbatimFootnotes % allows verbatim text in footnotes 120 | $endif$ 121 | $if(tables)$ 122 | \usepackage{longtable,booktabs} 123 | % Fix footnotes in tables (requires footnote package) 124 | \IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{long table}}{} 125 | $endif$ 126 | $if(graphics)$ 127 | \usepackage{graphicx,grffile} 128 | \makeatletter 129 | \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} 130 | \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi} 131 | \makeatother 132 | % Scale images if necessary, so that they will not overflow the page 133 | % margins by default, and it is still possible to overwrite the defaults 134 | % using explicit options in \includegraphics[width, height, ...]{} 135 | \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} 136 | $endif$ 137 | $if(links-as-notes)$ 138 | % Make links footnotes instead of hotlinks: 139 | \renewcommand{\href}[2]{#2\footnote{\url{#1}}} 140 | $endif$ 141 | $if(strikeout)$ 142 | \usepackage[normalem]{ulem} 143 | % avoid problems with \sout in headers with hyperref: 144 | \pdfstringdefDisableCommands{\renewcommand{\sout}{}} 145 | $endif$ 146 | $if(indent)$ 147 | $else$ 148 | \IfFileExists{parskip.sty}{% 149 | \usepackage{parskip} 150 | }{% else 151 | \setlength{\parindent}{0pt} 152 | \setlength{\parskip}{6pt plus 2pt minus 1pt} 153 | } 154 | $endif$ 155 | \setlength{\emergencystretch}{3em} % prevent overfull lines 156 | \providecommand{\tightlist}{% 157 | \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} 158 | $if(numbersections)$ 159 | \setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$} 160 | $else$ 161 | \setcounter{secnumdepth}{0} 162 | $endif$ 163 | $if(subparagraph)$ 164 | $else$ 165 | % Redefines (sub)paragraphs to behave more like sections 166 | \ifx\paragraph\undefined\else 167 | \let\oldparagraph\paragraph 168 | \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}} 169 | \fi 170 | \ifx\subparagraph\undefined\else 171 | \let\oldsubparagraph\subparagraph 172 | \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}} 173 | \fi 174 | $endif$ 175 | $if(dir)$ 176 | \ifxetex 177 | % load bidi as late as possible as it modifies e.g. graphicx 178 | $if(latex-dir-rtl)$ 179 | \usepackage[RTLdocument]{bidi} 180 | $else$ 181 | \usepackage{bidi} 182 | $endif$ 183 | \fi 184 | \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex 185 | \TeXXeTstate=1 186 | \newcommand{\RL}[1]{\beginR #1\endR} 187 | \newcommand{\LR}[1]{\beginL #1\endL} 188 | \newenvironment{RTL}{\beginR}{\endR} 189 | \newenvironment{LTR}{\beginL}{\endL} 190 | \fi 191 | $endif$ 192 | 193 | % set default figure placement to htbp 194 | \makeatletter 195 | \def\fps@figure{htbp} 196 | \makeatother 197 | 198 | $for(header-includes)$ 199 | $header-includes$ 200 | $endfor$ 201 | 202 | $if(title)$ 203 | \title{$title$$if(thanks)$\thanks{$thanks$}$endif$} 204 | $endif$ 205 | $if(subtitle)$ 206 | \providecommand{\subtitle}[1]{} 207 | \subtitle{$subtitle$} 208 | $endif$ 209 | $if(author)$ 210 | \author{$for(author)$$author$$sep$ \and $endfor$} 211 | $endif$ 212 | $if(institute)$ 213 | \providecommand{\institute}[1]{} 214 | \institute{$for(institute)$$institute$$sep$ \and $endfor$} 215 | $endif$ 216 | \date{$date$} 217 | 218 | \begin{document} 219 | $if(title)$ 220 | \maketitle 221 | $endif$ 222 | $if(abstract)$ 223 | \begin{abstract} 224 | $abstract$ 225 | \end{abstract} 226 | $endif$ 227 | 228 | $for(include-before)$ 229 | $include-before$ 230 | 231 | $endfor$ 232 | $if(toc)$ 233 | { 234 | \setcounter{tocdepth}{$toc-depth$} 235 | \tableofcontents 236 | } 237 | $endif$ 238 | $if(lot)$ 239 | \listoftables 240 | $endif$ 241 | $if(lof)$ 242 | \listoffigures 243 | $endif$ 244 | $body$ 245 | 246 | $if(natbib)$ 247 | $if(bibliography)$ 248 | $if(biblio-title)$ 249 | $if(book-class)$ 250 | \renewcommand\bibname{$biblio-title$} 251 | $else$ 252 | \renewcommand\refname{$biblio-title$} 253 | $endif$ 254 | $endif$ 255 | \bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$} 256 | 257 | $endif$ 258 | $endif$ 259 | $if(biblatex)$ 260 | \printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$ 261 | 262 | $endif$ 263 | $for(include-after)$ 264 | $include-after$ 265 | 266 | $endfor$ 267 | \end{document} 268 | -------------------------------------------------------------------------------- /packages.bib: -------------------------------------------------------------------------------- 1 | @Manual{R-base, 2 | title = {R: A Language and Environment for Statistical Computing}, 3 | author = {{R Core Team}}, 4 | organization = {R Foundation for Statistical Computing}, 5 | address = {Vienna, Austria}, 6 | year = {2020}, 7 | url = {https://www.R-project.org/}, 8 | } 9 | @Manual{R-bookdown, 10 | title = {bookdown: Authoring Books and Technical Documents with R Markdown}, 11 | author = {Yihui Xie}, 12 | year = {2020}, 13 | note = {R package version 0.19}, 14 | url = {https://CRAN.R-project.org/package=bookdown}, 15 | } 16 | @Manual{R-data.table, 17 | title = {data.table: Extension of `data.frame`}, 18 | author = {Matt Dowle and Arun Srinivasan}, 19 | year = {2019}, 20 | note = {R package version 1.12.8}, 21 | url = {https://CRAN.R-project.org/package=data.table}, 22 | } 23 | @Manual{R-dplyr, 24 | title = {dplyr: A Grammar of Data Manipulation}, 25 | author = {Hadley Wickham and Romain François and Lionel Henry and Kirill Müller}, 26 | year = {2020}, 27 | note = {R package version 0.8.5}, 28 | url = {https://CRAN.R-project.org/package=dplyr}, 29 | } 30 | @Manual{R-ggplot2, 31 | title = {ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics}, 32 | author = {Hadley Wickham and Winston Chang and Lionel Henry and Thomas Lin Pedersen and Kohske Takahashi and Claus Wilke and Kara Woo and Hiroaki Yutani and Dewey Dunnington}, 33 | year = {2020}, 34 | note = {R package version 3.3.0}, 35 | url = {https://CRAN.R-project.org/package=ggplot2}, 36 | } 37 | @Manual{R-knitr, 38 | title = {knitr: A General-Purpose Package for Dynamic Report Generation in R}, 39 | author = {Yihui Xie}, 40 | year = {2020}, 41 | note = {R package version 1.28}, 42 | url = {https://CRAN.R-project.org/package=knitr}, 43 | } 44 | @Manual{R-readr, 45 | title = {readr: Read Rectangular Text Data}, 46 | author = {Hadley Wickham and Jim Hester and Romain Francois}, 47 | year = {2018}, 48 | note = {R package version 1.3.1}, 49 | url = {https://CRAN.R-project.org/package=readr}, 50 | } 51 | @Manual{R-readxl, 52 | title = {readxl: Read Excel Files}, 53 | author = {Hadley Wickham and Jennifer Bryan}, 54 | year = {2019}, 55 | note = {R package version 1.3.1}, 56 | url = {https://CRAN.R-project.org/package=readxl}, 57 | } 58 | @Manual{R-rmarkdown, 59 | title = {rmarkdown: Dynamic Documents for R}, 60 | author = {JJ Allaire and Yihui Xie and Jonathan McPherson and Javier Luraschi and Kevin Ushey and Aron Atkins and Hadley Wickham and Joe Cheng and Winston Chang and Richard Iannone}, 61 | year = {2020}, 62 | note = {R package version 2.1}, 63 | url = {https://CRAN.R-project.org/package=rmarkdown}, 64 | } 65 | @Book{bookdown2016, 66 | title = {bookdown: Authoring Books and Technical Documents with {R} Markdown}, 67 | author = {Yihui Xie}, 68 | publisher = {Chapman and Hall/CRC}, 69 | address = {Boca Raton, Florida}, 70 | year = {2016}, 71 | note = {ISBN 978-1138700109}, 72 | url = {https://github.com/rstudio/bookdown}, 73 | } 74 | @Book{ggplot22016, 75 | author = {Hadley Wickham}, 76 | title = {ggplot2: Elegant Graphics for Data Analysis}, 77 | publisher = {Springer-Verlag New York}, 78 | year = {2016}, 79 | isbn = {978-3-319-24277-4}, 80 | url = {https://ggplot2.tidyverse.org}, 81 | } 82 | @Book{knitr2015, 83 | title = {Dynamic Documents with {R} and knitr}, 84 | author = {Yihui Xie}, 85 | publisher = {Chapman and Hall/CRC}, 86 | address = {Boca Raton, Florida}, 87 | year = {2015}, 88 | edition = {2nd}, 89 | note = {ISBN 978-1498716963}, 90 | url = {https://yihui.org/knitr/}, 91 | } 92 | @InCollection{knitr2014, 93 | booktitle = {Implementing Reproducible Computational Research}, 94 | editor = {Victoria Stodden and Friedrich Leisch and Roger D. Peng}, 95 | title = {knitr: A Comprehensive Tool for Reproducible Research in {R}}, 96 | author = {Yihui Xie}, 97 | publisher = {Chapman and Hall/CRC}, 98 | year = {2014}, 99 | note = {ISBN 978-1466561595}, 100 | url = {http://www.crcpress.com/product/isbn/9781466561595}, 101 | } 102 | @Book{rmarkdown2018, 103 | title = {R Markdown: The Definitive Guide}, 104 | author = {Yihui Xie and J.J. Allaire and Garrett Grolemund}, 105 | publisher = {Chapman and Hall/CRC}, 106 | address = {Boca Raton, Florida}, 107 | year = {2018}, 108 | note = {ISBN 9781138359338}, 109 | url = {https://bookdown.org/yihui/rmarkdown}, 110 | } 111 | -------------------------------------------------------------------------------- /render.R: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env Rscript 2 | # 确保自己在book目录下 3 | 4 | bookdown::render_book("index.Rmd") 5 | #bookdown::render_book("index.Rmd", output_format = "all") 6 | unlink("docs/*", recursive = TRUE, force = TRUE) 7 | system("cp -rf _book/* docs") --------------------------------------------------------------------------------