Skip to content

mdsumner/zr0

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zr

Thin R bindings to the zarrs Rust crate for reading and writing Zarr V2/V3 arrays. Built with extendr.

zr exposes zarrs primitives directly: stores, arrays, groups, metadata. Data comes back as native R vectors and arrays — no wrapper classes, no opinions.

Installation

# needs Rust toolchain (rustup.rs)
remotes::install_github("mdsumner/zr")

Usage

library(zr)

## report the zarrs version we are linked against
zr_zarrs_version()
#> [1] "zr 0.1.0 (zarrs 0.23)"

## create a store on disk
path <- tempfile(fileext = ".zarr")
store <- zr_store(path)

## create the root group
grp <- zr_group(store, "/", create = TRUE)

## create a float32 array
arr <- zr_create_array(
  store, "/temperature",
  shape = c(100L, 200L),
  chunks = c(10L, 20L),
  dtype = "float64",
  fill_value = NaN,
  dimension_names = c("y", "x"), attributes_json = ""
)

## inspect
zr_shape(arr)
#> [1] 100 200
zr_chunks(arr)
#> [1] 10 10
zr_dtype(arr)
#> [1] "float64 / <f8"
zr_ndim(arr)
#> [1] 2
zr_dimnames(arr)
#> [1] "y" "x"
zr_metadata(arr)
#> $zarr_format
#> [1] 3
#> 
#> $node_type
#> [1] "array"
#> 
#> $shape
#> [1] 100 200
#> 
#> $data_type
#> [1] "float64"
#> 
#> $chunk_grid
#> $chunk_grid$name
#> [1] "regular"
#> 
#> $chunk_grid$configuration
#> $chunk_grid$configuration$chunk_shape
#> [1] 10 20
#> 
#> 
#> 
#> $chunk_key_encoding
#> $chunk_key_encoding$name
#> [1] "default"
#> 
#> $chunk_key_encoding$configuration
#> $chunk_key_encoding$configuration$separator
#> [1] "/"
#> 
#> 
#> 
#> $fill_value
#> [1] "NaN"
#> 
#> $codecs
#>    name endian
#> 1 bytes little
#> 
#> $dimension_names
#> [1] "y" "x"

## write a 10x20 block into the first chunk
vals <- runif(200)
zr_write_chunk(arr, vals, chunk_index = c(0L, 0L))

## read it back
zr_read_chunk(arr, c(0L, 0L))
#>   [1] 0.931383203 0.414173105 0.032605747 0.917505681 0.687551026 0.583120650
#>   [7] 0.843320399 0.472138326 0.853036625 0.876569507 0.684179722 0.269251929
#>  [13] 0.852758782 0.819132691 0.599934984 0.903314901 0.855556080 0.817742091
#>  [19] 0.957865384 0.025129808 0.735112757 0.533055713 0.319268353 0.735165613
#>  [25] 0.723782655 0.366230463 0.734992260 0.212562216 0.440833070 0.952839793
#>  [31] 0.451251291 0.513774188 0.921307655 0.326137169 0.978236779 0.186116234
#>  [37] 0.172831731 0.332495203 0.558553798 0.715553815 0.139897630 0.710074113
#>  [43] 0.308260639 0.094215174 0.860932183 0.506422056 0.837997156 0.409358208
#>  [49] 0.056576072 0.396817891 0.187298655 0.176723782 0.738027273 0.660448201
#>  [55] 0.342020051 0.619234270 0.572164672 0.227262877 0.164566964 0.143439535
#>  [61] 0.766950500 0.612004424 0.188667882 0.650257589 0.380843102 0.904728262
#>  [67] 0.079218538 0.825400297 0.127518202 0.321053342 0.629652529 0.597757900
#>  [73] 0.688955295 0.473171645 0.166920047 0.769540472 0.845418869 0.792224063
#>  [79] 0.707851140 0.803606129 0.592504558 0.562598150 0.100732254 0.503312489
#>  [85] 0.527405855 0.990380022 0.005552610 0.320603400 0.933436735 0.898108663
#>  [91] 0.751849477 0.347627050 0.680749810 0.714428440 0.962651934 0.954155752
#>  [97] 0.615085683 0.350869833 0.827940395 0.142213660 0.748064600 0.672623267
#> [103] 0.204167986 0.490281187 0.657288305 0.235115537 0.369078583 0.169721150
#> [109] 0.882016752 0.973801602 0.571931449 0.765701167 0.457869669 0.260885617
#> [115] 0.948511356 0.373118838 0.547725532 0.059234825 0.490073458 0.256204017
#> [121] 0.982598753 0.623847696 0.187417249 0.543708068 0.141008024 0.297159133
#> [127] 0.623617232 0.311904406 0.760286704 0.955591757 0.678274720 0.292067096
#> [133] 0.723201856 0.373249152 0.507847740 0.581331135 0.372509863 0.237200405
#> [139] 0.853266119 0.347698691 0.498846343 0.587541175 0.871119851 0.445989388
#> [145] 0.397557280 0.370532216 0.816261978 0.891095654 0.066206542 0.893084538
#> [151] 0.319438304 0.747431557 0.072369184 0.666834856 0.780673333 0.503505463
#> [157] 0.083202310 0.434409922 0.030771351 0.842603241 0.605325761 0.240725004
#> [163] 0.284981307 0.903690048 0.720912215 0.150585151 0.805679113 0.530385109
#> [169] 0.754269863 0.481390545 0.272910197 0.421181077 0.684375359 0.129427999
#> [175] 0.484023807 0.032217239 0.934532587 0.517416320 0.273617767 0.125502565
#> [181] 0.764778241 0.126370466 0.638215421 0.667482315 0.948423343 0.211164251
#> [187] 0.717827740 0.578664012 0.250614643 0.005425361 0.420293190 0.265450441
#> [193] 0.749989375 0.797964657 0.155791944 0.733652364 0.644130563 0.977959551
#> [199] 0.083595048 0.122868196

