library vs :: How to load packages into a package

Would anyone be able to point me towards a good resource for understanding package calls from within a package?

Up to this point I've been working in R projects and calling the library() command to load other packages into my data analysis projects. I'm trying to switch to a package environment (mainly to access the testthat package) and I understand that from within a package it is preferred to use :: to call a package each time a function is used (e.g. dplyr::filter()). I find this makes code very difficult to read and to write. I'm unlikely to ever need to install and use my package as an actual package i.e. call it from another project using library(mypackage) so is there any reason I should not be using library() in my scripts?

Thanks for any help!
Dave

Not something I totally understand, but when making my first package I found that the package build check would throw warnings if I didn't use the :: approach, iirc. In theory you don't always need it: try reading the Namespaces page in the R Packages book, in particular this section on imports.

Summary: You shouldn't ever use library() in a package, but you may use @importFrom pkg fun and this should avoid the need for ::

PS If you're not really building a package, but more just a collection of scripts for your own use, then you can do what you want as along as you're willing to ignore error messages if you check or try to build the project. It's a nice challenge, imho, to actually turn it into a valid package though - I found it a frustrating but ultimately valuable learning journey. You might also be interested in this other topic I started.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.