setRefClass in package

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

setRefClass in package

Jeremie Juste
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)

> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: setRefClass in package

Bert Gunter-2
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 <[hidden email]>
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.
>
> - 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
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: setRefClass in package

Duncan Murdoch-2
In reply to this post by Jeremie Juste
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: setRefClass in package

Jeremie Juste
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
>> ______________________________________________
>> [hidden email] 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

______________________________________________
[hidden email] 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.