epiverse-trace / safeframe

Tagging, validating, and safeguarding data to help harden data pipelines.
https://epiverse-trace.github.io/datatagr/
Other
1 stars 0 forks source link

Integers are numeric #44

Open TimTaylor opened 1 month ago

TimTaylor commented 1 month ago

Please place an "x" in all the boxes that apply


Currently this package does not treat integers as numeric vectors but they are. Do you mean 'double'?

EDIT: DELETED ERRONEOUS REPREX

TimTaylor commented 1 month ago

Inserted the Wrong reprex above. Corrected below:

library(datatagr)
#> 
#> Attaching package: 'datatagr'
#> The following object is masked from 'package:base':
#> 
#>     labels
y <- 1L
x <- data.frame(y)
x <- make_datatagr(x, y = "bob")
validate_datatagr(x, y = "numeric")
#> Error: Some labels have the wrong class:
#>   - y: Must inherit from class 'numeric', but has class 'integer'
is.numeric(y)
#> [1] TRUE

Created on 2024-10-08 with reprex v2.1.1

TimTaylor commented 1 month ago

To expand further to see the full class you can use .class2

.class2(1L)
#> [1] "integer" "numeric"

I believe useMethod considers the full class (although need to double check this).

EDIT: yes it does. From the docs:

If the object does not have a class attribute, it has an implicit class. Matrices and arrays have class "matrix" or "array" followed by the class of the underlying vector. Most vectors have class the result of mode(x), except that integer vectors have class c("integer", "numeric") and real vectors have class c("double", "numeric"). Function .class2(x) (since R 4.0.x) returns the full implicit (or explicit) class vector of x.

When a function calling UseMethod("fun") is applied to an object with class vector c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object. If no such function is found a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used, if it exists, or an error results.