This commit is contained in:
Xiaobin.Liu 2023-03-12 10:47:42 +08:00
parent 4ce2220d2c
commit 59ae5f0df8
2 changed files with 409 additions and 369 deletions

View File

@ -1,369 +0,0 @@
[#]: subject: "Beginner's Guide to R Markdown Syntax [With Cheat Sheet]"
[#]: via: "https://itsfoss.com/r-markdown/"
[#]: author: "Sreenath https://itsfoss.com/author/sreenath/"
[#]: collector: "lkxed"
[#]: translator: "lxbwolf"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
======
You probably already know about the lightweight Markdown markup language. Refer to our [Markdown guide][1], if you're new to the concept. Overall, it is a simple and effective language for creating plain-text documents.
However, Markdown may not be enough to make detailed reports or technical documents.
Hence, **R Markdown** as an **interactive file format** came into existence back in 2014 thanks to packages like [knitr][2] and [Pandoc][3]. It combines plain text with in-line R code, helping you make a dynamic document.
To create R Markdown documents, you can use [various IDEs][4] and extensions to make it possible. However, the official IDE that helps you do it is **RStudio**. So, in this article, we will focus on **learning R Markdown syntax using RStudio**.
💡
If you did not know,
**R programming language**
is used for statistical computing, graphics representation, and reporting.
**Suggested Read 📖**
How to Install and Use R on UbuntuBrief: This tutorial teaches you to install R on Ubuntu. Youll also learn how to run your first R program in Ubuntu using various methods. R, together with Python, is the most commonly used programming language for statistical computing and graphics, making it easy to work with data. With![][5]It's FOSSSergiu![][6]
### Setting RStudio
RStudio makes it easy to work with R Markdown by its setup process. You just need to install a package, and you are done for the most part!
Once you have RStudio installed, head to the Tools menu and select the _Install Packages_ option.
![Select Install Packages option under Tools menu in RStudio][7]
On the new dialog box, search for rmarkdown and install it.
![Install RMarkdown Package by searching it and pressing install button on the new package install dialog box][8]
💡
To use code chunks like python, you need to install additional packages. RStudio will prompt you to install the required packages when you try to include them in your document.
Once installed, you can start a new rmarkdown document by selecting **File > New > RMarkdown**.
![Create a new RMarkdown Document from File menu][9]
This will prompt you to add some information regarding the document (metadata for the file). Fill those up.
![Provide the title and other details for new document in rmarkdown][10]
Or you can create an empty document to start fresh.
### RMarkdown Syntax
Since it is just "**enhanced Markdown**," most syntax remains the same.
There would be some differences when you add things not usually supported with Markdown, like **tables, math equations, code chunks, etc.**
Here's a quick summary of what we will be covering:
Name of the RMarkdown BlockProper Syntax |
| Heading | # Level 1## Level 2### Level 3Level 1=======Level 2------- |
| Emphasis | *Italics*_Italics_**Bold**__Bold__ |
| List | Unordered List* Item* Item + Sub + SubOrdered List1. Item2. Item + Sub + Sub |
| Code Chunk | Normal Code Block```Code Goes Here```R Code Block```{r}R CODE```You can use other languages also.Inline `code` |
| Links | Plain Link: Paste the URLLink with Caption: [Text](URL_Address)Link to a section: [Text](#Name-of-section) |
| Table | | Column | Column | Column || ------ | ------ | ------ || Item | Item | Item || Item | Item | Item | |
| Equations | In line Equations: $Equations$Display Equations: $$Equations$$ |
| Images | Without Caption: ![](Link-to-Image)With Caption : ![optional caption text](Location-of-image) |
| Block Quotes | > Type your Block Quotes |
| Misc | Super Script : Text^Superscript^Horizontal rule or Page Break:========= or ----------For Manual Line break, end line with 2+ spaces |
#### The YAML Header
At the top of a Rmarkdown document, there is a YAML header enclosed within two `---`. This block usually contains a title, author, date, and the file type you want to output, defining the **final look of the document.**
The file type is either **HTML, PDF, or Word.**
```
---
title: "Sample"
author: "It's FOSS"
date: "2023-02-08"
output: pdf_document
---
```
This can be added while setting the new document in RStudio, which is shown in the above section.
#### Heading
In R Markdown, we can give heading in two different methods. Either we can use the # character for different levels of heading like:
```
# Heading Level 1
## Heading Level 2
### Heading Level 3
#### Heading Level 4
##### Heading Level 5
###### Heading Level 6
```
Or, `=` and `-` for level 1 and 2 headings, respectively.
```
Level 1 Heading
===============
Level 2 Heading
---------------
```
![various types of heading levels in rmarkdown file][11]
#### Lists
There are two types of Lists, the first one is an **Unordered list**, or you could call them bullet points:
```
* Item 1
* Item 2
+ Sub 1
+ Sub 2
* Item 3
```
And the second one is the **Ordered list**, which is the numbered type:
```
1. Item 1
2. Item 2
+ Sub 1
+ Sub 2
3. Item 3
```
![order and unordered list example][12]
**Suggested Read 📖**
Read and Organize Markdown Files in Linux Terminal With GlowGlow is a CLI tool that lets you render Markdown files in the Linux terminal. You can also organize Markdown files with it.![][13]It's FOSSAbhishek Prakash![][14]
#### Format text within a paragraph
There are several ways to format text.
![][15]
You can add emphasis to the text like italics or bold using:
- Italics: Place the text in between single asterisks or single underscore
- Bold: Place the text in between double asterisks or double underscores.
```
*This is Italicized text*
_This is Italicized text_
**This is Bold Text**
__This is Bold Text__
```
You can explore on this using our resource on [how to add bold and italic text in Markdown][16].
If you want to add superscript to a text, place the text that should be superscript in between `^` symbol.
```
Normal Text^super_script^
```
Or, if you want to add text strike-through, place the text in between two `~~` symbol.
```
~Strike Through this~~
```
![][17]
#### Adding Code Chunks
Embedding code is the primary purpose of R Markdown. It allows us to add codes in several ways.
**Adding Normal code block.**
If you want to add a normal code block to separate it from other text, use the syntax below:
```
```
Your Code Goes Here
```
```
You can also try [adding code blocks with syntax highlighting][18].
You should append the language in curly braces if you want to add code and embed its output to the document:
```
```{Language}
Your Code Goes Here
```
```
Or, you can add inline codes by placing the respective text between ` symbols.
```
The `code` is a code
```
Here's how it should look like:
![][19]
#### Links
To add a link as plain text, just paste the link as it is in a line.
```
https://itsfoss.com
```
Or, to make a text hyperlink, use the syntax:
```
[Text](URL Address)
```
Another way to add a link is, when you want to link to a section of the page. In this case, use the syntax:
```
[Text](#Name-of-section)
```
![][20]
#### Tables
The syntax for adding tables is similar to that of markdown.
```
|Column|Column|Column|
| --- | --- | --- |
|Item|Item|Item|
|Item|Item|Item|
```
![][21]
📋
Curious to know more? Refer to our guide on
[creating tables in Markdown][22]
.
#### Images
To add an image, use the syntax:
```
![](http://example.com/logo.png)
OR
![optional caption text](figures/img.png)
```
![][23]
#### Block Quotes
RMarkdown allows you to add block quotes. To use this, use the **> (greater than)** symbol in front of the line/paragraph you want to quote.
```
This is a normal text
> This is a Block Quote
```
![][24]
If you want to explore more use cases of blockquote, head to our [Markdown quotes][25] guide.
#### Equations
Using RMarkdown, you can add either equations or display complex LaTex equations.
For example:
```
In line Pythagorean Theorem: $Equation$
Display Equation: $$Equation$$
```
![adding equations in R Markdown document][26]
#### Horizontal Rule / Page Break
Use three or more asterisks or dashes to add a horizontal rule /page break.
```
************
------------
```
If you want to add a manual line break, end that line with two or more spaces.
### Summary
### R Markdown is Useful (Cheat Sheet)
Whether you are working with scientific reports or want to create any other type of dynamic document, R Markdown is your best bet to make the most out of Markdown.
Here's a cheat sheet to help you summarize it all:
![][27]
[R Markdown Cheat SheetR Markdown Cheat Sheet.pdf145 KBdownload-circle][28]
_💬 Did we miss something that you use with R Markdown? Share your thoughts in the comments down below._
--------------------------------------------------------------------------------
via: https://itsfoss.com/r-markdown/
作者:[Sreenath][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sreenath/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/markdown-guide/
[2]: https://www.r-project.org/nosvn/pandoc/knitr.html
[3]: https://itsfoss.comknitr, and Pandoc
[4]: https://itsfoss.com/best-modern-open-source-code-editors-for-linux/
[5]: https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png
[6]: https://itsfoss.com/content/images/wordpress/2019/06/install-r-on-ubuntu.jpg
[7]: https://itsfoss.com/content/images/2023/02/select-install-packages.png
[8]: https://itsfoss.com/content/images/2023/02/install-rmarkdown-1.png
[9]: https://itsfoss.com/content/images/2023/02/new-r-markdown.png
[10]: https://itsfoss.com/content/images/2023/02/new-document-in-rmark.png
[11]: https://itsfoss.com/content/images/2023/02/Headings.png
[12]: https://itsfoss.com/content/images/2023/02/List.png
[13]: https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png
[14]: https://itsfoss.com/content/images/wordpress/2022/01/glow-cli-markdown.png
[15]: https://itsfoss.com/content/images/2023/02/emphasis.png
[16]: https://itsfoss.com/markdown-bold-italic/
[17]: https://itsfoss.com/content/images/2023/02/superescript.png
[18]: https://itsfoss.com/markdown-code-block/
[19]: https://itsfoss.com/content/images/2023/02/code-chunk.png
[20]: https://itsfoss.com/content/images/2023/02/links.png
[21]: https://itsfoss.com/content/images/2023/02/table.png
[22]: https://itsfoss.com/markdown-table/
[23]: https://itsfoss.com/content/images/2023/02/images.png
[24]: https://itsfoss.com/content/images/2023/02/block-quotes.png
[25]: https://itsfoss.com/markdown-quotes/
[26]: https://itsfoss.com/content/images/2023/02/equations.png
[27]: https://itsfoss.com/content/images/2023/02/R-Markdown-Cheat-Sheet.webp
[28]: https://itsfoss.com/content/files/2023/02/R-Markdown-Cheat-Sheet.pdf

View File

@ -0,0 +1,409 @@
[#]: subject: "Beginner's Guide to R Markdown Syntax [With Cheat Sheet]"
[#]: via: "https://itsfoss.com/r-markdown/"
[#]: author: "Sreenath https://itsfoss.com/author/sreenath/"
[#]: collector: "lkxed"
[#]: translator: "lxbwolf"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
R Markdown 语法新手指南[备忘录]
======
你可能已经了解过轻量级标记语言 Markdown。如果你是第一次接触这个概念请参考我们的 [Markdown 指南][1]。概括来讲,它是一种用于创建纯文本文档的简单又高效的语言。
然而Markdown 在制作详细的报告或技术文件方面可能还不够完善。
受益于[knitr][2]和[Pandoc][3]等软件包,**交互式文件格式 R Markdown** 早在 2014 年就出现了。它将纯文本与内嵌的 R 代码相结合,可以制作动态文件。
你可以使用 [各种 IDE][4] 和扩展来创建 R Markdown 文档,官方 IDE 为**RStudio**。因此,在这篇文章中,我们将重点介绍**使用 RStudio 学习 R Markdown语法**。
💡
(假如你没有了解过,)**R 编程语言** 是一种用于统计计算、图形表示和报告的语言。
**推荐阅读 📖**
是一种用于统计计算、图形表示和报告的语言。
**建议阅读 📖**
如何在 Ubuntu 上安装和使用 R。
概要:本教程教你如何在 Ubuntu上安装 R。你还将学习在 Ubuntu 上运行你的第一个 R 程序的各种方法。R 和 Python 都是最常用的统计计算和图形的编程语言,能让数据处理变得简单。
![][5]
![][6]
### 配置 RStudio
通过合适的配置,你可以很轻松地用 RStudio 来编写 R Markdown。当你安装完一个软件包后就已经完成了大部分的工作!
安装 RStudio 后,在**工具**菜单中选择_安装软件包_选项。
![在 RStudio 的工具菜单下选择安装软件包选项][7] 。
在弹出的对话框中,搜索 rmarkdown 并安装。
![通过搜索并在新的软件包安装对话框中按下安装按钮来安装 Rmarkdown 软件包][8]
💡
如果你想使用类似 python 的代码块你需要安装额外的包。当你想在你的文档中包含这些包时RStudio 会提示你安装所需的包。
安装完成后,你可以通过选择**文件>新建>RMarkdown** 来新建一个 rmarkdown 文档。
![从文件菜单中创建一个新的 RMarkdown 文档][9] 。
之后会提示你添加一些关于文件的信息(文件的元数据),把这些填上就可以了。
![用 rmarkdown 语法写出标题和其他细节][10]
或者你可以创建一个空的文件从零开始。
### RMarkdown 语法
由于它是"加强版的 Markdown",因此大多数语法与 markdown 是一样的。
它还有一些 Markdown 支持不完善的东西,比如**表格、数学方程式、代码块等等**。
下面是我们要介绍的内容的概括。
<table>
<thead>
<tr>
<th>RMarkdown 块名</th>
<th>语法</th>
</tr>
</thead>
<tbody>
<tr>
<td>标题</td>
<td># 一级标题<br>## 二级标题<br>### 三级标题<br><br>一级标题<br>=======<br><br>二级标题<br>-------</td>
</tr>
<tr>
<td>着重</td>
<td>*斜体*<br>_斜体_<br><br>**加粗**<br><br>__加粗__</td>
</tr>
<tr>
<td>列表</td>
<td>无序列表<br>* 列表项<br>* 列表项<br> + 子项<br> + 子项<br><br>有序列表<br>1. 列表项<br>2. 列表项<br> + 子项<br> + 子项</td>
</tr>
<tr>
<td>代码块</td>
<td>普通代码块<br><br>```<br>这里写代码<br>```<br><br>R 代码块<br><br>```{r}<br>R 代码<br>```<br>你也可以用其他的语言<br><br><br>行内 `代码`</td>
</tr>
<tr>
<td>链接</td>
<td>纯文本:粘贴 URL<br>带标题的链接:[显示的文本](URL_地址)<br>跳转到锚点[显示的文本](#锚点)</td>
</tr>
<tr>
<td>表格</td>
<td>| 列名 | 列名 | 列名 |<br>| ------ | ------ | ------ |<br>| 项内容 | 项内容 | 项内容 |<br>| 项内容 | 项内容 | 项内容 |</td>
</tr>
<tr>
<td>方程式</td>
<td>行内方程式 \$Equations\$<br><br>展示方程式: \$\$Equations\$\$</td>
</tr>
<tr>
<td>图片</td>
<td>无标题: ![](图片链接)<br><br>有标题: ![可选标题](图片地址)</td>
</tr>
<tr>
<td>引用块</td>
<td>> 输入你引用的内容</td>
</tr>
<tr>
<td>其他</td>
<td>上角标:文本内容^上角标^<br><br>章或页水平分割线<br><br>========= 或 ----------<br><br>行尾输入两个以上空格,即可添加人工行分割</td>
</tr>
</tbody>
</table>
#### YAML 头
在一个 R Markdown 文档的顶部,有一个 YAML 头,被两行 `------` 包围。这个块定义了文档的最终样式,通常包含一个标题、作者、日期和你想输出的文件类型。
支持的文件格式有三种:**HTML、PDF 和 Word**。
```
---
title: "Sample"
author: "It's FOSS"
date: "2023-02-08"
output: pdf_document
---
```
这可以在 RStudio 中设置新文件时添加,如上节所示。
#### 标题
在 R Markdown 中,有两种方法指定标题。我们可以使用 `#` 字符来表示不同级别的标题,比如:
```
# Heading Level 1
## Heading Level 2
### Heading Level 3
#### Heading Level 4
##### Heading Level 5
###### Heading Level 6
```
也可以用 `=``-` 分别表示一级和二级标题。
```
一级标题
===============
二级标题
---------------
```
![rmarkdown 文件中不同等级的标题][11]
#### 列表
有两种列表,一种是**无序列表**,用点句符来表示:
```
* Item 1
* Item 2
+ Sub 1
+ Sub 2
* Item 3
```
另一种是**有序列表**,用数字来排序:
```
1. Item 1
2. Item 2
+ Sub 1
+ Sub 2
3. Item 3
```
![有序和无序列表示例][12]
**推荐阅读 📖**
在 Linux 终端用 Glow 阅读和组织 Markdown 文件。Glow 是一个 CLI 工具,能在 Linux 终端渲染 Markdown 文件。你还可以用它来组织 Markdown 文件。
![][13]
![][14]
#### 段落中的文本格式
格式化文本有几种方式。
![][15]
你可以使用斜体或加粗来着重表示文本:
- 斜体:在文本前后各输入一个星号或下划线
- 加粗:在文本前后各输入两个星号或下划线
```
*这里是斜体内容*
_这里是斜体内容_
**这里是加粗内容**
__这里是加粗内容__
```
你可以阅读我们的文章 [Markdown 中怎么让内容变成加粗和斜体][16] 来了解更多内容.
如果你想使用上角标,在想变成上角标的内容前后加上 `^` 符号。
```
普通文本内容^上角标^
```
如果你想对文本内容加删除线,在文本前后加 `~~` 符号。
```
~~被删除的内容~~
```
![][17]
#### 添加代码块
内嵌代码 是 R Markdown 最主要的设计目的。我们有几种添加代码的方式。
**添加普通代码块**
如果你想添加一个代码块来与其他的文本进行区分,可以使用下面的语法:
````
```
这里输入你的代码
```
````
你也可以尝试 [对添加的代码进行高亮显示][18].
如果你想添加代码并将其输出嵌入到文档中,你可以在后面加上语言,并用大括号包裹:
```{语言}
这里输入你的代码
```
你可以用 ` 符号来添加行内代码。
```
这里是 `行内代码`
```
它看起来是这样的:
![][19]
#### 链接
如果想添加普通文本链接,把它粘贴到行内就可以了。
```
https://itsfoss.com
```
添加超链接,使用下面的语法:
```
[文本内容](URL 地址)
```
当你想链接到本页内的某个锚点时,使用下面的语法:
```
[文本内容](#锚点名称)
```
![][20]
#### 表格
表格的语法与 markdown 相似:
```
|列表|列表|列表|
| --- | --- | --- |
|表格项|表格项|表格项|
|表格项|表格项|表格项|
```
![][21]
📋
还想了解更多?请阅读我们的 [用 Markdown 创建表格][22] 指南。
.
#### 图片
添加图片使用下面的语法:
```
![](http://example.com/logo.png)
![可选显示文本](figures/img.png)
```
![][23]
#### 块引用
RMarkdown 可以添加块引用。在被引用的行或段落前添加 **>(大于号)**。
```
这里是普通文本内容。
> 这里是块引用
```
![][24]
如果你想了解更多块引用的内容,请阅读我们的 [Markdown 引用][25] 指南。
#### 方程式
你可以用 RMarkdown 来添加方程式和展示复杂的 LaTex 方程式。
例如:
```
行内毕达哥拉斯定理: $Equation$
展示方程式:$$Equation$$
```
![在 R Markdown 文档中添加方程式][26]
#### 章或页水平分割线
使用三个以上星号或减号来添加章或页水平分割线。
```
************
------------
```
你可以在行尾添加两个以上的空格来添加人工行分割。
### 总结
### R Markdown 非常有用(备忘录)
![][27]
[R Markdown 备忘录.pdf 下载文档][28]
_💬 你还知道其他关于 R Markdown 的内容吗请在下面的评论区告诉我们。_
--------------------------------------------------------------------------------
via: https://itsfoss.com/r-markdown/
作者:[Sreenath][a]
选题:[lkxed][b]
译者:[lxbwolf](https://github.com/lxbwolf)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sreenath/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/markdown-guide/
[2]: https://www.r-project.org/nosvn/pandoc/knitr.html
[3]: https://itsfoss.comknitr, and Pandoc
[4]: https://itsfoss.com/best-modern-open-source-code-editors-for-linux/
[5]: https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png
[6]: https://itsfoss.com/content/images/wordpress/2019/06/install-r-on-ubuntu.jpg
[7]: https://itsfoss.com/content/images/2023/02/select-install-packages.png
[8]: https://itsfoss.com/content/images/2023/02/install-rmarkdown-1.png
[9]: https://itsfoss.com/content/images/2023/02/new-r-markdown.png
[10]: https://itsfoss.com/content/images/2023/02/new-document-in-rmark.png
[11]: https://itsfoss.com/content/images/2023/02/Headings.png
[12]: https://itsfoss.com/content/images/2023/02/List.png
[13]: https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png
[14]: https://itsfoss.com/content/images/wordpress/2022/01/glow-cli-markdown.png
[15]: https://itsfoss.com/content/images/2023/02/emphasis.png
[16]: https://itsfoss.com/markdown-bold-italic/
[17]: https://itsfoss.com/content/images/2023/02/superescript.png
[18]: https://itsfoss.com/markdown-code-block/
[19]: https://itsfoss.com/content/images/2023/02/code-chunk.png
[20]: https://itsfoss.com/content/images/2023/02/links.png
[21]: https://itsfoss.com/content/images/2023/02/table.png
[22]: https://itsfoss.com/markdown-table/
[23]: https://itsfoss.com/content/images/2023/02/images.png
[24]: https://itsfoss.com/content/images/2023/02/block-quotes.png
[25]: https://itsfoss.com/markdown-quotes/
[26]: https://itsfoss.com/content/images/2023/02/equations.png
[27]: https://itsfoss.com/content/images/2023/02/R-Markdown-Cheat-Sheet.webp
[28]: https://itsfoss.com/content/files/2023/02/R-Markdown-Cheat-Sheet.pdf