R: 2 functions with the same name in 2 different packages

前端 未结 4 1328
無奈伤痛
無奈伤痛 2020-11-30 00:59

I need to load to R packages : tseries and chron

Both have a function named is.weekend

I always have in my environment the function from the second package I

4条回答
  •  死守一世寂寞
    2020-11-30 01:38

    you should turn to the conflicted package from Hadly.

    library(conflicted)
    library(dplyr)
    filter(mtcars, am & cyl == 8)
    

    Then the conflicted package will throw an error and will force you clearly determine which function you prefer:

    Error: filter found in 2 packages. You must indicate which one you want with :: * dplyr::filter * stats::filter

    To resolve conflicts for your entire session, use <-:

    filter <- dplyr::filter
    filter(mtcars, am & cyl == 8)
    
        mpg cyl disp  hp drat   wt qsec vs am gear carb
    1 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
    2 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
    

    You can also turn to the conflict_prefer() function which can determine the winner when a conflict occur. The code example is borrowed from Hadly, pls refer to the package website. https://www.tidyverse.org/blog/2018/06/conflicted/

提交回复
热议问题