You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "nealrichardson (via GitHub)" <gi...@apache.org> on 2023/04/13 17:50:18 UTC

[GitHub] [arrow] nealrichardson commented on issue #35071: [R] Improve interface for working with schemas

nealrichardson commented on issue #35071:
URL: https://github.com/apache/arrow/issues/35071#issuecomment-1507381913

   `[[<-.Schema` [is implemented](https://github.com/apache/arrow/blob/main/r/R/schema.R#L239-L275) and seems to work:
   
   ```
   > s <- schema(a=int64(), b=string())
   > s
   Schema
   a: int64
   b: string
   > s[["a"]] <- float64()
   > s
   Schema
   a: double
   b: string
   ```
   
   I think it would be ok if `schema(object_that_has_schema)` returned `object_that_has_schema$schema` as long as that's the only argument provided. I've run into this in the past myself and wished it just worked.
   
   Re: type operations, you could modify the `DataType$Equals()` method (which `==` calls) to handle comparing a type to a string, though that's a little slippery. Maybe what you want is something like `type_is(x, types)`, working like `base::inherits()`/`methods::is()`, defined for `x=DataType` and vectorized over the types if `x=Schema`. Something like:
   
   ```
   type_is <- function(x, types) {
     if (inherits(x, "Schema")) {
       map_lgl(x$fields, type_is, types)
     } else if (inherits(x, "Field")) {
       type_is(x$type, types)
     } else {
       # Some other things you could do here:
       # * allow `types` to be DataType or a list of
       # * ways to express groups of types like "integer" etc.
       x$ToString() %in% types
     }
   }
   ```
   
   ```
   > s <- schema(a = int64(), b = string(), c = timestamp())
   > s
   Schema
   a: int64
   b: string
   c: timestamp[s]
   > type_is(s, "int64")
   [1]  TRUE FALSE FALSE
   > s[type_is(s, "int64")]
   Schema
   a: int64
   ```
   
   ... and if you implement `[<-`, you could do:
   
   ```
   > s[type_is(s, "int64")] <- int32()
   Error in s[type_is(s, "int64")] <- int32() : 
     object of type 'environment' is not subsettable
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org