File: in-packages.R

package info (click to toggle)
r-cran-tidyr 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,720 kB
  • sloc: cpp: 268; sh: 9; makefile: 2
file content (134 lines) | stat: -rw-r--r-- 4,029 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
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
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(tidyr)
library(dplyr, warn.conflicts = FALSE)
library(purrr)

packageVersion("tidyr")

mini_iris <- as_tibble(iris)[c(1, 2, 51, 52, 101, 102), ]
mini_iris

## -----------------------------------------------------------------------------
mini_iris %>% nest(
  petal = c(Petal.Length, Petal.Width), 
  sepal = c(Sepal.Length, Sepal.Width)
)

## -----------------------------------------------------------------------------
mini_iris %>% nest(
  petal = all_of(c("Petal.Length", "Petal.Width")), 
  sepal = all_of(c("Sepal.Length", "Sepal.Width"))
)

## -----------------------------------------------------------------------------
tidyr_new_interface <- function() {
  packageVersion("tidyr") > "0.8.99"
}

## ----eval = FALSE-------------------------------------------------------------
#  my_function_inside_a_package <- function(...)
#    # my code here
#  
#    if (tidyr_new_interface()) {
#      # Freshly written code for v1.0.0
#      out <- tidyr::nest(df, data = any_of(c("x", "y", "z")))
#    } else {
#      # Existing code for v0.8.3
#      out <- tidyr::nest(df, x, y, z)
#    }
#  
#    # more code here
#  }

## -----------------------------------------------------------------------------
mini_iris %>% 
  nest(petal = matches("Petal"), sepal = matches("Sepal")) 

## ----eval = FALSE-------------------------------------------------------------
#  # v0.8.3
#  mini_iris %>%
#    nest(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, .key = "my_data")
#  
#  # v1.0.0
#  mini_iris %>%
#    nest(my_data = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))
#  
#  # v1.0.0 avoiding R CMD check NOTE
#  mini_iris %>%
#    nest(my_data = any_of(c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")))
#  
#  # or equivalently:
#  mini_iris %>%
#    nest(my_data = !any_of("Species"))

## ----eval = FALSE-------------------------------------------------------------
#  if (tidyr_new_interface()) {
#    out <- tidyr::nest_legacy(df, x, y, z)
#  } else {
#    out <- tidyr::nest(df, x, y, z)
#  }

## ----eval = FALSE-------------------------------------------------------------
#  # v0.8.3
#  df %>% unnest(x, .id = "id")
#  
#  # v1.0.0
#  df %>% mutate(id = names(x)) %>% unnest(x))

## ----eval = FALSE-------------------------------------------------------------
#  nested <- mini_iris %>%
#    nest(my_data = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))
#  
#  # v0.8.3 automatically unnests list-cols
#  nested %>% unnest()
#  
#  # v1.0.0 must be told which columns to unnest
#  nested %>% unnest(any_of("my_data"))

## ----eval = FALSE-------------------------------------------------------------
#  if (tidyr_new_interface()) {
#    out <- tidyr::unnest_legacy(df)
#  } else {
#    out <- tidyr::unnest(df)
#  }

## -----------------------------------------------------------------------------
(df <- mini_iris %>% 
   group_by(Species) %>% 
   nest())
(external_variable <- map_int(df$data, nrow))

## ----error = TRUE-------------------------------------------------------------
df %>% 
  mutate(n_rows = external_variable)

## -----------------------------------------------------------------------------
df %>% 
  mutate(n_rows = map_int(data, nrow))

## -----------------------------------------------------------------------------
df %>% 
  tibble::add_column(n_rows = external_variable)

## ----eval = FALSE-------------------------------------------------------------
#  # v0.8.3
#  mini_iris %>%
#    nest_(
#      key_col = "my_data",
#      nest_cols = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
#    )
#  
#  nested %>% unnest_(~ my_data)
#  
#  # v1.0.0
#  mini_iris %>%
#    nest(my_data = any_of(c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")))
#  
#  nested %>% unnest(any_of("my_data"))