File: types.Rmd

package info (click to toggle)
r-cran-tibble 3.1.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,008 kB
  • sloc: ansic: 317; sh: 10; makefile: 5
file content (148 lines) | stat: -rw-r--r-- 3,355 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
title: "Column types"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Column types}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(dplyr)
library(tidyr)
library(purrr)
requireNamespace("hms", quietly = TRUE)
```

```{r setup}
library(tibble)
```

## Overview

This vignette shows an overview of known data types and their abbreviations, and their origin.
For example, `<int>` in the header of a column indicates an integer column, and `<chr>` denotes a character column.

```{r howto, echo = FALSE, eval = FALSE}
library(tidyverse)
library(tidymodels)
library(vctrs)
library(pillar)

all_methods <-
  c("vec_ptype_abbr", "vec_ptype_full", "type_sum") %>% 
  map(methods) %>% 
  map(as.character) %>% 
  map(~ gsub("^.*[.]", "", .x)) %>% 
  unlist() %>% 
  unique()

set_names(rep_along(all_methods, list("")), all_methods) %>% 
  dput()
```



```{r data, echo = FALSE}
data <- list(
  "Atomic" = rlang::quos(
    logical = TRUE,
    integer = 1L,
    double = 1.5,
    character = "A",
    complex = 1i,
    raw = as.raw(1),
    list = list(1),
    "named list" = list(a = 1)
  ),

  "Built-in objects" = rlang::quos(
    factor = factor("A"),
    ordered = ordered("a"),
    Date = Sys.Date(),
    POSIXt = Sys.time(),
    difftime = vctrs::new_duration(1)
  ),
  
  "Objects from other packages" = rlang::quos(
    hms = hms::hms(1),
    integer64 = bit64::as.integer64(1e10),
    blob = blob::blob(raw(1))
  ),
  
  "Data frames" = rlang::quos(
    data.frame = data.frame(a = 1),
    tbl_df = tibble(a = 1)
  ),
  
  "Unchanged" = rlang::quos(
    AsIs = I(1L)
  ),

  "vctrs types" = rlang::quos(
    unspecified = vctrs::unspecified(1),

    vctrs_list_of = vctrs::list_of(c(1L)),
    vctrs_vctr = vctrs::new_vctr(1L),

    vctrs_partial_factor = vctrs::partial_factor(letters),
    vctrs_partial_frame = vctrs::partial_frame(a = 1)
  ),

  "Language objects" = rlang::quos(
    "function" = function() NULL,
    symbol = quote(a),
    expression = parse(text = "a <- 1\nb<- 2"),
    quosures = rlang::quos(a = 1)
  )
)
```

```{r table, echo = FALSE}
tbl <- 
  data %>% 
  map(unclass) %>% 
  map(enframe, "Data type", "Expression") %>% 
  enframe("Class", "data") %>%
  unnest(data) %>% 
  mutate(Example = map_chr(Expression, rlang::as_label)) %>% 
  mutate(Value = map(Expression, rlang::eval_tidy)) %>% 
  select(-Expression) %>% 
  mutate(Class = if_else(Class == lag(Class, default = ""), "", Class)) %>%
  mutate("Column header" = map_chr(Value, type_sum))
```

```{r kable, echo = FALSE}
tbl %>% 
  select(-Value) %>% 
  mutate(Example = paste0("`", Example, "`")) %>% 
  knitr::kable(escape = FALSE)
```

## Example values

The following overview contains example values for each type:

```{r glimpse, echo = FALSE}
tbl %>%
  select(`Data type`, `Value`) %>%
  filter(map_lgl(Value, vctrs::vec_is)) %>%
  deframe() %>% 
  as_tibble() %>% 
  glimpse()
```


## Implementation

The column header is obtained by calling `pillar::type_sum()` on the column.
This is an S3 method that can be overridden,
but most of the time it is more useful to override `vctrs::vec_ptype_abbr()`:

```{r type_sum_default, results = if (Sys.getenv("IN_GALLEY") != "") "hide" else "markup"}
pillar:::type_sum.default
```