[R] setRefClass in package

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Mar 24 16:34:35 CET 2021


On 24/03/2021 9:06 a.m., Jeremie Juste wrote:
> Hello,
> 
> I was wondering how to call a function outside a setRefClass but inside
> the package without export it. Let me explain by means of an example.

There are no scoping issues here:  Your initialize method can see all 
local functions in the package, including remove_if_all_NA.  But you've 
got a typo:  you called remove_if_all_na instead.  NA is not the same as na!

Duncan Murdoch

> 
> - in the file test-package/R/test.R
> 
> ##' some description
> ##'
> ##' some details
> ##' @title test
> ##' @return sideeffect
> ##' @author Jeremie Juste
> ##' @export test
> ##' @import data.table
> test <- setRefClass("test",
>              list(dt="data.table"))
> 
> 
> test$methods(
>    
>    initialize = function(x){
>      dt <<- remove_if_all_na(x[,abc:=1])
>      }
> )
> 
> 
> ##' remove rows for which all values are NA
> ##'
> ##' @title remove_if_all_NA
> ##' @param dt
> ##' @return dt
> ##' @author Jeremie Juste
> remove_if_all_NA <- function(dt) {
>    cn <- colnames(dt)
>    dt[!dt[NA],on=cn]
> }
> 
> 
> Here when I build and install the package test-package, if I don't export
> remove_if_all_NA
> 
> ##' remove rows for which all values are NA
> ##'
> ##' @title remove_if_all_NA
> ##' @param dt
> ##' @return dt
> ##' @author Jeremie Juste
> ##' @export
> remove_if_all_NA <- function(dt) {
>    cn <- colnames(dt)
>    dt[!dt[NA],on=cn]
> }
> 
> The package cannot use it.
> 
> library(test-package)
> library(data.table)
> 
>> aa <- data.table(a=1:10,b=letters[1:10])
>> b <- test(aa)
> Error in remove_if_all_na(x[, `:=`(abc, 1)]) :
>    could not find function "remove_if_all_na"
> 
> Do you have any recommendations? The official documentation for
> setRefClass is a bit thin for me but I wanted to use a tools that is going to stay. Any tip is
> welcome.
> 
> Best regards,
> Jeremie
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list