R for data science||R Markdown

话要说起来,我接触到的第一门现代语言就是Markdown了。起因是自己年轻的时候,喜欢写作,在网上了解到阳志平、李笑来等写作用Markdown。然后就开始用火狐浏览器的Markdown Here ,大概花了两个小时,搞出了自己喜欢的Markdown主题。后来才知道R是可以用markdown来写作的,然后是简书也可以,然后是有道笔记对Markdown的支持。

当然并不是说现在我的Markdown用的有多深,还是和一开始一样只是会一些基本的语法,这正是使用它的原因:简单。我也不会在这里介绍很多markdown语法,网上并不缺少这类资料。

R Markdown 基础

R Markdown 速查表 在Rstudio中很容易找到:


help中有这多好东西呢!

有了速查表可以开始了:

---
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


<http://example.com>

[linked phrase](http://example.com)

!optional caption text

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_documentmakes 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_documentfor 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
  • 演示文稿
  1. ioslides_presentation - HTML presentation with ioslides

  2. slidy_presentation - HTML presentation with W3C Slidy

  3. beamer_presentation - PDF presentation with LaTeX Beamer.

Two other popular formats are provided by packages:

  1. revealjs::revealjs_presentation - HTML presentation with reveal.js. Requires the revealjspackage.

  2. 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
    ---


    图片.png
  • 交互元素

    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:

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:

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.


r4ds
R Markdown 简介
R_文档输出_rmarkdown

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • R Markdown Workshop 背景 这对我来说是一个不寻常的帖子,我之前试图避开写关于R Markdow...
    生信银河战舰阅读 584评论 0 2
  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007阅读 2,165评论 0 3
  • 本篇文章是基于谷歌有关Graphic的一篇概览文章的翻译:http://source.android.com/de...
    lee_3do阅读 7,067评论 2 21
  • 培训师讲授 第一互动环节:假亦真时真亦假 每人在纸上写上自己的四个特质(eg:我喜欢吃饭,一天可以吃五顿饭,每吨三...
    Over任先生阅读 56评论 0 0
  • 这么多年了,他怎么可以一直欺骗我?欺骗哈桑?我很小的时候,有一次他抱我坐在他的膝盖上。眼睛直勾勾的看着我,并说,世...
    剑清0阅读 316评论 0 0