9

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

Charlotte Sirot
  • 931
  • 1
  • 8
  • 11
  • You've shared the error you are getting but not the actual code that generates it. Please provide a minimal [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Feb 23 '16 at 16:11
  • In fact, no so many code as I did all the procedure with RStudio (as mentioned in the steps I detailed in my question). I just have my R6class code (a very basic one): elem <- R6Class("elem_data", public = list( elem =NA ),#public private = list( aMethod = function() self$name )#private ) – Charlotte Sirot Feb 23 '16 at 16:35

2 Answers2

7

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

Charlotte Sirot
  • 931
  • 1
  • 8
  • 11
  • 2
    More generally, if you plan to use a function (or function-like object) from a different package, I believe that you need to either do what you noted here (`importFrom(package, function)` in `NAMESPACE`) or use the `::` operator whenever you use the function to directly reference the package it comes from (`package::function`). In this case, when defining the class, you would use `R6::R6Class`. – Paul de Barros Apr 21 '17 at 16:44
  • If you build documentation for your package with `roxygen2`, you actually shouldn't modify your NAMESPACE file. It should be added as roxygen tag @importFrom, as stated by Nikhil above. If you choose to reference package using `::` operator, as Paul said - remember to add the package you are referring to (in this case `R6`) to Imports in DESCRIPTION file – M_Kos Jan 02 '22 at 19:37
2

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
Nikhil Gupta
  • 1,436
  • 12
  • 15