Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create and document a package with R6 classes

Tags:

class

r

rstudio

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

like image 872
Charlotte Sirot Avatar asked Feb 23 '16 15:02

Charlotte Sirot


People also ask

How would you create a new R6 class?

Create a new R6 class object that on multipication of numbers. The class name and the result of class must be same always, as the R6Class() returns a R6 object that defines the class. We can then construct a new object from the class using the new() method which is accessed using the $ operator.

What is an R6 class?

R6 is an implemention of encapsulated object-oriented programming for R, and is a simpler, faster, lighter-weight alternative to R's built-in reference classes. This style of programming is also sometimes referred to as classical object-oriented programming. Some features of R6: R6 objects have reference semantics.

What does the public argument to the R6Class() function do?

The public argument is a list of items, which can be functions and fields (non-functions). Functions will be used as methods. The $new() method creates the object and calls the initialize() method, if it exists. Inside methods of the class, self refers to the object.

What is Self in R6?

From the "Introduction to R6 classes" vignette (emphasis mine): self. Inside methods of the class, self refers to the object. Public members of the object (all you've seen so far) are accessed with self$x. private.


1 Answers

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

like image 198
Charlotte Sirot Avatar answered Sep 28 '22 09:09

Charlotte Sirot