Skip to content

na.omit could not find function; S3 methods not loaded? #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dereckmezquita opened this issue Aug 9, 2024 · 4 comments
Closed

na.omit could not find function; S3 methods not loaded? #370

dereckmezquita opened this issue Aug 9, 2024 · 4 comments

Comments

@dereckmezquita
Copy link

Error description

I am using data.table and R6 classes. I want to use data.table na.omit function on the data but I get the above error.

I suspect that it's because na.omit is an S3 that has not been loaded. Unfortunately I cannot figure out how to solve this since na.omit is not exported from data.table.

I really don't want to have to call library("data.table") to solve this. Any help or is this indeed a bug?

Here's a minimal example.

R6 class module:

box::use(R6)

#' @export
TestClass <- R6$R6Class(
    "TestClass",
    public = list(
        some_method = function(data) {
            return(na.omit(data))
        }
    )
)

main.R

box::use(dt = data.table)
box::use(./dev/`na-omit`/`r6-class`[ TestClass ])

DT = dt$data.table(
    x=c(1,NaN,NA,3),
    y=c(NA_integer_, 1:3),
    z=c("a", NA_character_, "b", "c")
)

test <- TestClass$new()

test$some_method(DT)
r$> test$some_method(DT)
Error in na.omit(data) : could not find function "na.omit"

R version

r$> version
               _                           
platform       aarch64-apple-darwin23.4.0  
arch           aarch64                     
os             darwin23.4.0                
system         aarch64, darwin23.4.0       
status                                     
major          4                           
minor          4.1                         
year           2024                        
month          06                          
day            14                          
svn rev        86737                       
language       R                           
version.string R version 4.4.1 (2024-06-14)
nickname       Race for Your Life  
r$> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin23.4.0
Running under: macOS Sonoma 14.4.1

Matrix products: default
BLAS:   /opt/homebrew/Cellar/openblas/0.3.27/lib/libopenblasp-r0.3.27.dylib 
LAPACK: /opt/homebrew/Cellar/r/4.4.1/lib/R/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.4.1    R6_2.5.1          cli_3.6.3         tools_4.4.1      
 [5] yaml_2.3.10       box_1.2.0         data.table_1.15.4 jsonlite_1.8.8   
 [9] rlang_1.1.4       renv_1.0.7       

r$>


### ‘box’ version

1.2.0
@ArcadeAntics
Copy link

na.omit is part of package:stats, you'll have to add

box::use(
    stats[na.omit]
)

@dereckmezquita
Copy link
Author

dereckmezquita commented Aug 10, 2024

@ArcadeAntics yes indeed. However, data.table provides it's own version of na.omit which is an S3 method that applies to data.table objects. This version of na.omit is written in C and significantly faster.

https://rdatatable.gitlab.io/data.table/reference/na.omit.data.table.html

The issue is: I believe these are attached on package load event and thus not exported.

@klmr
Copy link
Owner

klmr commented Aug 12, 2024

‘data.table’ does not provide its own na.omit function. Instead, it registers an S3 method for na.omit for classes of type data.table.

You can use it by calling na.omit from ‘stats’ normally:

box::use(
  dt = data.table,
  stats[na.omit],
)

trace(data.table:::na.omit.data.table, quote(message('called na.omit.data.table')))

na.omit(dt$data.table(x = 1 : 5))

This will print “called na.omit.data.table”, showing that we called the expected S3 method.

(Please see #80 for possible limitations when working with ‘data.table’ inside modules, since ‘data.table’ hard-codes support for packages and certain functionalities it contains may break when used in other contexts due limitations in its data.table-awareness detection. However, in general it should work.)

@dereckmezquita
Copy link
Author

Okay thank you both then @klmr @ArcadeAntics!

I was wrong and had not understood how this works. Thank you for the trace example I'll be using that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants