EVOLUTION-MANAGER
Edit File: use_r.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Create or edit R or test files</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for use_r {usethis}"><tr><td>use_r {usethis}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Create or edit R or test files</h2> <h3>Description</h3> <p>This pair of functions makes it easy to create paired R and test files, using the convention that the tests for <code>R/foofy.R</code> should live in <code>tests/testthat/test-foofy.R</code>. You can use them to create new files from scratch by supplying <code>name</code>, or if you use RStudio, you can call to create (or navigate to) the paired file based on the currently open script. </p> <h3>Usage</h3> <pre> use_r(name = NULL, open = rlang::is_interactive()) use_test(name = NULL, open = rlang::is_interactive()) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>name</code></td> <td> <p>Either a name without extension, or <code>NULL</code> to create the paired file based on currently open file in the script editor. If the <code style="white-space: pre;">R/</code> file is open, <code>use_test()</code> will create/open the corresponding test file; if the test file is open, <code>use_r()</code> will create/open the corresponding <code style="white-space: pre;">R/</code> file.</p> </td></tr> <tr valign="top"><td><code>open</code></td> <td> <p>Whether to open the file for interactive editing.</p> </td></tr> </table> <h3>Renaming files in an existing package</h3> <p>Here are some tips on aligning file names across <code style="white-space: pre;">R/</code> and <code style="white-space: pre;">tests/testthat/</code> in an existing package that did not necessarily follow this convention before. </p> <p>This script generates a data frame of <code style="white-space: pre;">R/</code> and test files that can help you identify missed opportunities for pairing: </p> <div class="sourceCode"><pre>library(fs) library(tidyverse) bind_rows( tibble( type = "R", path = dir_ls("R/", regexp = "\\.[Rr]$"), name = as.character(path_ext_remove(path_file(path))), ), tibble( type = "test", path = dir_ls("tests/testthat/", regexp = "/test[^/]+\\.[Rr]$"), name = as.character(path_ext_remove(str_remove(path_file(path), "^test[-_]"))), ) ) %>% pivot_wider(names_from = type, values_from = path) %>% print(n = Inf) </pre></div> <p>The <code><a href="rename_files.html">rename_files()</a></code> function can also be helpful. </p> <h3>See Also</h3> <p>The <a href="https://r-pkgs.org/tests.html">testing</a> and <a href="https://r-pkgs.org/r.html">R code</a> chapters of <a href="https://r-pkgs.org">R Packages</a>. </p> <hr /><div style="text-align: center;">[Package <em>usethis</em> version 2.1.6 <a href="00Index.html">Index</a>]</div> </body></html>