R writing style - require vs. ::

后端 未结 2 2000
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 01:40

OK, we\'re all familiar with double colon operator in R. Whenever I\'m about to write some function, I use require(), but I was always thinking a

2条回答
  •  没有蜡笔的小新
    2020-12-31 02:26

    Since the time to load a package is almost always small compared to the time you spend trying to figure out what the code you wrote six months ago was about, in this case coding for clarity is the most important thing.

    For scripts, having a call to require or library at the start lets you know which packages you need straight away.

    Similarly, calling require (or a wrapper like requirePackage in Hmisc or try_require in ggplot2) at the start of a function is the most unambiguous way of showing that you need to use that package.

    :: should be reserved for cases when you have naming conflicts between packages – compare, e.g.,

    Hmisc::is.discrete
    

    and

    plyr::is.discrete
    

提交回复
热议问题