quanteda / readtext

an R package for reading text files
https://readtext.quanteda.io
120 stars 28 forks source link

Add better error messages for indexing errors for dfm #121

Closed kbenoit closed 6 years ago

kbenoit commented 6 years ago

We could make these messages friendlier. In solving #1266, I have dropped error checking for selection indexing and prefer to reply on errors returned by sending an invalid index argument to [.dfm methods.

> d <- dfm(c("a a b c", "b b a c c"))
> d
Document-feature matrix of: 2 documents, 3 features (0% sparse).
2 x 3 sparse Matrix of class "dfm"
       features
docs    a b c
  text1 2 1 1
  text2 1 2 2
> d[3, ]
Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) : 
  index larger than maximal 2
> d["text3", ]
Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) : 
  invalid character indexing
> d[c(TRUE, TRUE, TRUE), ]
Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) : 
  logical subscript too long (3, should be 2)
> d[, c(TRUE, TRUE, TRUE, FALSE)]
Error in intI(j, n = x@Dim[2], dn[[2]], give.dn = FALSE) : 
  logical subscript too long (4, should be 3)
> d[, c("a", "d")]
Error in intI(j, n = x@Dim[2], dn[[2]], give.dn = FALSE) : 
  invalid character indexing