Skip to content

setRefClass in package

4 messages · Jeremie Juste, Bert Gunter, Duncan Murdoch

#
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.

- 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)
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
#
I think this query fits better on r-package-devel rather than here.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Mar 24, 2021 at 6:07 AM Jeremie Juste <jeremiejuste at gmail.com>
wrote:

  
  
#
On 24/03/2021 9:06 a.m., Jeremie Juste wrote:
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
#
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: