本文介绍了你在R中命名变量的首选风格是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您喜欢在R代码中命名变量和函数的约定是什么?

Which conventions for naming variables and functions do you favor in R code?

据我所知,有几种不同的约定,所有这些约定在cacophonous和谐中并存:

As far as I can tell, there are several different conventions, all of which coexist in cacophonous harmony:

1。使用期间分隔符,例如

  stock.prices <- c(12.01, 10.12)
  col.names    <- c('symbol','price')

在R社区中具有历史优先级,在整个R核心中是普遍的,并且由。

缺点: Rife面向对象的内涵, R新手

Cons: Rife with object-oriented connotations, and confusing to R newbies

2。使用下划线

  stock_prices <- c(12.01, 10.12)
  col_names    <- c('symbol','price')

优点:在许多编程语言;受青睐,并在ggplot2和plyr包。

Pros: A common convention in many programming langs; favored by Hadley Wickham's Style Guide, and used in ggplot2 and plyr packages.

缺点: R程序员在历史上没有使用;被烦人地映射到Emacs-Speaks-Statistics中的'< - '运算符(可以用ess-toggle-underscore修改)。

Cons: Not historically used by R programmers; is annoyingly mapped to '<-' operator in Emacs-Speaks-Statistics (alterable with 'ess-toggle-underscore').

3。使用混合大写(camelCase)

  stockPrices <- c(12.01, 10.12)
  colNames    <- c('symbol','price')

>似乎在多个语言社群中广泛采用。

Pros: Appears to have wide adoption in several language communities.

缺点:有最近的先例,但没有历史使用文档)。

Cons: Has recent precedent, but not historically used (in either R base or its documentation).

最后,就像它不够混乱,我应该指出,Google Style Guide认为变量的点符号,但混合大写函数。

Finally, as if it weren't confusing enough, I ought to point out that the Google Style Guide argues for dot notation for variables, but mixed capitalization for functions.

在R包中缺少一致的样式在几个层次上是有问题的。从开发人员的角度来看,它使得维护和扩展其他代码变得困难(尤其是它的风格与你自己的风格不一致)。从R用户的角度来看,不一致的语法通过乘以一个概念的表达方式(例如,日期转换函数asDate(),as.date()或as_date()?)来歪曲R的学习曲线。

The lack of consistent style across R packages is problematic on several levels. From a developer standpoint, it makes maintaining and extending other's code difficult (esp. where its style is inconsistent with your own). From a R user standpoint, the inconsistent syntax steepens R's learning curve, by multiplying the ways a concept might be expressed (e.g. is that date casting function asDate(), as.date(), or as_date()? No, it's as.Date()).

推荐答案

好的以前的答案,所以只需要在这里添加:

Good previous answers so just a little to add here:


  • 下划线对于ESS用户来说是真的烦人;考虑到ESS使用得很广泛,你不会在ESS用户编写的代码中看到许多下划线(并且该集合包括一大堆R核心以及CRAN作者,像Hadley这样的排序);

  • underscores are really annoying for ESS users; given that ESS is pretty widely used you won't see many underscores in code authored by ESS users (and that set includes a bunch of R Core as well as CRAN authors, excptions like Hadley notwithstanding);

点也是邪恶的,因为他们可以在简单的方法调度中混淆;我相信我曾经对R列表中的一个阅读了这样的评论:点是一个历史文物,不再鼓励;

dots are evil too because they can get mixed up in simple method dispatch; I believe I once read comments to this effect on one of the R list: dots are a historical artifact and no longer encouraged;

仍然站在最后一轮:camelCase。我也不确定我是否真的同意R社区缺乏前身的说法。

so we have a clear winner still standing in the last round: camelCase. I am also not sure if I really agree with the assertion of 'lacking precendent in the R community'.

是的:实用主义和一致性tr ump教条。所以无论什么工作,并由同事和合作者使用。毕竟,我们还有空格和大括号来争论:)

And yes: pragmatism and consistency trump dogma. So whatever works and is used by colleagues and co-authors. After all, we still have white-space and braces to argue about :)

这篇关于你在R中命名变量的首选风格是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:14