[R] setRefClass in package

Jeremie Juste jerem|eju@te @end|ng |rom gm@||@com
Wed Mar 24 18:12:13 CET 2021


Hello,


Many thanks for the reply.
Indeed there was no scoping issue here. I was sloppy.

Best regards,
Jeremie


On Wednesday, 24 Mar 2021 at 11:34, Duncan Murdoch wrote:
> 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.
>> 
>

-- 
Jeremie Juste



More information about the R-help mailing list