Official guidelines for using functions newly added to base R

元气小坏坏 提交于 2019-12-12 01:44:08

问题


I am writing a package that performs a statistical analysis while handling missing values. I am using the wonderful, life-changing function anyNA which was added sometime after 3.0 (commit). Another recently added function that people might want to use is OlsonNames.

So as I am using this function, my package won't work on older versions of R. I see four options for dealing with this.

  1. Make the whole package depend on R >= 3.1 in DESCRIPTION.

  2. Redefine the function in my source.

  3. Redefine the function if the user is using <3.1 and don't define it if they are using >= 3.1 or make the function check the version each time e.g.

    anyNA <- function(x)
      if(as.numeric(R.Version()$minor) > 3.1){
        return(anyNA(x)
      } else {
        return(any(is.NA(x))
      }
    }
    

    or

    if(as.numeric(R.Version()$minor) > 3.1){
      anyNA <- base::anyNA
    } else {
      anyNA <- function(x) any(is.na(x))
    }          
    

    I'm not even sure this second one would work in package source code.

  4. Rewrite my code using any(is.na(x)).

My concrete question is is there an official CRAN preference for one of these?

Failing that, are there good reasons to use one over the others? To my eyes they all have failings. 1) It seems unnecessary to require users have R >= 3.1 for the sake of a small function. 2) If I redefine the function, any improvements made to the function in R base won't get used in my package. 3) This mostly seems messy. But also, if the base R version of the function changes I might end up with hard to fix bugs that only occur in certain R versions. 4) Code readability is reduced.

来源:https://stackoverflow.com/questions/35226041/official-guidelines-for-using-functions-newly-added-to-base-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!