0

I created a new package in R and the functions in my package require pre-installed packages like igraph, dplyr to run properly. In the DESCRIPTION file, I did add these packages in the Imports field. But when I run my package, I am getting an error which indicates the packages required have not been installed.

To check what the problem is, I installed the pre-required packages separately and ran my package, and it seems to be working fine then.

This is how my DESCRIPTION file looks like

Package: xxx
Type: Package
Title: xxx
Version: 0.1.0
Author: xxx
Maintainer: xxx
Description: xxx 
License: GPL-2
Encoding: UTF-8
LazyData: FALSE
Imports: 
    igraph,dplyr,network,gridExtra,centiserve
RoxygenNote: 6.1.1

Is there anyway I could load just my package which then loads the pre-required packages automatically, without having to load pre-required packages like igraph and dplyr manually.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • "But when I run my package" -- Do you mean when you try to install your package, or when you try to load your (installed) package? – duckmayr Aug 03 '19 at 14:53
  • I mean when I load my (installed) package and run the function in it – Chirag Srinivas Aug 03 '19 at 15:04
  • https://stackoverflow.com/questions/5805049/package-dependencies-when-installing-from-source-in-r ... in case you are asking how to install dependencies when installing a local package from source – user20650 Aug 03 '19 at 15:23

1 Answers1

1

This question raises the same issue as this Stack Overflow question. I offer this answer to provide a little more detail and official sources than what is at the other question.

The issue is that you've listed the packages in Imports rather than Depends. Normally this is preferable, but if you do it this way, you have to take care to import the functions you need in the NAMESPACE file. From Writing R Extensions, Section 1.1.3:

The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached.

Therefore, packages in Imports aren't loaded when your package is loaded, as would be the case if they are specified in Depends.

My advice would be to keep these packages in Imports, then specify the necessary imported functions in NAMESPACE, either manually adding the statements in that file using syntax like

importFrom(foo, f, g)

(See Writing R Extensions, Section 1.5.1), or via roxygen2 tags in your R scripts like

#' @importFrom foo f
duckmayr
  • 16,303
  • 3
  • 35
  • 53