Skip to Content
高效R语言编程
book

高效R语言编程

by Colin Gillespie, Robin Lovelace
August 2018
Intermediate to advanced
227 pages
4h 16m
Chinese
China Electric Power Press Ltd.
Content preview from 高效R语言编程
高效协作
177
使用
RStudio
格式化代码
RStudio
可自动清理不标准缩进和格式化代码。为了实现这点,选中要格式化
的代码行(例如通过
Ctrl-A
选择整个脚本),而后使用
Ctrl-I
自动缩进。快
捷键
Ctrl-Shift-A
将格式化代码,添加空格以保证最大的可读性。下面的例子
演示了该功能:
#
非标准缩进的代码
if(!exists("x")){
x=c(3,5)
y=x[2]}
该代码工作正常,但不适合阅读。
Rstudio
缩进 if 后面的代码,重排格式如下:
#
自动缩进代码
(RStudio
中的
Ctrl-I)
if(!exists("x")){
x=c(3,5)
y=x[2]}
这是一个好的开始,但仍旧不是很容易阅读。
RStudio
可进一步修正,下面的
代码演示该功能(可在
Code
菜单中找到这些选项,在
Windows/Linux
系统下
使用
Alt-C
热键打开
Code
菜单)
:
#
自动重新格式化代码
(
RStudio
中使用
Ctrl-Shift-A)
if(!exists("x")) {
x = c(3, 5)
y = x[2]
}
注意风格某些方面根据各人所好的,例如,我们不会 if ) 间留空格。
文件名
文件名应该使用
.R
后缀,并且应该小写(例如
load.R
)。避免使用空格。使
用破折号或下划线分割单词。
#
好的文件名
normalize.R
load.R
#
糟糕的文件名
Normalize.r
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进行数据清理、分析与可视化

迈克尔 弗里曼, 乔尔 罗斯
R数据科学

R数据科学

Hadley Wickham, Garrett Grolemund

Publisher Resources

ISBN: 9787519820855