Search code examples
rr6

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?


Solution

  • 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.