The metadata
argument in pins is flexible and can hold
any kind of metadata that you can formulate as a list()
. In
some situations, you may want to read and write with consistent
customized metadata; you can create functions to wrap
pin_write()
and pin_read()
for your particular
use case.
To see a different approach for when you want to write and read whole
file(s) in a customized way, see
vignette("managing-custom-formats")
.
We’ll begin by creating a temporary board for demonstration:
library(pins)
<- board_temp() board
Say you want to store a factor as JSON together with the
levels of the factor in the metadata. We can write a function
wrapping pin_write()
that creates the standardized metadata
we are interested in and writes it in a consistent way.
<- function(board,
pin_write_factor_json
x,
name, title = NULL,
description = NULL,
metadata = list(),
versioned = NULL,
tags = NULL,
...) {if (!is.factor(x)) rlang::abort("`x` is not a factor")
<- levels(x)
factor_levels <- modifyList(metadata, list(factor_levels = factor_levels))
metadata pin_write(
board = board,
x = x,
name = name,
type = "json",
title = title,
description = description,
metadata = metadata,
...
) }
We can use this new function to write a pin as JSON with our specific metadata:
<- factor(sample(letters, size = 10), levels = letters)
ten_letters %>% pin_write_factor_json(ten_letters, "letters-as-json")
board #> Creating new version '20230908T215344Z-688d7'
#> Writing to pin 'letters-as-json'
It’s possible to read this pin using the regular
pin_read()
function, but the object we get is no longer a
factor!
%>% pin_read("letters-as-json")
board #> [1] "e" "d" "x" "h" "l" "b" "i" "c" "v" "k"
Instead, we can also write a special function for reading, to reconstruct the factor including its levels:
<- function(board, name, version = NULL, hash = NULL, ...) {
pin_read_factor_json <- pin_read(board = board, name = name, version = version, hash = hash, ...)
ret <- pin_meta(board = board, name = name, version = version, ...)
meta factor(ret, levels = meta$user$factor_levels)
}
%>% pin_read_factor_json("letters-as-json")
board #> [1] e d x h l b i c v k
#> Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
How are these approaches used in real projects?