Create and save a tabular-data-resource

library(fr)

To illustrate, we will create a short tibble with a lot of different types of fields. Imagine that rating is a factor with three possible levels (good, better, best), but only two of them are present in the data:

d <-
  tibble::tibble(
    id = c("A01", "A02", "A03"),
    date = as.Date(c("2022-07-25", "2018-07-10", "2013-08-15")),
    measure = c(12.8, 13.9, 15.6),
    rating = factor(c("good", "best", "best"), levels = c("good", "better", "best")),
    ranking = c(14, 17, 19),
    impt = c(FALSE, TRUE, TRUE)
  )

Our example only has three rows, but in reality, any data frame imported, created, or curated using R can be used to create a tabular-data-resource.

We can see that we prepared a tibble with several different types of columns. Each column, or vector, has a native R class associated with it:

sapply(d, class)
#>          id        date     measure      rating     ranking        impt 
#> "character"      "Date"   "numeric"    "factor"   "numeric"   "logical"

d
#> # A tibble: 3 × 6
#>   id    date       measure rating ranking impt 
#>   <chr> <date>       <dbl> <fct>    <dbl> <lgl>
#> 1 A01   2022-07-25    12.8 good        14 FALSE
#> 2 A02   2018-07-10    13.9 best        17 TRUE 
#> 3 A03   2013-08-15    15.6 best        19 TRUE

Convert the data frame into a fr_tdr object by using as_fr_tdr() and specifying some table-specific metadata. as_fr_tdr() uses the class of each column in R to automatically create all of the frictionless field-specific metadata (name, type, constraints).

d_tdr <-
  d |>
  as_fr_tdr(
    name = "types_example",
    version = "0.1.0",
    title = "Example Data with Types",
    homepage = "https://geomarker.io",
    description = "This is used as an example dataset in the {fr} package vignette on `Creating a tabular-data-resource`."
  )

Reach in and update field-specific metadata:

d_tdr <-
  d_tdr |>
  update_field("id",
               title = "Identifier",
               description = "This is a unique identifier for each study participant.")

Write this to disk with write_fr_tdr:

write_fr_tdr(d_tdr, dir = tempdir())

fs::dir_tree(fs::path(tempdir(), "types_example"))
#> /var/folders/pg/q33bfwtj57d_v3vqpl7g26400000gn/T/RtmpCEdBJJ/types_example
#> ├── tabular-data-resource.yaml
#> └── types_example.csv