R语言代码规范

编码风格是开发人员的偏好,因此没有“正确”的编码样式,但在团队合作中, 需要行成统一的编码风格,以便团队中的其他人理解。尽管没有官方的样式指南,R仍然可以有一个“非正式”的约定俗成的编码规范。 在本文中,您将学习这些“非正式”规则,它们的偏差以及最常见的样式。

目录

一、命名

1.命名文件

2.命名变量

3.命名函数

二、句法

1. 页面长度

2.空格 

3.大括号

4.缩进

5.新行

6.注释

其他

References


一、命名

1.命名文件

  • 文件名应该以.R为后缀
  # Good
  read.R

  # Bad 
  read
  • 文件名应该有实际含义,最好小写
  # Good 
  model.R
    
  # Bad
  Untitled1.R
  • 使用有实际含义的动词
  # Good 
  validate-vbm.R
    
  # Bad
  regression.R

 

2.命名变量

变量名称尽可能地短

  # Good 
  fit_rt
  split_1
  imdb_page
    
  # Bad
  fit_regression_tree
  cross_validation_split_one
  foo

变量名称应该小写

  # Good 
  event
    
  # Bad 
  Event

使用下划线分割变量名,不要用“.”分割

  # Good 
  event_window
    
  # Bad 
  event.window
  EventWindow

不要使用已有的函数或变量命名

  # Bad
  T <- 10 # T is a shortcut of TRUE in R
  c <- "constant"

3.命名函数

函数名称应该为动词

  # Good
  add()
    
  # Bad
  addition()

用下划线分割函数名

  # Good 
  bw_test()
    
  # Bad
  bw.test()

二、句法

1. 页面长度

行的最大长度限制为80个字符

2.空格 

在所有二进制运算符周围放置空格(=,*,+,<-,==,%%等)

  # Good 
  x == y
  a <- a ^ 2 + 1
    
  # Bad
  x==y
  a<-a^2+1

$,@及::等前不需要空格

  # Good 
  car$cyl
  dplyr::select
  1:10
    
  # Bad
  car $cyl
  dplyr:: select
  1: 10

逗号前需要空格

  # Good 
  mtcars[, "cyl"]
  mtcars[1, ]
  mean(x = c(1, NA, 2), na.rm = TRUE)
    
  # Bad
  mtcars[,"cyl"]
  mtcars[1 ,]
  mean(x = c(1, NA, 2),na.rm = TRUE)

除函数调用外,在左括号前使用空格

  # Good 
  for (element in element_list)
  if (grade == 5.5)
  sum(1:10)
    
  # Bad
  for(element in element_list)
  if(grade == 5.5)
  sum (1:10)

括号或方括号中的代码之间没有空格

  # Good 
  if (debug) message("debug mode")
  species["tiger", ]
    
  # Bad
  if ( debug ) message("debug mode")
  species[ "tiger" ,]

3.大括号

