File: compat-vctrs.R

package info (click to toggle)
r-cran-googledrive 2.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,584 kB
  • sloc: sh: 13; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 4,025 bytes parent folder | download
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
# based on https://github.com/tidymodels/workflowsets/blob/main/R/compat-vctrs.R

# ------------------------------------------------------------------------------

# `vec_restore()`
#
# Called at the end of `vec_slice()` and `vec_ptype()` after all slicing has
# been done on the proxy object.

#' @export
vec_restore.dribble <- function(x, to, ...) {
  dribble_maybe_reconstruct(x)
}

# ------------------------------------------------------------------------------

# `vec_ptype2()`
#
# When combining two dribbles together, `x` and `y` will be zero-row slices
# which should always result in a new dribble, as long as
# `df_ptype2()` can compute a common type.
#
# Combining a dribble with a tibble/data.frame will only ever happen if
# the user calls `vec_c()` or `vec_rbind()` with one of each of those inputs.
# Although I could probably make this work, it feels pretty weird and exotic
# and I think that user, if they even exist, should just turn that "other"
# tibble/data.frame into a dribble first.
# So I'll follow workflowsets and not attempt to return dribble for these
# combinations.

#' @export
vec_ptype2.dribble.dribble <- function(x, y, ..., x_arg = "", y_arg = "") {
  out <- df_ptype2(x, y, ..., x_arg = x_arg, y_arg = y_arg)
  dribble_maybe_reconstruct(out)
}
#' @export
vec_ptype2.dribble.tbl_df <- function(x, y, ..., x_arg = "", y_arg = "") {
  tib_ptype2(x, y, ..., x_arg = x_arg, y_arg = y_arg)
}
#' @export
vec_ptype2.tbl_df.dribble <- function(x, y, ..., x_arg = "", y_arg = "") {
  tib_ptype2(x, y, ..., x_arg = x_arg, y_arg = y_arg)
}
#' @export
vec_ptype2.dribble.data.frame <- function(x, y, ..., x_arg = "", y_arg = "") {
  tib_ptype2(x, y, ..., x_arg = x_arg, y_arg = y_arg)
}
#' @export
vec_ptype2.data.frame.dribble <- function(x, y, ..., x_arg = "", y_arg = "") {
  tib_ptype2(x, y, ..., x_arg = x_arg, y_arg = y_arg)
}

# ------------------------------------------------------------------------------

# `vec_cast()`
#
# These methods are designed with `vec_ptype2()` in mind.
#
# Casting from one dribble to another will happen "automatically" when
# two dribbles are combined with `vec_c()`. The common type will be
# computed with `vec_ptype2()`, then each input will be `vec_cast()` to that
# common type. It should always be possible to reconstruct the dribble
# if `df_cast()` is able to cast the underlying data frames successfully.
#
# Casting a tibble or data.frame to a dribble should never happen
# automatically, because the ptype2 methods always push towards
# tibble / data.frame. Since it is so unlikely that this will be done
# correctly, we don't ever allow it.
#
# Casting a dribble to a tibble or data.frame is easy, the underlying
# vctrs function does the work for us. This is used when doing
# `vec_c(<dribble>, <tbl>)`, as the `vec_ptype2()` method will compute
# a common type of tibble, and then each input will be cast to tibble.

#' @export
vec_cast.dribble.dribble <- function(x, to, ..., x_arg = "", to_arg = "") {
  out <- df_cast(x, to, ..., x_arg = x_arg, to_arg = to_arg)
  dribble_maybe_reconstruct(out)
}
#' @export
vec_cast.dribble.tbl_df <- function(x, to, ..., x_arg = "", to_arg = "") {
  stop_incompatible_cast_dribble(x, to, x_arg = x_arg, to_arg = to_arg)
}
#' @export
vec_cast.tbl_df.dribble <- function(x, to, ..., x_arg = "", to_arg = "") {
  tib_cast(x, to, ..., x_arg = x_arg, to_arg = to_arg)
}
#' @export
vec_cast.dribble.data.frame <- function(x, to, ..., x_arg = "", to_arg = "") {
  stop_incompatible_cast_dribble(x, to, x_arg = x_arg, to_arg = to_arg)
}
#' @export
vec_cast.data.frame.dribble <- function(x, to, ..., x_arg = "", to_arg = "") {
  df_cast(x, to, ..., x_arg = x_arg, to_arg = to_arg)
}

# ------------------------------------------------------------------------------

stop_incompatible_cast_dribble <- function(x, to, ..., x_arg, to_arg) {
  details <- "Can't cast to a <dribble> because the resulting structure is likely invalid."
  stop_incompatible_cast(x, to, x_arg = x_arg, to_arg = to_arg, details = details)
}