R Markdown allows the user to integrate R code into a report R Markdown 允许用户将 R 代码集成到报告中
When data changes or code changes, so does the report 当数据更改或代码更改时,报告也会更改
No more need to copy-and-paste graphics, tables, or numbers 不再需要复制和粘贴图形、表格或数字
Creates reproducible reports 创建可重复的报告
Anyone who has your R Markdown (.Rmd) file and input data can re-run your analysis and get the exact same results (tables, figures, summaries) 任何拥有你的 R Markdown (.Rmd) 文件和输入数据的人都可以重新运行你的分析并获得完全相同的结果(表格、数字、摘要)
Can output report in HTML (default), Microsoft Word, or PDF 可以以 HTML(默认)、Microsoft Word 或 PDF 格式输出报告
To turn an Rmd file into a report, click the Knit button in the Source pane menu bar 要将 Rmd 文件转换为报告,请单击 “源” 窗格菜单栏中的 “Kint” 按钮
The results will appear in a new window 结果将出现在新窗口中
You can knit into html (default), MS Word, and pdf format 您可以 Kint 成 html(默认)、MS Word 和 pdf 格式
To integrate R output into your report, you need to use R code chunks 要将 R 输出集成到您的报告中,您需要使用 R 代码块
All of the code that appears in between the "triple back-ticks" gets executed when you Knit 出现在 “三重反引号” 之间的所有代码在您编织时都会执行
Pro tip: The ideas here apply to Python just as well, but the syntax is slightly different. We will cover those differences in a later lecture. 这些同样适用于 Python,但语法略有不同。我们将在后面的讲座中介绍这些差异。
Everything we'll do comes down to applying functions to data 我们要做的一切都归结为将函数应用于数据
Data: things like 7, "seven", 7.000, the matrix
[777777]
Functions: things like log, + (two arguments), < (two), mod (two), mean (one)
A function is a machine which turns input objects (arguments) into an output object (return value), possibly with side effects, according to a definite rule 函数是一台机器,它根据确定的规则将输入对象(参数)转换为输出对象(返回值),可能有副作用。
is.foo() functions return Booleans for whether the argument is of type foo 返回布尔值以确定参数是否为 foo 类型
as.foo() (tries to) "cast" its argument to type foo --- to translate it sensibly into a foo-type value (尝试)将其参数 “强制转换” 为类型 foo --- 将其合理地转换为 foo 类型值
Special case: as.factor() will be important later for telling R when numbers are actually encodings and not numeric values. (E.g., 1 = High school grad; 2 = College grad; 3 = Postgrad) as.factor() 在数字实际上是编码而不是数值时很重要。 (例如,1 = 高中毕业生;2 = 大学毕业生;3 = 研究生)
Most variables are created with the assignment operator, <- or = 大多数变量是使用赋值运算符创建的
time.factor <-12
time.factor
[1] 12
time.in.years =2.5
time.in.years * time.factor
[1] 30
The assignment operator also changes values: 赋值运算符还会更改值:
time.in.months <- time.in.years * time.factor
time.in.months
[1] 30
time.in.months <-45
time.in.months
[1] 45
Using names and variables makes code: easier to design, easier to debug, less prone to bugs, easier to improve, and easier for others to read 使用名称和变量使代码:更容易设计,更容易调试,更不容易出错,更容易改进,更容易被其他人阅读
Avoid "magic constants"; use named variables 避免 “魔法常数”;使用命名变量
Just like every other programming language you may be familiar with, R's capabilities can be greatly extended by installing additional "packages" and "libraries". 就像你可能熟悉的所有其他编程语言一样,R 的功能可以通过安装额外的 “包” 和 “库” 得到极大的扩展。
To install a package, use the install.packages() command. You'll want to run the following commands to get the necessary packages for today's lab: 要安装软件包,请使用该 install.packages() 命令。您需要运行以下命令来获取今天 lab 所需的包:
install.packages("rmdformats")
install.packages("ggplot2")
install.packages("knitr")
You only need to install packages once. Once they're installed, you may use them by loading the libraries using the library() command. For today's lab, you'll want to run the following code 您只需要安装一次软件包。安装后,可以通过使用 library() 命令加载库来使用它们。对于今天的 lab,您需要运行以下代码