Function naming for R packages -




i writing r package , avoid using function names found in other packages. example, planned call function 'annotate', has been used in nlp package. evidently best avoid obvious name choices, there systematic way search exhaustive list of cran published function names avoid duplication? appreciate important cran shared packages can relevant when sharing locally in case there conflict loaded package.

name clashes occur when 2 packages loaded contain functions same name. so, name clashes can avoided @ 2 places:

  • when defining function names in package
  • when calling functions package

creating functions unique names

at time of writing (23 aug 2017), incredible number of 11272 packages available on cran (the latest figure can found here) , new packages being added every day.

so, creating function names unique today may cause name clashes in future when other packages added.

alistaire has mentioned option prefix functions. besides stringi , stringr, forcats packages example uses prefixes fct_ , lvls_.

this approach may reduce probability of name clashes.

(although it's not guaranteed no other package maintainer might choose same prefix.)

calling functions unambiguously using double colon operator

imho, ultimate responsibility avoiding name clashes user's.

i've seen questions here on more half dozen of packages being loaded. or, library(tidyverse) called convenience, loading 19 other packages dplyrand tidyr have been sufficient.

cluttering namespace many loaded packages increases risk of name clashes. , 2 packages loaded, name clashes might occur. instance, lubridate , data.table package both have defined

hour, isoweek, mday, minute, month, quarter, second, wday, week, yday, year 

which function being called depend on order packages have been loaded. (you may use conflicts() find objects exist same name in 2 or more places on search path.)

to avoid ambiguities , unexpected results, suggest load few packages possible , use double colon operator ?"::" call functions packages without loading package beforehand, e.g.,

library(data.table) dt <- data.table(t = lubridate::now() + 0:3) # call function loaded package data.table dt[, second(t)]  
[1] 18 19 20 21 
# call function lubridate package dt[, lubridate::second(t)] 
[1] 18.88337 19.88337 20.88337 21.88337 

there benefit using double colon operator. serve documentation within code package function being called.

this comes @ expense of few additional key strokes may save lot of time when code inspected, amended, or debugged weeks or years later. i've seen many questions on op hasn't mentioned package.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -