Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Serialize vctrs objects to JSON 🥣

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
NotificationsYou must be signed in to change notification settings

r-lib/cereal

Repository files navigation

R-CMD-checkCRAN statusCodecov test coverageLifecycle: experimental

The goal of cereal is to provide methods toserialize objects fromvctrs toJSON, as well as back from JSON tovctrs objects.

Installation

You can install the released version of vetiver fromCRAN with:

install.packages("cereal")

You can install the development version of cereal fromGitHub with:

# install.packages("pak")pak::pak("r-lib/cereal")

Example

A data frame is a rectangular collection of variables (in the columns)and observations (in the rows). Each variable is a vector of one datatype, like factor or datetime:

df<-tibble::tibble(a= c(1.2,2.3,3.4),b=2L:4L,c= Sys.Date()+0:2,d= as.POSIXct("2019-01-01",tz="America/New_York")+100:102,e= sample(letters,3),f=factor(c("blue","blue","green"),levels= c("blue","green","red")),g= ordered(c("small","large","medium"),levels= c("small","medium","large")))df#> # A tibble: 3 × 7#>       a     b c          d                   e     f     g#>   <dbl> <int> <date>     <dttm>              <chr> <fct> <ord>#> 1   1.2     2 2023-06-09 2019-01-01 00:01:40 b     blue  small#> 2   2.3     3 2023-06-10 2019-01-01 00:01:41 p     blue  large#> 3   3.4     4 2023-06-11 2019-01-01 00:01:42 k     green medium

The vctrs package has aconcept of avectorprototype whichcaptures the metadata associated with a vector without keeping any ofthe data itself.

vctrs::vec_ptype(df)#> # A tibble: 0 × 7#> # ℹ 7 variables: a <dbl>, b <int>, c <date>, d <dttm>, e <chr>, f <fct>,#> #   g <ord>

The information stored in such a vector prototype includes, for example,the levels of a factor and the timezone for a datetime. This can beuseful or important information when deploying code or models, such aswhen usingvetiver. We could store thisvector prototype as an R binary object saved as an.rds file, but withcereal, you can store this vector prototype in plain text as JSON:

library(cereal)json<- cereal_to_json(df)json#> {#>   "a": {#>     "type": "numeric",#>     "example": "1.2",#>     "details": []#>   },#>   "b": {#>     "type": "integer",#>     "example": "2",#>     "details": []#>   },#>   "c": {#>     "type": "Date",#>     "example": "2023-06-09",#>     "details": []#>   },#>   "d": {#>     "type": "POSIXct",#>     "example": "2019-01-01 00:01:40",#>     "details": {#>       "tzone": "America/New_York"#>     }#>   },#>   "e": {#>     "type": "character",#>     "example": "b",#>     "details": []#>   },#>   "f": {#>     "type": "factor",#>     "example": "blue",#>     "details": {#>       "levels": ["blue", "green", "red"]#>     }#>   },#>   "g": {#>     "type": "ordered",#>     "example": "small",#>     "details": {#>       "levels": ["small", "medium", "large"]#>     }#>   }#> }

Storing prototype information as JSON (rather than a binary file) meansit can be used as plain-text metadata for a model.

You can also convert from JSON back to the original prototype:

cereal_from_json(json)#> # A tibble: 0 × 7#> # ℹ 7 variables: a <dbl>, b <int>, c <date>, d <dttm>, e <chr>, f <fct>,#> #   g <ord>

For an approach to this same task using Python,see Pydantic’smodel.json().

Contributing

  • This project is released with aContributor Code ofConduct.By contributing to this project, you agree to abide by its terms.

  • If you think you have encountered a bug, pleasesubmit anissue.

  • Either way, learn how to create and share areprex(a minimal, reproducible example), to clearly communicate about yourcode.

About

Serialize vctrs objects to JSON 🥣

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp