Skip to Content
R在数据科学中的应用,第2版
book

R在数据科学中的应用,第2版

by Hadley Wickham, Mine Cetinkaya-Rundel, Garrett Grolemund
May 2025
Intermediate to advanced
578 pages
8h 9m
Chinese
O'Reilly Media, Inc.
Content preview from R在数据科学中的应用,第2版

第 6 章 工作流程 工作流程:脚本和项目

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章将向你介绍组织代码的两个基本工具:脚本和项目。

脚本

到目前为止,您已经使用控制台运行代码。这是一个很好的开始,但当你创建更复杂的 ggplot2 图形和更长的 dplyr 管道时,你会发现它很快就会变得拥挤不堪。为了给自己更多的工作空间,请使用脚本编辑器。单击 "文件 "菜单,选择 "新建文件",然后选择 "R 脚本",或者使用键盘快捷键 Cmd/Ctrl+Shift+N,打开脚本编辑器。现在你会看到四个窗格,如图 6-1 所示。脚本编辑器是试验代码的好地方。当你想修改某些内容时,不必重新输入整个代码,只需编辑脚本并重新运行即可。一旦你编写的代码可以运行并达到你想要的效果,你就可以将其保存为脚本文件,方便日后返回。

RStudio IDE with Editor, Console, and Output highlighted.
图 6-1. 打开脚本编辑器后,集成开发环境的左上角会增加一个新窗格。

运行代码

脚本编辑器是绘制复杂 ggplot2 图形或进行长序列 dplyr 操作的绝佳场所。有效使用脚本编辑器的关键是记住一个最重要的键盘快捷键:Cmd/Ctrl+Enter。这会在控制台中执行当前的 R 表达式。例如,下面的代码

library(dplyr)
library(nycflights13)

not_cancelled <- flights |> 
  filter(!is.na(dep_delay), !is.na(arr_delay))

not_cancelled |> 
  group_by(year, month, day) |> 
  summarize(mean = mean(dep_delay))

如果光标位于"...... "处,按 Cmd/Ctrl+Enter 将运行生成not_cancelled 的完整命令。它还会将光标移到下面的语句(以not_cancelled |> 开头)。这样就可以通过反复按 Cmd/Ctrl+Enter 来轻松浏览完整的脚本。

您可以使用 Cmd/Ctrl+Shift+S 一步执行整个脚本,而不是逐个表达式运行代码。经常这样做可以确保在脚本中捕捉到代码的所有重要部分。

我们建议您始终使用所需的软件包启动脚本。这样,如果您与他人共享代码,他们就能很容易地看到需要安装哪些软件包。但请注意,千万不要在共享的脚本中包含 install.packages()。如果不小心将脚本安装到别人的电脑上,那就太不体贴了!

在学习未来章节时,我们强烈建议从脚本编辑器开始练习键盘快捷键。随着时间的推移,以这种方式向控制台发送代码将变得如此自然,以至于你根本不会去想它。

RStudio 诊断

在脚本编辑器中,RStudio 会在侧边栏中用红色斜线和叉号突出显示语法错误:

Script editor with the script x y <- 10. A red X indicates that there is syntax error. The syntax error is also highlighted with a red squiggly line.

将鼠标悬停在十字架上,查看问题所在:

RStudio 还会让您了解潜在的问题:

保存和命名

RStudio 会在退出时自动保存脚本编辑器的内容,并在重新打开时自动重新加载。不过,最好还是不要使用 Untitled1、Untitled2、Untitled3 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
AI工程

AI工程

Chip Huyen
Raku学习手册

Raku学习手册

brian d foy

Publisher Resources

ISBN: 9798341657304