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语言编程
178
9
load data.R
Writing R Extensions
”的
1.1
节提供有关文件名的更详细的指导,例如避免
使用非英语字母,因为它们不能保证所有地方都正常工作。虽然指导原则很
严格但使脚本移植更方便。
加载包
应在你的代码开始处调用库函数。当加载必要包时,使用 library而不要
使用 require,因为缺包时 library 会抛出错误。如果一个包不是必须的,
使用 require 并在代码中相应的捕获报警异常。
包名应该使用引号包裹。
#
好的用法
library("dplyr")
#
非标准用法
library(dplyr)
应避免列出所有你可能需要的包;而是仅包含你实际使用的包。如果你发现
你正在加载很多包,不妨考虑将所有包名放到一个
packages.R
文件中,并恰
当地使用 source 调用。
注释
注释能帮助每个人理解每行代码的含义,从而极大改善协作项目的效率。然而,
注释应该仔细使用,你的代码布满了注释并未使其更高效,相反太多注释降
低了效率。升级有大量注释的代码是很痛苦的,你不仅仅需要改变所有的
R
代码,还需要重写或者删除所有的注释!
确保你的注释有具体含义。避免使用无意义的英语解释标准
R
代码。例如下
面的注释添加了无用的信息,因为通过阅读代码就可清楚看到 i 设置成了
1
# Setting x equal to 1
x = 1
相反,注释应该提供语境。假如 x 被用作计数器(此时,它应该有一个有意
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