话要说起来,我接触到的第一门现代语言就是Markdown了。起因是自己年轻的时候,喜欢写作,在网上了解到阳志平、李笑来等写作用Markdown。然后就开始用火狐浏览器的Markdown Here ,大概花了两个小时,搞出了自己喜欢的Markdown主题。后来才知道R是可以用markdown来写作的,然后是简书也可以,然后是有道笔记对Markdown的支持。
当然并不是说现在我的Markdown用的有多深,还是和一开始一样只是会一些基本的语法,这正是使用它的原因:简单。我也不会在这里介绍很多markdown语法,网上并不缺少这类资料。
R Markdown 基础
R Markdown 速查表 在Rstudio中很容易找到:
有了速查表可以开始了:
---
title: "Untitled"
author: "zhouyunlai"
date: "2019年8月6日"
output: html_document
---
\```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
\```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
\```{r cars}
summary(cars)
\```
## Including Plots
You can also embed plots, for example:
\```{r pressure, echo=FALSE}
plot(pressure)
\```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
新建的文件基本包含了RMarkdown的基本结构了:
- 两个--- 之间YAML文件头
- ``` 之间的 R代码段
- 一些具有简单格式的文文本
如果想要生成包含所有文本代码的报告可以点击 Kint 或者按组合键Ctrl+ Shift+ K ,还可以使用rmarkdown::render("1-example.Rmd") 在R中生成。
使用Markdown 格式化文本
.Rmd文件中的文本是使用Markdown语言写的,Markdown适用于格式化纯文本文件的一种轻量级语法,其设计思想是使得文本极容易书写又容易阅读。Markdown学习起来非常容易。
Text formatting
*italic* or italic
*bold* bold
`code`
\superscript^2^ and subscript~2~
Headings
# 1st Level Header
## 2nd Level Header
### 3rd Level Header
Lists
* Bulleted list item 1
* Item 2
* Item 2a
* Item 2b
\1. Numbered list item 1
\1. Item 2. The numbers are incremented automatically in the output.
Links and images
[linked phrase](http://example.com)
Tables
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
代码段
插入代码段:
- Cmd/Ctrl + Alt + I
- The “Insert” button icon in the editor toolbar.
- By manually typing the chunk delimiters```{r} and ```
代码段名称
我们可以赋予一个代码段一个名称,这样做有三个好处:
- 方便浏览特定的代码段
- 可以使代码生成的图形具有特定的名称
- 可以建立代码段网络
代码段选项
用选项results=选择文本型结果的类型。 取值有:
- markup, 这是缺省选项, 会把文本型结果变成HTML的原样文本格式。
- hide, 运行了代码后不显示运行结果。
- hold, 一个代码块所有的代码都显示完, 才显示所有的结果。
- asis, 文本型输出直接进入到HTML文件中, 这需要R代码直接生成HTML标签, knitr包的kable()函数可以把数据框转换为HTML代码的表格。
表格
knitr::kable(
mtcars[1:5, ],
caption = "A knitr kable."
)
| | mpg| cyl| disp| hp| drat| wt| qsec| vs| am| gear| carb|
|:-----------------|----:|---:|----:|---:|----:|-----:|-----:|--:|--:|----:|----:|
|Mazda RX4 | 21.0| 6| 160| 110| 3.90| 2.620| 16.46| 0| 1| 4| 4|
|Mazda RX4 Wag | 21.0| 6| 160| 110| 3.90| 2.875| 17.02| 0| 1| 4| 4|
|Datsun 710 | 22.8| 4| 108| 93| 3.85| 2.320| 18.61| 1| 1| 4| 1|
|Hornet 4 Drive | 21.4| 6| 258| 110| 3.08| 3.215| 19.44| 1| 0| 3| 1|
|Hornet Sportabout | 18.7| 8| 360| 175| 3.15| 3.440| 17.02| 0| 0| 3| 2|
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
值得学习的R Markdown 包: xtable, stargazer, pander, tables, and ascii packages.
书中还讨论了,缓存、全局选项,内联代码、排错、YAML文件头,文档参数和参考文献引用等主题。
R Markdown 输出类型
- 文档
pdf_document
makes a PDF with LaTeX (an open source document layout system), which you’ll need to install. RStudio will prompt you if you don’t already have it.
word_document
for Microsoft Word documents (.docx).
odt_document
for OpenDocument Text documents (.odt).
rtf_document
for Rich Text Format (.rtf) documents.
md_document
for a Markdown document. This isn’t typically useful by itself, but you might use it if, for example, your corporate CMS or lab wiki uses markdown.
github_document
: this is a tailored version of md_document designed for sharing on GitHub.
For html_documents another option is to make the code chunks hidden by default, but visible with a click:
output:
html_document:
code_folding: hide
- 笔记本
output:
html_notebook: default
github_document: default
- 演示文稿
ioslides_presentation
- HTML presentation with ioslidesslidy_presentation
- HTML presentation with W3C Slidybeamer_presentation
- PDF presentation with LaTeX Beamer.
Two other popular formats are provided by packages:
revealjs::revealjs_presentation
- HTML presentation with reveal.js. Requires the revealjspackage.rmdshower, https://github.com/MangoTheCat/rmdshower, provides a wrapper around the shower, https://github.com/shower/shower, presentation engine
-
仪表盘
---
title: "Diamonds distribution dashboard"
output: flexdashboard::flex_dashboard
---
-
交互元素
htmlwidgets
library(leaflet)
leaflet() %>%
setView(117.0425, 39.40777777777778 , zoom = 10) %>%
addTiles() %>%
addMarkers(117.0425, 39.40777777777778 , popup = "zhouyunlai@")
There are many packages that provide htmlwidgets, including:
dygraphs, http://rstudio.github.io/dygraphs/, for interactive time series visualisations.
DT, http://rstudio.github.io/DT/, for interactive tables.
threejs, https://github.com/bwlewis/rthreejs for interactive 3d plots.
DiagrammeR, http://rich-iannone.github.io/DiagrammeR/ for diagrams (like flow charts and simple node-link diagrams).
To learn more about htmlwidgets and see a more complete list of packages that provide them visit http://www.htmlwidgets.org/.
shiny
---
title: "Shiny Web App"
output: html_document
runtime: shiny
---
```{r setup, include = FALSE}
library(ggplot2)
library(dplyr)
knitr::opts_chunk$set(fig.width = 5, fig.asp = 1/3)
```
```{r}
library(shiny)
textInput("name", "What is your name?")
numericInput("age", "How old are you?", NA, min = 0, max = 150)
```
- 网站
With a little additional infrastructure you can use R Markdown to generate a complete website:
Put your
.Rmd
files in a single directory.index.Rmd
will become the home page.-
Add a YAML file named
_site.yml
provides the navigation for the site. For example:name: "my-website" navbar: title: "My Website" left: - text: "Home" href: index.html - text: "Viridis Colors" href: 1-example.html - text: "Terrain Colors" href: 3-inline.html
Execute rmarkdown::render_site()
to build _site
, a directory of files ready to deploy as a standalone static website, or if you use an RStudio Project for your website directory. RStudio will add a Build tab to the IDE that you can use to build and preview your site.
Read more at http://rmarkdown.rstudio.com/rmarkdown_websites.html.
Other packages provide even more output formats:
The bookdown package, https://github.com/rstudio/bookdown, makes it easy to write books, like this one. To learn more, read Authoring Books with R Markdown, by Yihui Xie, which is, of course, written in bookdown. Visit http://www.bookdown.org to see other bookdown books written by the wider R community.
The prettydoc package, https://github.com/yixuan/prettydoc/, provides lightweight document formats with a range of attractive themes.
The rticles package, https://github.com/rstudio/rticles, compiles a selection of formats tailored for specific scientific journals.
See http://rmarkdown.rstudio.com/formats.html for a list of even more formats. You can also create your own by following the instructions at http://rmarkdown.rstudio.com/developer_custom_formats.html.