wdkrnls
wdkrnls

Reputation: 4702

How to import an R6 class into a package?

I would like to use functions in my personal package built on top of an R6 class called ms_team defined inside of the Microsoft365R package. Right now these functions all fail because even though I import the functions I need, when I try to call one of my functions which rely on them, I get an error that this object is not found.

Error in login$get_team(team_id) : object 'ms_team' not found

I have tried to include @importFrom Microsoft365R ms_team ... in my function, but with no success. What are the magic words to use functions which rely on externally defined R6 classes in a package?

Upvotes: 3

Views: 600

Answers (2)

jpiversen
jpiversen

Reputation: 3222

As of version 2.3.1 (September 2021) it is possible to use Microsoft365R without the package being on the search list.

This means that you don't have to import the whole package anymore. You can now add Microsoft365R to Imports in your DESCRIPTION file, and call functions/objects like Microsoft365R::function() / Microsoft365R::object.

Upvotes: 0

Hong Ooi
Hong Ooi

Reputation: 57697

Microsoft365R dev here. I assume you're using roxygen2.

To import an exported object (any object, not just an R6 class) from another package, put

#' @importFrom pkgname objname
NULL

in one of your package's R files. In this case, you would do

#' @importFrom Microsoft365R ms_team
NULL

It may be easier, and more robust, to import the entire package though:

#' @import Microsoft365R
NULL

See here for how I import the AzureGraph package into Microsoft365R itself, for example.

Upvotes: 2

Related Questions