Masking methods in R

后端 未结 2 1587
感动是毒
感动是毒 2021-02-07 06:32

This question and in particular this answer brought up the following question: How can I get a warning about the masking of methods in R?

If you run the following code

2条回答
  •  长发绾君心
    2021-02-07 07:22

    The conflicted package (see here) now offers a potential solution to this problem. With conflicted loaded, you get more explicit error messages about conflicting function names. You also can use conflict_prefer (details here) to specify which package's function you want to use by default and which should be masked.

    For example, here is a recent error I got when attempting to use the function parallel from the nFactors package:

    # Error: [conflicted] `parallel` found in 2 packages.
    # Either pick the one you want with `::` 
    # * nFactors::parallel
    # * lattice::parallel
    # Or declare a preference with `conflict_prefer()`
    # * conflict_prefer("parallel", "nFactors")
    # * conflict_prefer("parallel", "lattice")
    

    I then added

    conflict_prefer("parallel", "nFactors") 
    

    right after the code loading my libraries at the beginning of the script to make sure that parallel would call nFactors::parallel in my code.

提交回复
热议问题