## read a subset (0-based start, count)
zr_read(arr, start = c(0L, 0L), count = c(10L, 20L))
#>             [,1]       [,2]      [,3]      [,4]       [,5]      [,6]       [,7]
#>  [1,] 0.93138320 0.68417972 0.7351128 0.4512513 0.13989763 0.1872987 0.76695050
#>  [2,] 0.41417311 0.26925193 0.5330557 0.5137742 0.71007411 0.1767238 0.61200442
#>  [3,] 0.03260575 0.85275878 0.3192684 0.9213077 0.30826064 0.7380273 0.18866788
#>  [4,] 0.91750568 0.81913269 0.7351656 0.3261372 0.09421517 0.6604482 0.65025759
#>  [5,] 0.68755103 0.59993498 0.7237827 0.9782368 0.86093218 0.3420201 0.38084310
#>  [6,] 0.58312065 0.90331490 0.3662305 0.1861162 0.50642206 0.6192343 0.90472826
#>  [7,] 0.84332040 0.85555608 0.7349923 0.1728317 0.83799716 0.5721647 0.07921854
#>  [8,] 0.47213833 0.81774209 0.2125622 0.3324952 0.40935821 0.2272629 0.82540030
#>  [9,] 0.85303662 0.95786538 0.4408331 0.5585538 0.05657607 0.1645670 0.12751820
#> [10,] 0.87656951 0.02512981 0.9528398 0.7155538 0.39681789 0.1434395 0.32105334
#>            [,8]       [,9]     [,10]     [,11]      [,12]     [,13]     [,14]
#>  [1,] 0.6296525 0.59250456 0.7518495 0.7480646 0.57193145 0.9825988 0.6782747
#>  [2,] 0.5977579 0.56259815 0.3476271 0.6726233 0.76570117 0.6238477 0.2920671
#>  [3,] 0.6889553 0.10073225 0.6807498 0.2041680 0.45786967 0.1874172 0.7232019
#>  [4,] 0.4731716 0.50331249 0.7144284 0.4902812 0.26088562 0.5437081 0.3732492
#>  [5,] 0.1669200 0.52740585 0.9626519 0.6572883 0.94851136 0.1410080 0.5078477
#>  [6,] 0.7695405 0.99038002 0.9541558 0.2351155 0.37311884 0.2971591 0.5813311
#>  [7,] 0.8454189 0.00555261 0.6150857 0.3690786 0.54772553 0.6236172 0.3725099
#>  [8,] 0.7922241 0.32060340 0.3508698 0.1697212 0.05923482 0.3119044 0.2372004
#>  [9,] 0.7078511 0.93343674 0.8279404 0.8820168 0.49007346 0.7602867 0.8532661
#> [10,] 0.8036061 0.89810866 0.1422137 0.9738016 0.25620402 0.9555918 0.3476987
#>            [,15]      [,16]     [,17]      [,18]       [,19]      [,20]
#>  [1,] 0.49884634 0.31943830 0.6053258 0.27291020 0.764778241 0.42029319
#>  [2,] 0.58754118 0.74743156 0.2407250 0.42118108 0.126370466 0.26545044
#>  [3,] 0.87111985 0.07236918 0.2849813 0.68437536 0.638215421 0.74998937
#>  [4,] 0.44598939 0.66683486 0.9036900 0.12942800 0.667482315 0.79796466
#>  [5,] 0.39755728 0.78067333 0.7209122 0.48402381 0.948423343 0.15579194
#>  [6,] 0.37053222 0.50350546 0.1505852 0.03221724 0.211164251 0.73365236
#>  [7,] 0.81626198 0.08320231 0.8056791 0.93453259 0.717827740 0.64413056
#>  [8,] 0.89109565 0.43440992 0.5303851 0.51741632 0.578664012 0.97795955
#>  [9,] 0.06620654 0.03077135 0.7542699 0.27361777 0.250614643 0.08359505
#> [10,] 0.89308454 0.84260324 0.4813905 0.12550256 0.005425361 0.12286820

## read entire array (returns R array with dim)
x <- zr_read(arr)
dim(x)
#> [1] 100 200

Design

zr aims to expose zarrs as much as possible.

  • Store/Array/Group are external pointers — lightweight, pass them around
  • 0-based indexing matching zarrs convention (no off-by-one translation)
  • Data type on read is explicit: "float64", "float32", "int32" — you choose what R type to decode into
  • Metadata comes as JSON strings (parsed to list via jsonlite)
  • No R6/S4 classes — plain functions compose freely

Roadmap

  • HTTP/S3 stores via zarrs_http or zarrs_object_store
  • Sharding codec control
  • Upgrade to zarrs 0.23+ API (generic retrieve_array_subset, data_type::float32() factories)
  • Integration with ndr as a backend engine

About

What the Package Does (One Line, Title Case)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors