分数向量(来自 MASS)如何进行错误检查?

How do fractions vectors (from MASS) have error checking?

采用以下生成 fractions 向量的代码:

> example<-MASS:: as.fractions(c(0.2,0.4))
> example
[1] 1/5 2/5

如果我们用字符串替换这些分数之一,我们会收到以下错误:

> example[1]<-"0/1"
Error in floor(x) : non-numeric argument to mathematical function

这是怎么发生的?就我而言,fractions 向量不是 S4 对象,不应该进行任何类型的错误检查,所以当我尝试替换时 floor(x) 是 运行这样一个向量中的条目?

fractions 有一个 S3 [<- 方法:

library(MASS)
methods(class = "fractions")
## [1] [            [<-          as.character Math         Ops         
## [6] print        Summary      t           
## see '?methods' for accessing help and source code

并在 https://github.com/cran/MASS/blob/master/R/fractions.R

查看 R 源代码

floor.rat 中调用,在 fractions 中调用,在 [<-.fractions 中调用,在 [<- 泛型中调用。

> example[1] <- "0/1"
Error in floor(x) : non-numeric argument to mathematical function
> traceback()
5: matrix(floor(x))
4: .rat(x, cycles, max.denominator)
3: fractions(NextMethod())
2: `[<-.fractions`(`*tmp*`, 1, value = "0/1")
1: `[<-`(`*tmp*`, 1, value = "0/1")