EVOLUTION-MANAGER
Edit File: in-packages.html
<!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> <script>// Hide empty <a> tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) --> // v0.0.1 // Written by JooYoung Seo (jooyoung@psu.edu) and Atsushi Yasumoto on June 1st, 2020. document.addEventListener('DOMContentLoaded', function() { const codeList = document.getElementsByClassName("sourceCode"); for (var i = 0; i < codeList.length; i++) { var linkList = codeList[i].getElementsByTagName('a'); for (var j = 0; j < linkList.length; j++) { if (linkList[j].innerHTML === "") { linkList[j].setAttribute('aria-hidden', 'true'); } } } }); </script> <style type="text/css">code{white-space: pre;}</style> <style type="text/css" data-origin="pandoc"> pre > code.sourceCode { white-space: pre; position: relative; } pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } pre > code.sourceCode > span:empty { height: 1.2em; } 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; } /* Alert */ code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */ code span.at { color: #7d9029; } /* Attribute */ code span.bn { color: #40a070; } /* BaseN */ code span.bu { } /* BuiltIn */ code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */ code span.ch { color: #4070a0; } /* Char */ code span.cn { color: #880000; } /* Constant */ code span.co { color: #60a0b0; font-style: italic; } /* Comment */ code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */ code span.do { color: #ba2121; font-style: italic; } /* Documentation */ code span.dt { color: #902000; } /* DataType */ code span.dv { color: #40a070; } /* DecVal */ code span.er { color: #ff0000; font-weight: bold; } /* Error */ code span.ex { } /* Extension */ code span.fl { color: #40a070; } /* Float */ code span.fu { color: #06287e; } /* Function */ code span.im { } /* Import */ code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */ code span.kw { color: #007020; font-weight: bold; } /* Keyword */ code span.op { color: #666666; } /* Operator */ code span.ot { color: #007020; } /* Other */ code span.pp { color: #bc7a00; } /* Preprocessor */ code span.sc { color: #4070a0; } /* SpecialChar */ code span.ss { color: #bb6688; } /* SpecialString */ code span.st { color: #4070a0; } /* String */ code span.va { color: #19177c; } /* Variable */ code span.vs { color: #4070a0; } /* VerbatimString */ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */ </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; } for (var j = 0; j < rules.length; j++) { var rule = rules[j]; // check if there is a div.sourceCode rule if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue; var style = rule.style.cssText; // check if color or background-color is set if (rule.style.color === '' && rule.style.backgroundColor === '') 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"></a><span class="kw">library</span>(tidyr)</span> <span id="cb1-2"><a href="#cb1-2"></a><span class="kw">library</span>(dplyr, <span class="dt">warn.conflicts =</span> <span class="ot">FALSE</span>)</span> <span id="cb1-3"><a href="#cb1-3"></a><span class="kw">library</span>(purrr)</span> <span id="cb1-4"><a href="#cb1-4"></a></span> <span id="cb1-5"><a href="#cb1-5"></a><span class="kw">packageVersion</span>(<span class="st">"tidyr"</span>)</span> <span id="cb1-6"><a href="#cb1-6"></a><span class="co">#> [1] '1.1.2'</span></span> <span id="cb1-7"><a href="#cb1-7"></a></span> <span id="cb1-8"><a href="#cb1-8"></a>mini_iris <-<span class="st"> </span><span class="kw">as_tibble</span>(iris)[<span class="kw">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"></a>mini_iris</span> <span id="cb1-10"><a href="#cb1-10"></a><span class="co">#> # A tibble: 6 x 5</span></span> <span id="cb1-11"><a href="#cb1-11"></a><span class="co">#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species </span></span> <span id="cb1-12"><a href="#cb1-12"></a><span class="co">#> <dbl> <dbl> <dbl> <dbl> <fct> </span></span> <span id="cb1-13"><a href="#cb1-13"></a><span class="co">#> 1 5.1 3.5 1.4 0.2 setosa </span></span> <span id="cb1-14"><a href="#cb1-14"></a><span class="co">#> 2 4.9 3 1.4 0.2 setosa </span></span> <span id="cb1-15"><a href="#cb1-15"></a><span class="co">#> 3 7 3.2 4.7 1.4 versicolor</span></span> <span id="cb1-16"><a href="#cb1-16"></a><span class="co">#> 4 6.4 3.2 4.5 1.5 versicolor</span></span> <span id="cb1-17"><a href="#cb1-17"></a><span class="co">#> 5 6.3 3.3 6 2.5 virginica </span></span> <span id="cb1-18"><a href="#cb1-18"></a><span class="co">#> 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("programming.Rmd)</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"></a>mini_iris <span class="op">%>%</span><span class="st"> </span><span class="kw">nest</span>(</span> <span id="cb2-2"><a href="#cb2-2"></a> <span class="dt">petal =</span> <span class="kw">c</span>(Petal.Length, Petal.Width), </span> <span id="cb2-3"><a href="#cb2-3"></a> <span class="dt">sepal =</span> <span class="kw">c</span>(Sepal.Length, Sepal.Width)</span> <span id="cb2-4"><a href="#cb2-4"></a>)</span> <span id="cb2-5"><a href="#cb2-5"></a><span class="co">#> # A tibble: 3 x 3</span></span> <span id="cb2-6"><a href="#cb2-6"></a><span class="co">#> Species petal sepal </span></span> <span id="cb2-7"><a href="#cb2-7"></a><span class="co">#> <fct> <list> <list> </span></span> <span id="cb2-8"><a href="#cb2-8"></a><span class="co">#> 1 setosa <tibble [2 × 2]> <tibble [2 × 2]></span></span> <span id="cb2-9"><a href="#cb2-9"></a><span class="co">#> 2 versicolor <tibble [2 × 2]> <tibble [2 × 2]></span></span> <span id="cb2-10"><a href="#cb2-10"></a><span class="co">#> 3 virginica <tibble [2 × 2]> <tibble [2 × 2]></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"></a>mini_iris <span class="op">%>%</span><span class="st"> </span><span class="kw">nest</span>(</span> <span id="cb3-2"><a href="#cb3-2"></a> <span class="dt">petal =</span> <span class="kw">all_of</span>(<span class="kw">c</span>(<span class="st">"Petal.Length"</span>, <span class="st">"Petal.Width"</span>)), </span> <span id="cb3-3"><a href="#cb3-3"></a> <span class="dt">sepal =</span> <span class="kw">all_of</span>(<span class="kw">c</span>(<span class="st">"Sepal.Length"</span>, <span class="st">"Sepal.Width"</span>))</span> <span id="cb3-4"><a href="#cb3-4"></a>)</span> <span id="cb3-5"><a href="#cb3-5"></a><span class="co">#> # A tibble: 3 x 3</span></span> <span id="cb3-6"><a href="#cb3-6"></a><span class="co">#> Species petal sepal </span></span> <span id="cb3-7"><a href="#cb3-7"></a><span class="co">#> <fct> <list> <list> </span></span> <span id="cb3-8"><a href="#cb3-8"></a><span class="co">#> 1 setosa <tibble [2 × 2]> <tibble [2 × 2]></span></span> <span id="cb3-9"><a href="#cb3-9"></a><span class="co">#> 2 versicolor <tibble [2 × 2]> <tibble [2 × 2]></span></span> <span id="cb3-10"><a href="#cb3-10"></a><span class="co">#> 3 virginica <tibble [2 × 2]> <tibble [2 × 2]></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"></a><span class="fu">on</span><span class="kw">:</span></span> <span id="cb4-2"><a href="#cb4-2"></a><span class="at"> </span><span class="fu">push</span><span class="kw">:</span></span> <span id="cb4-3"><a href="#cb4-3"></a><span class="at"> </span><span class="fu">branches</span><span class="kw">:</span></span> <span id="cb4-4"><a href="#cb4-4"></a><span class="at"> </span><span class="kw">-</span><span class="at"> master</span></span> <span id="cb4-5"><a href="#cb4-5"></a><span class="at"> </span><span class="fu">pull_request</span><span class="kw">:</span></span> <span id="cb4-6"><a href="#cb4-6"></a><span class="at"> </span><span class="fu">branches</span><span class="kw">:</span></span> <span id="cb4-7"><a href="#cb4-7"></a><span class="at"> </span><span class="kw">-</span><span class="at"> master</span></span> <span id="cb4-8"><a href="#cb4-8"></a></span> <span id="cb4-9"><a href="#cb4-9"></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"></a></span> <span id="cb4-11"><a href="#cb4-11"></a><span class="fu">jobs</span><span class="kw">:</span></span> <span id="cb4-12"><a href="#cb4-12"></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"></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"></a><span class="at"> </span><span class="fu">steps</span><span class="kw">:</span></span> <span id="cb4-15"><a href="#cb4-15"></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"></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@master</span></span> <span id="cb4-17"><a href="#cb4-17"></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"></a><span class="fu"> run</span><span class="kw">: </span><span class="ch">|</span></span> <span id="cb4-19"><a href="#cb4-19"></a> install.packages(c("remotes", "rcmdcheck"))</span> <span id="cb4-20"><a href="#cb4-20"></a> remotes::install_deps(dependencies = TRUE)</span> <span id="cb4-21"><a href="#cb4-21"></a> remotes::install_github("tidyverse/tidyr")</span> <span id="cb4-22"><a href="#cb4-22"></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"></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"></a><span class="at"> </span><span class="fu">run</span><span class="kw">:</span><span class="at"> rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error")</span></span> <span id="cb4-25"><a href="#cb4-25"></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/master/.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 -> 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://principles.tidyverse.org/changes-multivers.html" class="uri">https://principles.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@rstudio.com">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"></a>tidyr_new_interface <-<span class="st"> </span><span class="cf">function</span>() {</span> <span id="cb5-2"><a href="#cb5-2"></a> <span class="kw">packageVersion</span>(<span class="st">"tidyr"</span>) <span class="op">></span><span class="st"> "0.8.99"</span></span> <span id="cb5-3"><a href="#cb5-3"></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"></a>my_function_inside_a_package <-<span class="st"> </span><span class="cf">function</span>(...)</span> <span id="cb6-2"><a href="#cb6-2"></a> <span class="co"># my code here</span></span> <span id="cb6-3"><a href="#cb6-3"></a></span> <span id="cb6-4"><a href="#cb6-4"></a> <span class="cf">if</span> (<span class="kw">tidyr_new_interface</span>()) {</span> <span id="cb6-5"><a href="#cb6-5"></a> <span class="co"># Freshly written code for v1.0.0</span></span> <span id="cb6-6"><a href="#cb6-6"></a> out <-<span class="st"> </span>tidyr<span class="op">::</span><span class="kw">nest</span>(df, <span class="dt">data =</span> <span class="kw">any_of</span>(<span class="kw">c</span>(<span class="st">"x"</span>, <span class="st">"y"</span>, <span class="st">"z"</span>)))</span> <span id="cb6-7"><a href="#cb6-7"></a> } <span class="cf">else</span> {</span> <span id="cb6-8"><a href="#cb6-8"></a> <span class="co"># Existing code for v0.8.3</span></span> <span id="cb6-9"><a href="#cb6-9"></a> out <-<span class="st"> </span>tidyr<span class="op">::</span><span class="kw">nest</span>(df, x, y, z)</span> <span id="cb6-10"><a href="#cb6-10"></a> }</span> <span id="cb6-11"><a href="#cb6-11"></a></span> <span id="cb6-12"><a href="#cb6-12"></a> <span class="co"># more code here</span></span> <span id="cb6-13"><a href="#cb6-13"></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 = <something about existing cols></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://principles.tidyverse.org/dots-data.html" class="uri">https://principles.tidyverse.org/dots-data.html</a></p></li> <li><p>The <code>new_col = <something about existing cols></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"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb7-2"><a href="#cb7-2"></a><span class="st"> </span><span class="kw">nest</span>(<span class="dt">petal =</span> <span class="kw">matches</span>(<span class="st">"Petal"</span>), <span class="dt">sepal =</span> <span class="kw">matches</span>(<span class="st">"Sepal"</span>)) </span> <span id="cb7-3"><a href="#cb7-3"></a><span class="co">#> # A tibble: 3 x 3</span></span> <span id="cb7-4"><a href="#cb7-4"></a><span class="co">#> Species petal sepal </span></span> <span id="cb7-5"><a href="#cb7-5"></a><span class="co">#> <fct> <list> <list> </span></span> <span id="cb7-6"><a href="#cb7-6"></a><span class="co">#> 1 setosa <tibble [2 × 2]> <tibble [2 × 2]></span></span> <span id="cb7-7"><a href="#cb7-7"></a><span class="co">#> 2 versicolor <tibble [2 × 2]> <tibble [2 × 2]></span></span> <span id="cb7-8"><a href="#cb7-8"></a><span class="co">#> 3 virginica <tibble [2 × 2]> <tibble [2 × 2]></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"></a><span class="co"># v0.8.3</span></span> <span id="cb8-2"><a href="#cb8-2"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb8-3"><a href="#cb8-3"></a><span class="st"> </span><span class="kw">nest</span>(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, <span class="dt">.key =</span> <span class="st">"my_data"</span>)</span> <span id="cb8-4"><a href="#cb8-4"></a></span> <span id="cb8-5"><a href="#cb8-5"></a><span class="co"># v1.0.0</span></span> <span id="cb8-6"><a href="#cb8-6"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb8-7"><a href="#cb8-7"></a><span class="st"> </span><span class="kw">nest</span>(<span class="dt">my_data =</span> <span class="kw">c</span>(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))</span> <span id="cb8-8"><a href="#cb8-8"></a></span> <span id="cb8-9"><a href="#cb8-9"></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"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb8-11"><a href="#cb8-11"></a><span class="st"> </span><span class="kw">nest</span>(<span class="dt">my_data =</span> <span class="kw">any_of</span>(<span class="kw">c</span>(<span class="st">"Sepal.Length"</span>, <span class="st">"Sepal.Width"</span>, <span class="st">"Petal.Length"</span>, <span class="st">"Petal.Width"</span>)))</span> <span id="cb8-12"><a href="#cb8-12"></a></span> <span id="cb8-13"><a href="#cb8-13"></a><span class="co"># or equivalently:</span></span> <span id="cb8-14"><a href="#cb8-14"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb8-15"><a href="#cb8-15"></a><span class="st"> </span><span class="kw">nest</span>(<span class="dt">my_data =</span> <span class="op">!</span><span class="kw">any_of</span>(<span class="st">"Species"</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"></a><span class="cf">if</span> (<span class="kw">tidyr_new_interface</span>()) {</span> <span id="cb9-2"><a href="#cb9-2"></a> out <-<span class="st"> </span>tidyr<span class="op">::</span><span class="kw">nest_legacy</span>(df, x, y, z)</span> <span id="cb9-3"><a href="#cb9-3"></a>} <span class="cf">else</span> {</span> <span id="cb9-4"><a href="#cb9-4"></a> out <-<span class="st"> </span>tidyr<span class="op">::</span><span class="kw">nest</span>(df, x, y, z)</span> <span id="cb9-5"><a href="#cb9-5"></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"></a><span class="co"># v0.8.3</span></span> <span id="cb10-2"><a href="#cb10-2"></a>df <span class="op">%>%</span><span class="st"> </span><span class="kw">unnest</span>(x, <span class="dt">.id =</span> <span class="st">"id"</span>)</span> <span id="cb10-3"><a href="#cb10-3"></a></span> <span id="cb10-4"><a href="#cb10-4"></a><span class="co"># v1.0.0</span></span> <span id="cb10-5"><a href="#cb10-5"></a>df <span class="op">%>%</span><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">id =</span> <span class="kw">names</span>(x)) <span class="op">%>%</span><span class="st"> </span><span class="kw">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://principles.tidyverse.org/dots-data.html" class="uri">https://principles.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"></a>nested <-<span class="st"> </span>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb11-2"><a href="#cb11-2"></a><span class="st"> </span><span class="kw">nest</span>(<span class="dt">my_data =</span> <span class="kw">c</span>(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))</span> <span id="cb11-3"><a href="#cb11-3"></a></span> <span id="cb11-4"><a href="#cb11-4"></a><span class="co"># v0.8.3 automatically unnests list-cols</span></span> <span id="cb11-5"><a href="#cb11-5"></a>nested <span class="op">%>%</span><span class="st"> </span><span class="kw">unnest</span>()</span> <span id="cb11-6"><a href="#cb11-6"></a></span> <span id="cb11-7"><a href="#cb11-7"></a><span class="co"># v1.0.0 must be told which columns to unnest</span></span> <span id="cb11-8"><a href="#cb11-8"></a>nested <span class="op">%>%</span><span class="st"> </span><span class="kw">unnest</span>(<span class="kw">any_of</span>(<span class="st">"my_data"</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"></a><span class="cf">if</span> (<span class="kw">tidyr_new_interface</span>()) {</span> <span id="cb12-2"><a href="#cb12-2"></a> out <-<span class="st"> </span>tidyr<span class="op">::</span><span class="kw">unnest_legacy</span>(df)</span> <span id="cb12-3"><a href="#cb12-3"></a>} <span class="cf">else</span> {</span> <span id="cb12-4"><a href="#cb12-4"></a> out <-<span class="st"> </span>tidyr<span class="op">::</span><span class="kw">unnest</span>(df)</span> <span id="cb12-5"><a href="#cb12-5"></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"></a>(df <-<span class="st"> </span>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb13-2"><a href="#cb13-2"></a><span class="st"> </span><span class="kw">group_by</span>(Species) <span class="op">%>%</span><span class="st"> </span></span> <span id="cb13-3"><a href="#cb13-3"></a><span class="st"> </span><span class="kw">nest</span>())</span> <span id="cb13-4"><a href="#cb13-4"></a><span class="co">#> # A tibble: 3 x 2</span></span> <span id="cb13-5"><a href="#cb13-5"></a><span class="co">#> # Groups: Species [3]</span></span> <span id="cb13-6"><a href="#cb13-6"></a><span class="co">#> Species data </span></span> <span id="cb13-7"><a href="#cb13-7"></a><span class="co">#> <fct> <list> </span></span> <span id="cb13-8"><a href="#cb13-8"></a><span class="co">#> 1 setosa <tibble [2 × 4]></span></span> <span id="cb13-9"><a href="#cb13-9"></a><span class="co">#> 2 versicolor <tibble [2 × 4]></span></span> <span id="cb13-10"><a href="#cb13-10"></a><span class="co">#> 3 virginica <tibble [2 × 4]></span></span> <span id="cb13-11"><a href="#cb13-11"></a>(external_variable <-<span class="st"> </span><span class="kw">map_int</span>(df<span class="op">$</span>data, nrow))</span> <span id="cb13-12"><a href="#cb13-12"></a><span class="co">#> [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"></a>df <span class="op">%>%</span><span class="st"> </span></span> <span id="cb14-2"><a href="#cb14-2"></a><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">n_rows =</span> external_variable)</span> <span id="cb14-3"><a href="#cb14-3"></a><span class="co">#> Error: Problem with `mutate()` input `n_rows`.</span></span> <span id="cb14-4"><a href="#cb14-4"></a><span class="co">#> x Input `n_rows` can't be recycled to size 1.</span></span> <span id="cb14-5"><a href="#cb14-5"></a><span class="co">#> ℹ Input `n_rows` is `external_variable`.</span></span> <span id="cb14-6"><a href="#cb14-6"></a><span class="co">#> ℹ Input `n_rows` must be size 1, not 3.</span></span> <span id="cb14-7"><a href="#cb14-7"></a><span class="co">#> ℹ The error occurred in group 1: Species = "setosa".</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"></a>df <span class="op">%>%</span><span class="st"> </span></span> <span id="cb15-2"><a href="#cb15-2"></a><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">n_rows =</span> <span class="kw">map_int</span>(data, nrow))</span> <span id="cb15-3"><a href="#cb15-3"></a><span class="co">#> # A tibble: 3 x 3</span></span> <span id="cb15-4"><a href="#cb15-4"></a><span class="co">#> # Groups: Species [3]</span></span> <span id="cb15-5"><a href="#cb15-5"></a><span class="co">#> Species data n_rows</span></span> <span id="cb15-6"><a href="#cb15-6"></a><span class="co">#> <fct> <list> <int></span></span> <span id="cb15-7"><a href="#cb15-7"></a><span class="co">#> 1 setosa <tibble [2 × 4]> 2</span></span> <span id="cb15-8"><a href="#cb15-8"></a><span class="co">#> 2 versicolor <tibble [2 × 4]> 2</span></span> <span id="cb15-9"><a href="#cb15-9"></a><span class="co">#> 3 virginica <tibble [2 × 4]> 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"></a>df <span class="op">%>%</span><span class="st"> </span></span> <span id="cb16-2"><a href="#cb16-2"></a><span class="st"> </span>tibble<span class="op">::</span><span class="kw">add_column</span>(<span class="dt">n_rows =</span> external_variable)</span> <span id="cb16-3"><a href="#cb16-3"></a><span class="co">#> # A tibble: 3 x 3</span></span> <span id="cb16-4"><a href="#cb16-4"></a><span class="co">#> # Groups: Species [3]</span></span> <span id="cb16-5"><a href="#cb16-5"></a><span class="co">#> Species data n_rows</span></span> <span id="cb16-6"><a href="#cb16-6"></a><span class="co">#> <fct> <list> <int></span></span> <span id="cb16-7"><a href="#cb16-7"></a><span class="co">#> 1 setosa <tibble [2 × 4]> 2</span></span> <span id="cb16-8"><a href="#cb16-8"></a><span class="co">#> 2 versicolor <tibble [2 × 4]> 2</span></span> <span id="cb16-9"><a href="#cb16-9"></a><span class="co">#> 3 virginica <tibble [2 × 4]> 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"></a><span class="co"># v0.8.3</span></span> <span id="cb17-2"><a href="#cb17-2"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb17-3"><a href="#cb17-3"></a><span class="st"> </span><span class="kw">nest_</span>(</span> <span id="cb17-4"><a href="#cb17-4"></a> <span class="dt">key_col =</span> <span class="st">"my_data"</span>,</span> <span id="cb17-5"><a href="#cb17-5"></a> <span class="dt">nest_cols =</span> <span class="kw">c</span>(<span class="st">"Sepal.Length"</span>, <span class="st">"Sepal.Width"</span>, <span class="st">"Petal.Length"</span>, <span class="st">"Petal.Width"</span>)</span> <span id="cb17-6"><a href="#cb17-6"></a> )</span> <span id="cb17-7"><a href="#cb17-7"></a></span> <span id="cb17-8"><a href="#cb17-8"></a>nested <span class="op">%>%</span><span class="st"> </span><span class="kw">unnest_</span>(<span class="op">~</span><span class="st"> </span>my_data)</span> <span id="cb17-9"><a href="#cb17-9"></a></span> <span id="cb17-10"><a href="#cb17-10"></a><span class="co"># v1.0.0</span></span> <span id="cb17-11"><a href="#cb17-11"></a>mini_iris <span class="op">%>%</span><span class="st"> </span></span> <span id="cb17-12"><a href="#cb17-12"></a><span class="st"> </span><span class="kw">nest</span>(<span class="dt">my_data =</span> <span class="kw">any_of</span>(<span class="kw">c</span>(<span class="st">"Sepal.Length"</span>, <span class="st">"Sepal.Width"</span>, <span class="st">"Petal.Length"</span>, <span class="st">"Petal.Width"</span>)))</span> <span id="cb17-13"><a href="#cb17-13"></a></span> <span id="cb17-14"><a href="#cb17-14"></a>nested <span class="op">%>%</span><span class="st"> </span><span class="kw">unnest</span>(<span class="kw">any_of</span>(<span class="st">"my_data"</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>