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版

第 4 章 工作流程 工作流程:代码风格

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

良好的编码风格就像正确的标点符号:没有它你也能应付自如,但它却能让你更容易阅读。即使是一个新手程序员,也不妨在代码风格上下功夫。使用统一的风格会让其他人(包括未来的你!)更容易阅读你的作品,如果你需要别人的帮助,这一点尤为重要。本章将介绍贯穿本书的tidyverse 风格指南的要点。

代码样式化一开始会感觉有些繁琐,但如果多加练习,很快就会成为第二天性。此外,还有一些很棒的工具可以快速调整现有代码的样式,比如 Lorenz Walthert 的styler软件包。通过install.packages("styler") 安装后,使用它的简单方法是通过RStudio 的命令调板。通过命令调板可以使用任何内置的 RStudio 命令和许多软件包提供的插件。按 Cmd/Ctrl+Shift+P 打开调板,然后输入styler查看 styler 提供的所有快捷方式。图 4-1显示了结果。

A screenshot showing the command palette after typing "styler", showing the four styling tool provided by the package.

图 4-1. 通过 RStudio 的命令调色板,只需使用键盘就能轻松访问每个 RStudio 命令。

在本章中,我们将使用 tidyverse 和 nycflights13 软件包进行代码示例。

library(tidyverse)
library(nycflights13)

名称

我们在"名字有什么用?请记住,变量名(由<- 创建的变量名和由 mutate()创建的变量)只能使用小写字母、数字和_ 。使用_ 分隔名称中的单词。

# Strive for:
short_flights <- flights |> filter(air_time < 60)

# Avoid:
SHORTFLIGHTS <- flights |> filter(air_time < 60)

一般来说,最好选择描述性强、易于理解的长名称,而不是输入速度快的简短名称。在编写代码时,简短的名称节省的时间相对较少(尤其是自动完成功能可以帮助您完成键入),但当您回到旧代码时,却不得不费时费力地拼写一个神秘的缩写。

如果你有一堆相关事物的名称,请尽量保持一致。如果你忘记了以前的约定,就很容易出现不一致的情况,所以如果你不得不回头重新命名,也不要感到难过。一般来说,如果你有一堆主题不同的变量,最好给它们起一个共同的前缀,而不是共同的后缀,因为自动完成功能在变量的开头效果最好。

空间

^ 以外的数学运算符两侧(即+,-,==,<, ......)和赋值运算符 (<-) 的周围加上空格。

# Strive for
z <- (a + b)^2 / d

# Avoid
z<-( a + b ) ^ 2/d

对于常规函数调用,不要在括号内外留空格。逗号后一定要有空格,就像标准英语一样。

# Strive for
mean(x, na.rm = TRUE)

# Avoid
mean (x ,na.rm=TRUE)

如果可以改善对齐,添加额外的空格也是可以的。例如,如果您在 mutate()中创建多个变量时,您可能需要添加空格,以便所有= 都能对齐。1这样可以更方便地浏览代码。

flights |> 
  mutate(
    speed ...
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