Charlotte Sirot
Charlotte Sirot

Reputation: 971

create and document a package with R6 classes

I am currently creating my first R package (:D) with R6 class thanks to RStudio, devtools and roxygen2. When my package contains only functions, I can build and load it without problem. But when I want to document R6 classes (with fields and methods like "Node" in this package https://cran.r-project.org/web/packages/data.tree/data.tree.pdf), RStudio don't want to build the package. I tried to find the trick on forums, but information about this issue is very scarce

My procedure:

  1. Open Rstudio, create a new project, I choose "R package"
  2. I fill out the name of the package and I am selecting the source files on which my packages will be based (one function and 3 classes).

-> At this point, the procedure is Ok as I obtain the correct structure with "man" with a description of my different classes, "R" with the different scripts of these classes.

But when I try to build and reload the package (having loaded beforehand the R6 packages) there is an error :

==> R CMD INSTALL --no-multiarch --with-keep.source esa

* installing to library ‘/home/cha/R/x86_64-pc-linux-gnu-library/3.0’
* installing *source* package ‘esa’ ...
** R
** preparing package for lazy loading
Error in eval(expr, envir, enclos) :
  impossible de trouver la fonction "R6Class" (translation: impossible to find the function "R6Class")
Error : unable to load R code in package ‘esa’
ERROR: lazy loading failed for package ‘esa’
* removing ‘/home/cha/R/x86_64-pc-linux-gnu-library/3.0/esa’
* restoring previous ‘/home/cha/R/x86_64-pc-linux-gnu-library/3.0/esa’  

Exited with status 1. 

I don't understand how to fix this error as Rstudio is right: R6Class is not a function !

My questions:

Is my procedure correct ? How can I fix this error ?

I need to know whether Rstudio is able to take in account R6 classes in R package building ? If not, I can do it manually, but I just need to know in order to stop waisting my time trying with RStudio :)

Thank you in advance for your help !!

Cha

Upvotes: 10

Views: 3765

Answers (2)

Nikhil Gupta
Nikhil Gupta

Reputation: 1486

Maybe you should include the following in your .R file where you are using your R6 class and have 'devtools' take care of creating the NAMESPACE file

#' @importFrom R6 R6Class   

> devtools::check() # Will create the NAMESPACE file for you

Upvotes: 2

Charlotte Sirot
Charlotte Sirot

Reputation: 971

Thank you very much McFlick !!

I just received an answer from the developers of R6Class package. They told me that I just need to add the line

importFrom(R6, R6Class)

in the NAMESPACE file. And it works correctly !!! (I would never find it alone:S)

Moreover, to add some items such as inheritance, fields and methods, they recommend to take as an example an .Rd from a package that have the intended structure.

Finally they recommend a book http://r-pkgs.had.co.nz/, I think I'll read it !!

Again thank you all

Upvotes: 7

Related Questions