File: in-packages.html

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 (781 lines) | stat: -rw-r--r-- 47,065 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />

<meta name="viewport" content="width=device-width, initial-scale=1" />



<title>In packages</title>

<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
  var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
  var i, h, a;
  for (i = 0; i < hs.length; i++) {
    h = hs[i];
    if (!/^h[1-6]$/i.test(h.tagName)) continue;  // it should be a header h1-h6
    a = h.attributes;
    while (a.length > 0) h.removeAttribute(a[0].name);
  }
});
</script>

<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>



<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } 
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } 
code span.at { color: #7d9029; } 
code span.bn { color: #40a070; } 
code span.bu { color: #008000; } 
code span.cf { color: #007020; font-weight: bold; } 
code span.ch { color: #4070a0; } 
code span.cn { color: #880000; } 
code span.co { color: #60a0b0; font-style: italic; } 
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } 
code span.do { color: #ba2121; font-style: italic; } 
code span.dt { color: #902000; } 
code span.dv { color: #40a070; } 
code span.er { color: #ff0000; font-weight: bold; } 
code span.ex { } 
code span.fl { color: #40a070; } 
code span.fu { color: #06287e; } 
code span.im { color: #008000; font-weight: bold; } 
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } 
code span.kw { color: #007020; font-weight: bold; } 
code span.op { color: #666666; } 
code span.ot { color: #007020; } 
code span.pp { color: #bc7a00; } 
code span.sc { color: #4070a0; } 
code span.ss { color: #bb6688; } 
code span.st { color: #4070a0; } 
code span.va { color: #19177c; } 
code span.vs { color: #4070a0; } 
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } 
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
  var sheets = document.styleSheets;
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
    try { var rules = sheets[i].cssRules; } catch (e) { continue; }
    var j = 0;
    while (j < rules.length) {
      var rule = rules[j];
      // check if there is a div.sourceCode rule
      if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
        j++;
        continue;
      }
      var style = rule.style.cssText;
      // check if color or background-color is set
      if (rule.style.color === '' && rule.style.backgroundColor === '') {
        j++;
        continue;
      }
      // replace div.sourceCode by a pre.sourceCode rule
      sheets[i].deleteRule(j);
      sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
    }
  }
})();
</script>




<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap; 
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }

code > span.kw { color: #555; font-weight: bold; } 
code > span.dt { color: #902000; } 
code > span.dv { color: #40a070; } 
code > span.bn { color: #d14; } 
code > span.fl { color: #d14; } 
code > span.ch { color: #d14; } 
code > span.st { color: #d14; } 
code > span.co { color: #888888; font-style: italic; } 
code > span.ot { color: #007020; } 
code > span.al { color: #ff0000; font-weight: bold; } 
code > span.fu { color: #900; font-weight: bold; } 
code > span.er { color: #a61717; background-color: #e3d2d2; } 
</style>




</head>

<body>




<h1 class="title toc-ignore">In packages</h1>



<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>This vignette serves two distinct, but related, purposes:</p>
<ul>
<li><p>It documents general best practices for using tidyr in a package,
inspired by <a href="https://ggplot2.tidyverse.org/dev/articles/ggplot2-in-packages.html">using
ggplot2 in packages</a>.</p></li>
<li><p>It describes migration patterns for the transition from tidyr
v0.8.3 to v1.0.0. This release includes breaking changes to
<code>nest()</code> and <code>unnest()</code> in order to increase
consistency within tidyr and with the rest of the tidyverse.</p></li>
</ul>
<p>Before we go on, we’ll attach the packages we use, expose the version
of tidyr, and make a small dataset to use in examples.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(dplyr, <span class="at">warn.conflicts =</span> <span class="cn">FALSE</span>)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">library</span>(purrr)</span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="fu">packageVersion</span>(<span class="st">&quot;tidyr&quot;</span>)</span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co">#&gt; [1] &#39;1.3.1&#39;</span></span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a>mini_iris <span class="ot">&lt;-</span> <span class="fu">as_tibble</span>(iris)[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">51</span>, <span class="dv">52</span>, <span class="dv">101</span>, <span class="dv">102</span>), ]</span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a>mini_iris</span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="co">#&gt; # A tibble: 6 × 5</span></span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="co">#&gt;   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   </span></span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="co">#&gt;          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;     </span></span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a><span class="co">#&gt; 1          5.1         3.5          1.4         0.2 setosa    </span></span>
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a><span class="co">#&gt; 2          4.9         3            1.4         0.2 setosa    </span></span>
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a><span class="co">#&gt; 3          7           3.2          4.7         1.4 versicolor</span></span>
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a><span class="co">#&gt; 4          6.4         3.2          4.5         1.5 versicolor</span></span>
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a><span class="co">#&gt; 5          6.3         3.3          6           2.5 virginica </span></span>
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a><span class="co">#&gt; 6          5.8         2.7          5.1         1.9 virginica</span></span></code></pre></div>
</div>
<div id="using-tidyr-in-packages" class="section level2">
<h2>Using tidyr in packages</h2>
<p>Here we assume that you’re already familiar with using tidyr in
functions, as described in <code>vignette(&quot;programming.Rmd&quot;)</code>.
There are two important considerations when using tidyr in a
package:</p>
<ul>
<li>How to avoid <code>R CMD CHECK</code> notes when using fixed
variable names.</li>
<li>How to alert yourself to upcoming changes in the development version
of tidyr.</li>
</ul>
<div id="fixed-column-names" class="section level3">
<h3>Fixed column names</h3>
<p>If you know the column names, this code works in the same way
regardless of whether its inside or outside of a package:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> <span class="fu">nest</span>(</span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a>  <span class="at">petal =</span> <span class="fu">c</span>(Petal.Length, Petal.Width), </span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a>  <span class="at">sepal =</span> <span class="fu">c</span>(Sepal.Length, Sepal.Width)</span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a>)</span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 3</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#&gt;   Species    petal            sepal           </span></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;      &lt;list&gt;           &lt;list&gt;          </span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#&gt; 1 setosa     &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#&gt; 2 versicolor &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#&gt; 3 virginica  &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span></code></pre></div>
<p>But <code>R CMD check</code> will warn about undefined global
variables (<code>Petal.Length</code>, <code>Petal.Width</code>,
<code>Sepal.Length</code>, and <code>Sepal.Width</code>), because it
doesn’t know that <code>nest()</code> is looking for the variables
inside of <code>mini_iris</code> (i.e. <code>Petal.Length</code> and
friends are data-variables, not env-variables).</p>
<p>The easiest way to silence this note is to use <code>all_of()</code>.
<code>all_of()</code> is a tidyselect helper (like
<code>starts_with()</code>, <code>ends_with()</code>, etc.) that takes
column names stored as strings:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> <span class="fu">nest</span>(</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>  <span class="at">petal =</span> <span class="fu">all_of</span>(<span class="fu">c</span>(<span class="st">&quot;Petal.Length&quot;</span>, <span class="st">&quot;Petal.Width&quot;</span>)), </span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a>  <span class="at">sepal =</span> <span class="fu">all_of</span>(<span class="fu">c</span>(<span class="st">&quot;Sepal.Length&quot;</span>, <span class="st">&quot;Sepal.Width&quot;</span>))</span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a>)</span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 3</span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#&gt;   Species    petal            sepal           </span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;      &lt;list&gt;           &lt;list&gt;          </span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#&gt; 1 setosa     &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#&gt; 2 versicolor &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#&gt; 3 virginica  &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span></code></pre></div>
<p>Alternatively, you may want to use <code>any_of()</code> if it is OK
that some of the specified variables cannot be found in the input
data.</p>
<p>The <a href="https://tidyselect.r-lib.org">tidyselect</a> package
offers an entire family of select helpers. You are probably already
familiar with them from using <code>dplyr::select()</code>.</p>
</div>
<div id="continuous-integration" class="section level3">
<h3>Continuous integration</h3>
<p>Hopefully you’ve already adopted continuous integration for your
package, in which <code>R CMD check</code> (which includes your own
tests) is run on a regular basis, e.g. every time you push changes to
your package’s source on GitHub or similar. The tidyverse team currently
relies most heavily on GitHub Actions, so that will be our example.
<code>usethis::use_github_action()</code> can help you get started.</p>
<p>We recommend adding a workflow that targets the devel version of
tidyr. When should you do this?</p>
<ul>
<li><p>Always? If your package is tightly coupled to tidyr, consider
leaving this in place all the time, so you know if changes in tidyr
affect your package.</p></li>
<li><p>Right before a tidyr release? For everyone else, you could add
(or re-activate an existing) tidyr-devel workflow during the period
preceding a major tidyr release that has the potential for breaking
changes, especially if you’ve been contacted during our reverse
dependency checks.</p></li>
</ul>
<p>Example of a GitHub Actions workflow that tests your package against
the development version of tidyr:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode yaml"><code class="sourceCode yaml"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">on</span><span class="kw">:</span></span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="at">  </span><span class="fu">push</span><span class="kw">:</span></span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="at">    </span><span class="fu">branches</span><span class="kw">:</span></span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="at">      </span><span class="kw">-</span><span class="at"> main</span></span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="at">  </span><span class="fu">pull_request</span><span class="kw">:</span></span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="at">    </span><span class="fu">branches</span><span class="kw">:</span></span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="at">      </span><span class="kw">-</span><span class="at"> main</span></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="fu">name</span><span class="kw">:</span><span class="at"> R-CMD-check-tidyr-devel</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="fu">jobs</span><span class="kw">:</span></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="at">  </span><span class="fu">R-CMD-check</span><span class="kw">:</span></span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="at">    </span><span class="fu">runs-on</span><span class="kw">:</span><span class="at"> macOS-latest</span></span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="at">    </span><span class="fu">steps</span><span class="kw">:</span></span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="at">      </span><span class="kw">-</span><span class="at"> </span><span class="fu">uses</span><span class="kw">:</span><span class="at"> actions/checkout@v2</span></span>
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="at">      </span><span class="kw">-</span><span class="at"> </span><span class="fu">uses</span><span class="kw">:</span><span class="at"> r-lib/actions/setup-r@v1</span></span>
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a><span class="at">      </span><span class="kw">-</span><span class="at"> </span><span class="fu">name</span><span class="kw">:</span><span class="at"> Install dependencies</span></span>
<span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a><span class="fu">        run</span><span class="kw">: </span><span class="ch">|</span></span>
<span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a>          install.packages(c(&quot;remotes&quot;, &quot;rcmdcheck&quot;))</span>
<span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a>          remotes::install_deps(dependencies = TRUE)</span>
<span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a>          remotes::install_github(&quot;tidyverse/tidyr&quot;)</span>
<span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a><span class="at">        </span><span class="fu">shell</span><span class="kw">:</span><span class="at"> Rscript {0}</span></span>
<span id="cb4-23"><a href="#cb4-23" tabindex="-1"></a><span class="at">      </span><span class="kw">-</span><span class="at"> </span><span class="fu">name</span><span class="kw">:</span><span class="at"> Check</span></span>
<span id="cb4-24"><a href="#cb4-24" tabindex="-1"></a><span class="at">        </span><span class="fu">run</span><span class="kw">:</span><span class="at"> rcmdcheck::rcmdcheck(args = &quot;--no-manual&quot;, error_on = &quot;error&quot;)</span></span>
<span id="cb4-25"><a href="#cb4-25" tabindex="-1"></a><span class="at">        </span><span class="fu">shell</span><span class="kw">:</span><span class="at"> Rscript {0}</span></span></code></pre></div>
<p>GitHub Actions are an evolving landscape, so you can always mine the
workflows for tidyr itself (<a href="https://github.com/tidyverse/tidyr/tree/main/.github/workflows">tidyverse/tidyr/.github/workflows</a>)
or the main <a href="https://github.com/r-lib/actions">r-lib/actions</a>
repo for ideas.</p>
</div>
</div>
<div id="tidyr-v0.8.3---v1.0.0" class="section level2">
<h2>tidyr v0.8.3 -&gt; v1.0.0</h2>
<p>v1.0.0 makes considerable changes to the interface of
<code>nest()</code> and <code>unnest()</code> in order to bring them in
line with newer tidyverse conventions. I have tried to make the
functions as backward compatible as possible and to give informative
warning messages, but I could not cover 100% of use cases, so you may
need to change your package code. This guide will help you do so with a
minimum of pain.</p>
<p>Ideally, you’ll tweak your package so that it works with both tidyr
0.8.3 and tidyr 1.0.0. This makes life considerably easier because it
means there’s no need to coordinate CRAN submissions - you can submit
your package that works with both tidyr versions, before I submit tidyr
to CRAN. This section describes our recommend practices for doing so,
drawing from the general principles described in <a href="https://design.tidyverse.org/changes-multivers.html" class="uri">https://design.tidyverse.org/changes-multivers.html</a>.</p>
<p>If you use continuous integration already, we
<strong>strongly</strong> recommend adding a build that tests with the
development version of tidyr; see above for details.</p>
<p>This section briefly describes how to run different code for
different versions of tidyr, then goes through the major changes that
might require workarounds:</p>
<ul>
<li><code>nest()</code> and <code>unnest()</code> get new
interfaces.</li>
<li><code>nest()</code> preserves groups.</li>
<li><code>nest_()</code> and <code>unnest_()</code> are defunct.</li>
</ul>
<p>If you’re struggling with a problem that’s not described here, please
reach out via <a href="https://github.com/tidyverse/tidyr/issues/new">github</a> or <a href="mailto:hadley@posit.co">email</a> so we can help out.</p>
<div id="conditional-code" class="section level3">
<h3>Conditional code</h3>
<p>Sometimes you’ll be able to write code that works with v0.8.3
<em>and</em> v1.0.0. But this often requires code that’s not
particularly natural for either version and you’d be better off to
(temporarily) have separate code paths, each containing non-contrived
code. You get to re-use your existing code in the “old” branch, which
will eventually be phased out, and write clean, forward-looking code in
the “new” branch.</p>
<p>The basic approach looks like this. First you define a function that
returns <code>TRUE</code> for new versions of tidyr:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>tidyr_new_interface <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a>  <span class="fu">packageVersion</span>(<span class="st">&quot;tidyr&quot;</span>) <span class="sc">&gt;</span> <span class="st">&quot;0.8.99&quot;</span></span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a>}</span></code></pre></div>
<p>We highly recommend keeping this as a function because it provides an
obvious place to jot any transition notes for your package, and it makes
it easier to remove transitional code later on. Another benefit is that
the tidyr version is determined at <em>run time</em>, not at <em>build
time</em>, and will therefore detect your user’s current tidyr
version.</p>
<p>Then in your functions, you use an <code>if</code> statement to call
different code for different versions:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>my_function_inside_a_package <span class="ot">&lt;-</span> <span class="cf">function</span>(...)</span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a>  <span class="co"># my code here</span></span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a>  <span class="cf">if</span> (<span class="fu">tidyr_new_interface</span>()) {</span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a>    <span class="co"># Freshly written code for v1.0.0</span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a>    out <span class="ot">&lt;-</span> tidyr<span class="sc">::</span><span class="fu">nest</span>(df, <span class="at">data =</span> <span class="fu">any_of</span>(<span class="fu">c</span>(<span class="st">&quot;x&quot;</span>, <span class="st">&quot;y&quot;</span>, <span class="st">&quot;z&quot;</span>)))</span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a>  } <span class="cf">else</span> {</span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a>    <span class="co"># Existing code for v0.8.3</span></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a>    out <span class="ot">&lt;-</span> tidyr<span class="sc">::</span><span class="fu">nest</span>(df, x, y, z)</span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a>  }</span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a></span>
<span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a>  <span class="co"># more code here</span></span>
<span id="cb6-13"><a href="#cb6-13" tabindex="-1"></a><span class="er">}</span></span></code></pre></div>
<p>If your new code uses a function that only exists in tidyr 1.0.0, you
will get a <code>NOTE</code> from <code>R CMD check</code>: this is one
of the few notes that you can explain in your CRAN submission comments.
Just mention that it’s for forward compatibility with tidyr 1.0.0, and
CRAN will let your package through.</p>
</div>
<div id="new-syntax-for-nest" class="section level3">
<h3>New syntax for <code>nest()</code></h3>
<p>What changed:</p>
<ul>
<li>The to-be-nested columns are no longer accepted as “loose
parts”.</li>
<li>The new list-column’s name is no longer provided via the
<code>.key</code> argument.</li>
<li>Now we use a construct like this:
<code>new_col = &lt;something about existing cols&gt;</code>.</li>
</ul>
<p>Why it changed:</p>
<ul>
<li><p>The use of <code>...</code> for metadata is a problematic pattern
we’re moving away from. <a href="https://design.tidyverse.org/dots-data.html" class="uri">https://design.tidyverse.org/dots-data.html</a></p></li>
<li><p>The <code>new_col = &lt;something about existing cols&gt;</code>
construct lets us create multiple nested list-columns at once
(“multi-nest”).</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a>  <span class="fu">nest</span>(<span class="at">petal =</span> <span class="fu">matches</span>(<span class="st">&quot;Petal&quot;</span>), <span class="at">sepal =</span> <span class="fu">matches</span>(<span class="st">&quot;Sepal&quot;</span>)) </span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 3</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#&gt;   Species    petal            sepal           </span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;      &lt;list&gt;           &lt;list&gt;          </span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#&gt; 1 setosa     &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#&gt; 2 versicolor &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#&gt; 3 virginica  &lt;tibble [2 × 2]&gt; &lt;tibble [2 × 2]&gt;</span></span></code></pre></div></li>
</ul>
<p>Before and after examples:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="co"># v0.8.3</span></span>
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a>  <span class="fu">nest</span>(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, <span class="at">.key =</span> <span class="st">&quot;my_data&quot;</span>)</span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a></span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co"># v1.0.0</span></span>
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a>  <span class="fu">nest</span>(<span class="at">my_data =</span> <span class="fu">c</span>(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))</span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a></span>
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co"># v1.0.0 avoiding R CMD check </span><span class="al">NOTE</span></span>
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a>  <span class="fu">nest</span>(<span class="at">my_data =</span> <span class="fu">any_of</span>(<span class="fu">c</span>(<span class="st">&quot;Sepal.Length&quot;</span>, <span class="st">&quot;Sepal.Width&quot;</span>, <span class="st">&quot;Petal.Length&quot;</span>, <span class="st">&quot;Petal.Width&quot;</span>)))</span>
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a></span>
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="co"># or equivalently:</span></span>
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a>  <span class="fu">nest</span>(<span class="at">my_data =</span> <span class="sc">!</span><span class="fu">any_of</span>(<span class="st">&quot;Species&quot;</span>))</span></code></pre></div>
<p>If you need a quick and dirty fix without having to think, just call
<code>nest_legacy()</code> instead of <code>nest()</code>. It’s the same
as <code>nest()</code> in v0.8.3:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">tidyr_new_interface</span>()) {</span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>  out <span class="ot">&lt;-</span> tidyr<span class="sc">::</span><span class="fu">nest_legacy</span>(df, x, y, z)</span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a>  out <span class="ot">&lt;-</span> tidyr<span class="sc">::</span><span class="fu">nest</span>(df, x, y, z)</span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div id="new-syntax-for-unnest" class="section level3">
<h3>New syntax for <code>unnest()</code></h3>
<p>What changed:</p>
<ul>
<li><p>The to-be-unnested columns must now be specified explicitly,
instead of defaulting to all list-columns. This also deprecates
<code>.drop</code> and <code>.preserve</code>.</p></li>
<li><p><code>.sep</code> has been deprecated and replaced with
<code>names_sep</code>.</p></li>
<li><p><code>unnest()</code> uses the <a href="https://www.tidyverse.org/blog/2019/01/tibble-2.0.1/#name-repair">emerging
tidyverse standard</a> to disambiguate duplicated names. Use
<code>names_repair = tidyr_legacy</code> to request the previous
approach.</p></li>
<li><p><code>.id</code> has been deprecated because it can be easily
replaced by creating the column of names prior to <code>unnest()</code>,
e.g. with an upstream call to <code>mutate()</code>.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="co"># v0.8.3</span></span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">unnest</span>(x, <span class="at">.id =</span> <span class="st">&quot;id&quot;</span>)</span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a></span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co"># v1.0.0</span></span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a>df <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="at">id =</span> <span class="fu">names</span>(x)) <span class="sc">%&gt;%</span> <span class="fu">unnest</span>(x)<span class="er">)</span></span></code></pre></div></li>
</ul>
<p>Why it changed:</p>
<ul>
<li><p>The use of <code>...</code> for metadata is a problematic pattern
we’re moving away from. <a href="https://design.tidyverse.org/dots-data.html" class="uri">https://design.tidyverse.org/dots-data.html</a></p></li>
<li><p>The changes to details arguments relate to features rolling out
across multiple packages in the tidyverse. For example,
<code>ptype</code> exposes prototype support from the new <a href="https://vctrs.r-lib.org">vctrs package</a>.
<code>names_repair</code> specifies what to do about duplicated or
non-syntactic names, consistent with tibble and readxl.</p></li>
</ul>
<p>Before and after:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>nested <span class="ot">&lt;-</span> mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a>  <span class="fu">nest</span>(<span class="at">my_data =</span> <span class="fu">c</span>(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))</span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a></span>
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co"># v0.8.3 automatically unnests list-cols</span></span>
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a>nested <span class="sc">%&gt;%</span> <span class="fu">unnest</span>()</span>
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a></span>
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co"># v1.0.0 must be told which columns to unnest</span></span>
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a>nested <span class="sc">%&gt;%</span> <span class="fu">unnest</span>(<span class="fu">any_of</span>(<span class="st">&quot;my_data&quot;</span>))</span></code></pre></div>
<p>If you need a quick and dirty fix without having to think, just call
<code>unnest_legacy()</code> instead of <code>unnest()</code>. It’s the
same as <code>unnest()</code> in v0.8.3:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">tidyr_new_interface</span>()) {</span>
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>  out <span class="ot">&lt;-</span> tidyr<span class="sc">::</span><span class="fu">unnest_legacy</span>(df)</span>
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a>  out <span class="ot">&lt;-</span> tidyr<span class="sc">::</span><span class="fu">unnest</span>(df)</span>
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a>}</span></code></pre></div>
</div>
<div id="nest-preserves-groups" class="section level3">
<h3><code>nest()</code> preserves groups</h3>
<p>What changed:</p>
<ul>
<li><code>nest()</code> now preserves the groups present in the
input.</li>
</ul>
<p>Why it changed:</p>
<ul>
<li>To reflect the growing support for grouped data frames, especially
in recent releases of dplyr. See, for example,
<code>dplyr::group_modify()</code>, <code>group_map()</code>, and
friends.</li>
</ul>
<p>If the fact that <code>nest()</code> now preserves groups is
problematic downstream, you have a few choices:</p>
<ul>
<li><p>Apply <code>ungroup()</code> to the result. This level of
pragmatism suggests, however, you should at least consider the next two
options.</p></li>
<li><p>You should never have grouped in the first place. Eliminate the
<code>group_by()</code> call and specify which columns should be nested
versus not nested directly in <code>nest()</code>.</p></li>
<li><p>Adjust the downstream code to accommodate grouping.</p></li>
</ul>
<p>Imagine we used <code>group_by()</code> then <code>nest()</code> on
<code>mini_iris</code>, then we computed on the list-column <em>outside
the data frame</em>.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>(df <span class="ot">&lt;-</span> mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a>   <span class="fu">group_by</span>(Species) <span class="sc">%&gt;%</span> </span>
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a>   <span class="fu">nest</span>())</span>
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 2</span></span>
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="co">#&gt; # Groups:   Species [3]</span></span>
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#&gt;   Species    data            </span></span>
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;      &lt;list&gt;          </span></span>
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a><span class="co">#&gt; 1 setosa     &lt;tibble [2 × 4]&gt;</span></span>
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a><span class="co">#&gt; 2 versicolor &lt;tibble [2 × 4]&gt;</span></span>
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="co">#&gt; 3 virginica  &lt;tibble [2 × 4]&gt;</span></span>
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a>(external_variable <span class="ot">&lt;-</span> <span class="fu">map_int</span>(df<span class="sc">$</span>data, nrow))</span>
<span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="co">#&gt; [1] 2 2 2</span></span></code></pre></div>
<p>And now we try to add that back to the data <em>post hoc</em>:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>df <span class="sc">%&gt;%</span> </span>
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">n_rows =</span> external_variable)</span>
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#&gt; Error in `mutate()`:</span></span>
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#&gt; ℹ In argument: `n_rows = external_variable`.</span></span>
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#&gt; ℹ In group 1: `Species = setosa`.</span></span>
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#&gt; Caused by error:</span></span>
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#&gt; ! `n_rows` must be size 1, not 3.</span></span></code></pre></div>
<p>This fails because <code>df</code> is grouped and
<code>mutate()</code> is group-aware, so it’s hard to add a completely
external variable. Other than pragmatically <code>ungroup()</code>ing,
what can we do? One option is to work inside the data frame, i.e. bring
the <code>map()</code> inside the <code>mutate()</code>, and design the
problem away:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>df <span class="sc">%&gt;%</span> </span>
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">n_rows =</span> <span class="fu">map_int</span>(data, nrow))</span>
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 3</span></span>
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#&gt; # Groups:   Species [3]</span></span>
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="co">#&gt;   Species    data             n_rows</span></span>
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;      &lt;list&gt;            &lt;int&gt;</span></span>
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#&gt; 1 setosa     &lt;tibble [2 × 4]&gt;      2</span></span>
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a><span class="co">#&gt; 2 versicolor &lt;tibble [2 × 4]&gt;      2</span></span>
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a><span class="co">#&gt; 3 virginica  &lt;tibble [2 × 4]&gt;      2</span></span></code></pre></div>
<p>If, somehow, the grouping seems appropriate AND working inside the
data frame is not an option, <code>tibble::add_column()</code> is
group-unaware. It lets you add external data to a grouped data
frame.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>df <span class="sc">%&gt;%</span> </span>
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a>  tibble<span class="sc">::</span><span class="fu">add_column</span>(<span class="at">n_rows =</span> external_variable)</span>
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="co">#&gt; # A tibble: 3 × 3</span></span>
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a><span class="co">#&gt; # Groups:   Species [3]</span></span>
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co">#&gt;   Species    data             n_rows</span></span>
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co">#&gt;   &lt;fct&gt;      &lt;list&gt;            &lt;int&gt;</span></span>
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="co">#&gt; 1 setosa     &lt;tibble [2 × 4]&gt;      2</span></span>
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="co">#&gt; 2 versicolor &lt;tibble [2 × 4]&gt;      2</span></span>
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a><span class="co">#&gt; 3 virginica  &lt;tibble [2 × 4]&gt;      2</span></span></code></pre></div>
</div>
<div id="nest_-and-unnest_-are-defunct" class="section level3">
<h3><code>nest_()</code> and <code>unnest_()</code> are defunct</h3>
<p>What changed:</p>
<ul>
<li><code>nest_()</code> and <code>unnest_()</code> no longer work</li>
</ul>
<p>Why it changed:</p>
<ul>
<li>We are transitioning the whole tidyverse to the powerful tidy eval
framework. Therefore, we are gradually removing all previous solutions:
<ul>
<li>Specialized standard evaluation versions of functions, e.g.,
<code>foo_()</code> as a complement to <code>foo()</code>.</li>
<li>The older lazyeval framework.</li>
</ul></li>
</ul>
<p>Before and after:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="co"># v0.8.3</span></span>
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a>  <span class="fu">nest_</span>(</span>
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a>    <span class="at">key_col =</span> <span class="st">&quot;my_data&quot;</span>,</span>
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a>    <span class="at">nest_cols =</span> <span class="fu">c</span>(<span class="st">&quot;Sepal.Length&quot;</span>, <span class="st">&quot;Sepal.Width&quot;</span>, <span class="st">&quot;Petal.Length&quot;</span>, <span class="st">&quot;Petal.Width&quot;</span>)</span>
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a>  )</span>
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a></span>
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a>nested <span class="sc">%&gt;%</span> <span class="fu">unnest_</span>(<span class="sc">~</span> my_data)</span>
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a></span>
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a><span class="co"># v1.0.0</span></span>
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a>mini_iris <span class="sc">%&gt;%</span> </span>
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a>  <span class="fu">nest</span>(<span class="at">my_data =</span> <span class="fu">any_of</span>(<span class="fu">c</span>(<span class="st">&quot;Sepal.Length&quot;</span>, <span class="st">&quot;Sepal.Width&quot;</span>, <span class="st">&quot;Petal.Length&quot;</span>, <span class="st">&quot;Petal.Width&quot;</span>)))</span>
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a></span>
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a>nested <span class="sc">%&gt;%</span> <span class="fu">unnest</span>(<span class="fu">any_of</span>(<span class="st">&quot;my_data&quot;</span>))</span></code></pre></div>
</div>
</div>



<!-- code folding -->


<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>