R Markdown——Markdown 学习笔记

UCLA:Introduction To R (简单入门)

1.什么是 Markdown ?

Markdown是一种轻量级的标记语言,它是一个用于格式化文档的代码标签系统。标签被用来定义Section,改变文本的外观,建立表格,链接图像,等等。

超文本标记语言(HTML)是一种旨在用于网页的标记语言。文本、图像、框架、表格和其他元素的位置和外观都可以用HTML指定。

Markdown最初被设计为HTML的缩写。例如,在HTML中,为了使文本斜体化,你将其包围在中。在 Markdown 中,你只需要把它包围在 * * 中。

2.一些常用的Markdown标签(tag):

  1. italics produces italics (* *斜体)

  2. bold produces bold (** **加粗)

  3. Header(# 标题等级)

例如:

这是一级标题

这是二级标题

这是三级标题

这是四级标题

注意:在格式化标签和文本之间不要插入空格。

3.添加R代码块

To create a code chunk:
Use the shortcut Ctrl + Alt + I (Cmd + Option + I on Macs)
Use the green Insert code button towards the top right of the script editor (will appear if the file extension is .rmd)
Type out the tags

例如: 添加R代码:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

上面是上述代码的运行结果:
也可以用R生成图形:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

4.练习:

A. Add an R code chunk using the keyboard shortcut or the Insert button to your R Markdown file.
B. Inside this code chunk, load the trees data set with the code data(“trees”).
C. Produce a correlation matrix of the 3 variables in the trees data using the code cor(trees).
D. Knit the .Rmd file and take note of the correlation matrix.
E. Then, restrict the trees dataset to observations with Height greater than 80 by adding the code trees <- subset(trees, trees$Height>80) immediately after data(“trees”).
F. Knit the .Rmd file again and notice how the correlation matrix changes.
解答:

data(trees)
cor(trees)
##            Girth    Height    Volume
## Girth  1.0000000 0.5192801 0.9671194
## Height 0.5192801 1.0000000 0.5982497
## Volume 0.9671194 0.5982497 1.0000000
data(trees)
trees <- subset(trees, trees$Height>80)
trees
##    Girth Height Volume
## 5   10.7     81   18.8
## 6   10.8     83   19.7
## 17  12.9     85   33.8
## 18  13.3     86   27.4
## 26  17.3     81   55.4
## 27  17.5     82   55.7
## 31  20.6     87   77.0

5.YAML header

YAML头控制文档的整体外观,包括文档类型。它位于文件顶部的 之间。
YAML(Yet Another Markup Language)头与一个叫做pandoc的程序一起工作,它可以在不同的文件类型之间翻译代码标签。
默认的文档类型是html_document,它通常是一个网页。其他文档类型包括:
HTML slideshows
LaTeX pdf documents
Word documents
PowerPoint presentations
Books

6.尝试改变输出类型为Word documents

7.插入各种内容

  1. 插入超链接 B站教学视频
  2. 插入图片 image
  3. 插入其他语言代码块(eg. Python)