简体   繁体   中英

Conflicted package in R package building

I am creating my first package, here I am making some var estimations, the functions are running, however, I use packages that have the same function names.

Before writing the package I made an R script with the function and tests if it works, but at the top of my script I used the following code:

invisible(lapply(c("tibble","readxl","dplyr","stringr", "tidyr", "vars", "conflicted","forecast", "lubridate"), library, character.only = T))
conflict_prefer("select","dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("filter", "dplyr")

The conflicted package chose the functions select , lag , and filter comes from the dplyr package rather from the stats package.

So I have not figured out how to use the conflict_prefer function inside the package.

Should they be the first lines of my function?

There is a roxygen way to prefer same-name functions?

I ask this because I get this warning:

> devtools::load_all()
i Loading FAVAR.MEF
Warning messages:
1: replacing previous import ‘dplyr::filter’ by ‘stats::filter’ when loading ‘FAVAR.MEF’ 
2: replacing previous import ‘dplyr::lag’ by ‘stats::lag’ when loading ‘FAVAR.MEF’ 
3: replacing previous import ‘stats::filter’ by ‘dplyr::filter’ when loading ‘FAVAR.MEF’ 
4: In setup_ns_exports(path, export_all, export_imports) :
  Objects listed as exports, but not present in namespace: favar_est

Thanks in advance!!

If you are writing your own package and using external dependencies, you should not load them through repeated calls to library .

The proper way to do it is to state your dependencies in the DECRIPTION file of your package, which will mean that your dependencies are put on the search path in the correct order when your package is loaded. In your case, this removes the need for conflict_prefer , as dplyr will be higher up on the search path than stats . It also makes your package portable, because anyone who installs your package will have any missing dependencies installed automatically according to the packages listed in your DESCRIPTION file. Furthermore, doing it this way allows you to specify a minimum version of the dependency, so that anyone who already has an older version of the dependency installed will not come up against an obscure error when they try to use your package.

The DESCRIPTION file resides in the root directory of your package. It is a simple text file.

You need only add:

Depends:
  tibble,
  readxl,
  dplyr,
  stringr, 
  tidyr, 
  vars, 
  conflicted, 
  forecast, 
  lubridate

within this file, and your dependencies will be loaded with your package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM