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.
Related
I have created a test package which depends on few other packages data.table, dplyr,knitr, stringr etc. And i have mentioned all those under imports. I am after installing this test package only calling library(test). Which is giving error with the functions from these package like %>% from dplyr and with other functions also.
How can we fix this?
You also have to import the entire package or the specific functions in NAMESPACE. It is preferable to use importsFrom(pkg, func1, func2, etc.) to reduce possible name collisions. See https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Specifying-imports-and-exports
I am currently doing Logistic Regression on the 'birthwt' data set found within R. This data is found within the package 'MASS'.
However, when I use library(MASS) to retrieve the data, it masks the function of select() from the dplyr package. I use this function almost immediately in my analysis.
After loading the data, I attempt
detach("package:MASS", unload = TRUE)
but I am met with
‘MASS’ namespace cannot be unloaded:
namespace ‘MASS’ is imported by ‘pbkrtest’, ‘car’, ‘lme4’ so cannot be unloaded
I wold really love to sort this out as I have completed all my necessary analysis on the data, but was met with this issue when attempting to knit.
Thank you in advance for any help!
You shouldn't choose unload = TRUE. The default is unload = FALSE, and that's what you need.
Here's the explanation:
In R, packages can be "loaded", which makes them available to other packages that import functions from them. They can also be "attached", which puts them on the search list, so that they are available to the user in the console. If a package is attached, it needs to be loaded, but the reverse is not true.
So if you run detach("package:MASS"), you will remove it from the search list, and in the console, running select() will no longer find the function in MASS. It will still be loaded, so will be available to the other packages that need it.
By the way, using the prefix form MASS::select() or dplyr::select() will work regardless of whether either or both packages are in your search list.
I'm working on update of my package. However I'm struggling with dependencies/imports. I use two conflicted packages - ggplot2 and psych and their functions alpha and of course alpha object of ggplot2 differs from alpha function of psych package.
When building package I get an warning:
Warning: replacing previous import 'ggplot2::alpha' by 'psych::alpha' when loading 'ShinyItemAnalysis'
Hence I wont be able to publish my package on CRAN (untill I solved this warning). Is there any easy way how to avoid this import conflict?
Note that when using roxygen2 package to build NAMESPACE from fellow .R scripts, it is very helpful to delete the NAMESPACE file and run roxygen2 again. Solves many problems.
I'm writing an R package that has several dependencies of other packages, some of them are available in CRAN and other ones are homemade.
According to the help, library("my_package") will load the namespace of the package once I have previously installed it, i.e, install.package("my_package").
Nevertheless, once I have installed the package I am able to use all the functions of the installed but not loaded package through my_package::my_function(), so if my package has dependencies, beside adding those into DESCRIPTION file:
Imports:
dplyr,
my_package2,
ggvis,
in the root of the package folder.
Do I have to load the dependencies of the new package through library() or the final user will see an error if he has not installed on his computer as the required packages are specified in the Imports section?
No, the user does not have to load the packages that are used by functions in my_package.
The fact that you have listed a package under Imports: in the DESCRIPTION file means that during the installation of my_package, R will check that this package is available on your system. This means then that functions in my_package can use functions from these packages using the :: notation, as you suggested.
Using the :: notation is the recommended way to refer to functions from other packages, but there are also other options:
In order to make all the functions from, say, dplyr accessable without :: in my_package, you could add import(dplyr) to the NAMESPACE file. This is convenient, if you use many functions from a package.
If you intend to use only, say, the function select from dplyr, you could add importFrom(select, dplyr) to the NAMESPACE file.
You could also add the package to the DESCRIPTION file under Depends:. This would mean that the package is loaded to the global environment when you use library(my_package). This is almost never a good solution.
The general idea of dependencies is R is that my_package will have "it's own version" of the packages it depends on loaded. Therefore, you can always be sure that you will, e.g., use the function select() from the dplyr package, as you intended to do. The exception is the use of Depends: which bypasses this system. In this case, my_package will look for functions in the global environment and if somebody should have defined some function called select() in the global environment, my_package will use this function and you will get unexpected results.
Example 1:
DESCRIPTION file:
Imports:
dpylr
some function from my_package:
my_fun <- function(...) {
dplyr::mutate(...) %>%
dplyr::select(1:3)
}
Example 2:
DESCRIPTION file:
Imports:
dpylr
NAMESPACE file:
import(dplyr)
some function from my_package:
my_fun <- function(...) {
mutate(...) %>%
select(1:3)
}
Example 3:
DESCRIPTION file:
Imports:
dpylr
NAMESPACE file:
importFrom(dplyr,select)
some function from my_package:
my_fun <- function(...) {
dpylr::mutate(...) %>%
select(1:3)
}
You find more detailed explanations of how to handle dependencies in R packages on the web. For instance the following are useful:
On the DESCRIPTION file
On the NAMESPACE file
Also, it is not necessary to write the NAMESPACE file by hand. You can let roxygen2 do that for you. Read the documentation for more information.
I am writing a small package to do some routine data analysis and I wish to include some easy mapping functions. My package imports tidyr, dplyr, and leaflet (from RStudio on Github). When I build or load my package I get the warning:
replacing previous import by ‘leaflet::%>%’ when loading ‘MyPackage’
Looking at the NAMESPACE files of leaflet, dplyr and tidyr, I find that all three have
importFrom(magrittr,"%>%")
and
export("%>%")
So basically all 3 are importing and exporting the same function from the magrittr package. Is there some way I can resolve this within may package so that the warning is no longer generated (not just suppressed)? Is it something I should just ignore?