左大括号开头绝对不能单行,而后总是换行

  # Good 
  if (is_used) {
      # do something
  }
    
  if (is_used) {
      # do something
  } else {
      # do something else
  }
    
  # Bad
  if (is_used)
  {
      # do something
  }
    
  if (is_used) { # do something }
  else { # do something else }

右大括号应始终沿自己的行,除非后面紧跟其他行。

  # Good 
  if (is_used) {
      # do something
  } else {
      # do something else
  }
    
  # Bad
  if (is_used) {
      # do something
  }
  else {
      # do something else 
  }
  • 始终在花括号内缩进代码(请参阅下一节)

    # Good
    
    if (is_used) {
    
    # do something
    
    # and then something else
    
    }
    
    # Bad
    
    if (is_used) {
    
    # do something
    
    # and then something else
    
    }

     

  • 如果if后的语句非常少,可以直接跟在后面

    # Good
    
    if (is_used) return(rval)

     

4.缩进

 

  • 切忌无缩进或缩进与空格混用
  • 快捷键Ctrl+I

5.新行

  • 通常,函数定义不适合一行。 在这种情况下,应从左括号开始将过多的参数移到新行。

long_function_name <- function(arg1, arg2, arg3, arg4,

long_argument_name1 = TRUE)

 

  • 如果参数扩展到两行以上,则每个参数应放在单独的行上。

    long_function_name <- function(long_argument_name1 = c("value1", "value2"),
    
    long_argument_name2 = TRUE,
    
    long_argument_name3 = NULL,
    
    long_argument_name4 = FALSE)

     

  • 对于函数调用也是如此:如果只有两行就足够了,则多余的参数应缩进右括号所在的位置。

    plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
    
    main = "rpois(100, lambda = 5)")
    
    

     

  • 否则,每个参数都可以进入单独的行,从左括号后的新行开始。

    list(
    
    mean = mean(x),
    
    sd = sd(x),
    
    var = var(x),
    
    min = min(x),
    
    max = max(x),
    
    median = median(x)
    
    )

     

  • 如果if语句中的条件扩展为多行,则每个条件都应以逻辑运算符结尾,而不是以它开头。

# Good

if (some_very_long_name_1 == 1 &&

some_very_long_name_2 == 1 ||

some_very_long_name_3 %in% some_very_long_name_4)

# Bad

if (some_very_long_name_1 == 1

&& some_very_long_name_2 == 1

|| some_very_long_name_3 %in% some_very_long_name_4)

 

6.注释

  • 注释代码,以#开头 ,增强代码可读性

    # This is a comment.

  • 评论应说明原因,而不是内容。 注释不应用简单的语言来复制代码,而应解释命令的总体意图。

    # Good
    
    # define iterator
    
    i <- 1
    
    # Bad
    
    # set i to 1
    
    i <- 1
    
    

     

  • 简短注释可以与代码行放在同一行

    plot(price, weight) # plot a scatter chart of price and weight

     

  • 注释的切换快捷键 Command+Shift+C

其他

  • 使用 <-而不是=进行赋值

  • 使用library()而不是require()进行包的加载
     
    # Good
    
    library("dplyr")
    
    # Bad
    
    require(dplyr)
    
    

     

  • 在函数调用中,可以通过位置,完整名称或部分名称来指定参数。 永远不要用部分名称指定,也不要按位置和完整名称混合。

    # Good
    
    mean(x, na.rm = TRUE)
    
    rnorm(10, 0.2, 0.3)
    
    # Bad
    
    mean(x, na = TRUE)
    
    rnorm(mean = 0.2, 10, 0.3)

     

  • 开发软件包时,请按名称指定参数,必需的参数(无默认值)应位于第一个,然后是可选参数。

  • # Good
    
    raise_to_power(x, power = 2.7)
    
    # Bad
    
    raise_to_power(power = 2.7, x)
  •  ...符号应该要么在开头要么在结尾

    # Good
    
    standardize(..., scale = TRUE, center = TRUE)
    
    save_chart(chart, file, width, height, ...)
    
    # Bad
    
    standardize(scale = TRUE, ..., center = TRUE)
    
    save_chart(chart, ..., file, width, height)

     

  • 开发软件包时,请指定每个使用的函数的名称空间,除非它来自基础软件包。

  • 每行不要放置超过一个语句(命令)。 不要使用分号作为命令的终止符。

    # Good
    
    x <- 1
    
    x <- x + 1
    
    # Bad
    
    x <- 1; x <- x + 1
    
    

     

  • 避免使用setwd("/Users/irudnyts/path/that/only/I/have"),使用here包中的here::here()函数,以便其他人重复你的结果

  • 避免使用rm(list = ls()),该语句从全局环境中删除所有对象,重新开始R
  • 关键快捷键Command+Shift+A可以重排代码块,加入空格和缩进,但不要过度使用!还是需要自己练习

References

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值