0

I am getting a circular dependency error for my R package when running CHECK:

checking package dependencies ... ERROR
  There is circular dependency in the installation order:
    One or more packages in

then a long list of packages.

However, my Depends category in DESCRIPTION is very minimal:

Depends: methods, R (>= 3.5.0), magrittr

And all other referenced packages are in either Imports or Suggests. One package I have in Imports also lists my package in their Imports, but I did not think that would lead to a dependency issue. I don't think any of the other packages I have in DESCRIPTION list mine in theirs.

I've searched quite a bit online but found no relevant solutions. Any ideas? Thanks in advance for the advice.

Session info:

R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.5 tools_4.0.5    tinytex_0.31   xfun_0.28     
ndimhypervol
  • 479
  • 1
  • 5
  • 17
  • 3
    Well, "One package I have in Imports also lists my package in their Imports" sure sounds like a circular dependency to me. Is your package on github or somewhere we can look at it and test it ourselves? Without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it's very difficult to help. – MrFlick Dec 09 '21 at 06:34
  • Thanks for the comment. My package is here: . Don't packages import each other all the time? I use explicit refs in my code (pkg::func), so I never fully load any referenced pkgs, specifically to avoid this. If there is some protocol to follow that I am missing here, please inform me! – ndimhypervol Dec 09 '21 at 06:42
  • 2
    Depends and imports both require a dependency. The main difference is that imports doesn't add items to the user's search path. See [here](https://stackoverflow.com/questions/8637993/better-explanation-of-when-to-use-imports-depends) or [here](https://kbroman.org/pkg_primer/pages/depends.html). You can't list a package under your "imports" that lists you under their "imports" -- that part is the same as "depends." Packages use functions from packages all the time, but the sharing is just one-way. If two packages can't exist without each other then they should be the same package. – MrFlick Dec 09 '21 at 06:55
  • Thanks again. I understand, but then how do I use a function from another package when that package uses a function from mine? The packages have different authors, so it is very unlikely they will be combined (and wouldn't make sense methodologically). Is my only option to integrate their function directly in my package and list an attribution, etc.? – ndimhypervol Dec 09 '21 at 07:09
  • 1
    Sure, one option is copying the function (with permission). When this happens with other packages often they will move shared logic to a separate package and then both packages can Import that helper package. If it's a function you don't *really* need you can move it to suggests and then check if that package is installed and throw an error if not. – MrFlick Dec 09 '21 at 07:14
  • Yes! Moving the package to Suggests and putting a require() in my code was the perfect solution. This worked so well because I had so few calls to the package's functionality, and is likely the best solution when there is a circular dependency that prevents one from listing a package in Imports. Thanks so much for the help. – ndimhypervol Dec 10 '21 at 02:41
  • If you'd like to post this as an answer, I will gladly accept. – ndimhypervol Dec 10 '21 at 02:42

1 Answers1

1

Depends and imports both require a dependency. The main difference is that imports doesn't add items to the user's search path. See here or here. You can't list a package under your "imports" that lists you under their "imports" -- that part is the same as "depends." Packages use functions from packages all the time, but the sharing is just one-way.

One possible work-around is copying the function (with permission from the other package author) into your own package.

Another option that I've seen used by other packages is to move shared logic to a separate helper package and then both packages can Import that helper package.

Finally, if it's a function you don't really need you can move it to suggests and then check if that package is installed only when you need the function and can throw an error if not.

MrFlick
  • 195,160
  • 17
  • 277
  • 295