R语言学习之<代码规范>

R语言的代码规范

R作为一个高级程序语言主要是用来统计计算和做图。对R语言代码进行规范便于代码的易于阅读、分享和验证。下面这些规范整理自google的R社区共同讨论的结果(Google'sR Style Guide)

 

1、一般性规则

避免使用attach

写函数是尽量少的使用stop()

定义S3和S4的对象不要混在一起使用

 

2、文件命名

以.r结束文件,尽可能的增加信息在文件名里面,比如

Good:

predict_ad_revenue.R

Bad:

foo.R

 

3、变量名和函数命名规则

注意,在R环境下,大小写是敏感的。变量名应该都用小写字母,单词间用.分隔;函数名用每个单词用大写字母开头,不用.连接。常数项跟函数一样命名但以小k开头。

变量:

Good:

avg.clicks

Bad:

avg_Clicks, avgClicks

函数名:

Good:

CalculateAvgClicks

Bad:

calculate_avg_clicks, calculateAvgClicks

用动词命名函数

常数项:kConstantName

 

4、R语句

这个可能是最快的,使代码外观变”漂亮”的规则了

每行最长80个字符。

代码缩进时,空两格。

二次运算符(=,+,-,<-,等)两端用空格。

逗号前不用空格,但逗号后一定得用。

GOOD:

tabPrior <- table(df[df$daysFromOpt< 0, "campaignid"])

total <- sum(x[, 1])

total <- sum(x[1, ])

BAD:

tabPrior <-table(df[df$daysFromOpt<0,"campaignid"])  # Needs spaces around'<'

tabPrior <- table(df[df$daysFromOpt< 0,"campaignid"])  # Needs a spaceafter the comma

tabPrior<- table(df[df$daysFromOpt< 0, "campaignid"])  # Needs aspace before <-

tabPrior<-table(df[df$daysFromOpt< 0, "campaignid"])  # Needs spacesaround <-

total <- sum(x[,1])  # Needs aspace after the comma

total <- sum(x[ ,1])  # Needs aspace after the comma, not before

 

这里包含了赋值、逻辑符号以及逗点分隔。

# 在R里面尽量少用 =

括号左边用空格,调用函数时除外。

Good:

if (debug)

Bad:

if(debug)

 

可以有多余的空格(一行中有多个空格),如果这能够改善符号间的对齐。

plot(x    =xCoord,

       = dataMat[,makeColName(metric, ptiles[1], "roiOpt")],

    ylim = ylim,

    xlab = "dates",

    ylab = metric,

    main = (paste(metric, " for 3 samples ", sep="")))


括号或方括号内不用空格,逗号后面除外。

GOOD:

if (debug)

x[1, ]

BAD:

if ( debug )  # No spaces around debug

x[1,]  # Needs a space after the comma

 

大括号{ 开始的{不能单独成行,结束的}必须单独成行。

 

单独的一个语句应从新的一行开始。

BAD:

if (is.null(ylim)) ylim <- c(0, 0.06)

if (is.null(ylim)) {ylim <- c(0, 0.06)}

 

5、代码组织

尤其是做项目的话,以下信息是必须有的:

版权声明

作者注释

文件说明,项目目的,输入和输出的说明

source() 和 library() 说明

函数定义

其他

 

6、注释

养成良好的注释习惯

单行注释以 # 开头,加一个空格

短注释需要在代码后面空两格,然后 # ,再加一个空格

# Create histogram of frequency of campaigns by pct budgetspent.

hist(df$pctSpent,

    breaks = "scott",  # method for choosing number ofbuckets

    main   = "Histogram: fractionbudget spent by campaignid",

    xlab   = "Fraction of budgetspent",

    ylab   = "Frequency (count ofcampaignids)")


7、函数定义与调用

函数定义应该先列出语句中没有默认值,然后是默认值。在函数定义和调用时,允许每行多个语句,一般只是在赋值时才换行。

GOOD:

PredictCTR <- function(query, property,numDays,

                      showPlot= TRUE)

BAD:

PredictCTR <- function(query, property, numDays,showPlot =

                      TRUE)

 

8、 函数归档

应该在函数定义的下一行进行注释,包括描述函数的作用;描述语句中的参数用Args:描述数据类型;描述返回值用Returns:。注释部分应尽可能详细,以便读者再不看代码时就能应用该函数。比如:

Example Function

CalculateSampleCovariance <- function(x, y,verbose = TRUE) {

  # Computes the sample covariance between twovectors.

  #

  # Args:

   x: Oneof two vectors whose sample covariance is to be calculated.

   y: Theother vector. x and y must have the same length, greater thanone,

     with no missing values.

  verbose: If TRUE, prints sample covariance; if not, not. Default isTRUE.

  #

  # Returns:

   Thesample covariance between x and y.

  n <- length(x)

  # Error handling

  if (n <= 1 || n != length(y)){

   stop("Arguments x and y have invalid lengths: ",

        length(x), " and ", length(y), ".")

  }

  if (TRUE %in% is.na(x) || TRUE %in% is.na(y)){

    stop("Arguments x and y must not have missing values.")

  }

  covariance <- var(x, y)

  if (verbose)

   cat("Covariance = ", round(covariance, 4), ".\n", sep = "")

  return(covariance)

}

 

9、任务间代码风格应保持一致

TODO(username): Explicit description of action to be taken

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值