make_func_call_explicit() adds namespace qualifiers to
calls from selected packages. package_func_call_explicit()
applies the same operation throughout a package.
file <- tempfile(fileext = ".R")
writeLines(
c(
"starwars |>",
" mutate(name, bmi = mass / ((height / 100)^2)) |>",
" select(name:mass, bmi)"
),
file
)
make_func_call_explicit(file, use_packages = "dplyr")
#> ✔ Successfully made function call explicit in '/tmp/RtmplsgiIp/file21853b92eb8a.R'
readLines(file) |> cli::cli_code()
#> dplyr::starwars |>
#> dplyr::mutate(name, bmi = mass / ((height / 100)^2)) |>
#> dplyr::select(name:mass, bmi)detect_lost_glue_brace() checks one file;
package_lost_glue_brace() checks an entire package. Both
support glue and cli expressions, and print
the offending source line with cli syntax highlighting.
file <- tempfile(fileext = ".R")
writeLines(
c(
'name <- "world"',
'msg <- glue::glue("Hello, {name!")',
'cli::cli_alert_warning("{.field warning}}")'
),
file
)
detect_lost_glue_brace(file)
#> msg <- glue::glue("Hello, {name!")
#> ^^^^^^
#> cli::cli_alert_warning("{.field warning}}")
#> ^^^^^^^^^^^^^^^^^
#> ✖ Found 2 lines with mismatched braces: 2 and 3make_func_arg_explicit() makes arguments in one file
explicit, and package_func_arg_explicit() applies this
transformation across a package.
file <- tempfile(fileext = ".R")
writeLines("vapply(1:9, function(x) x * 2, numeric(1))", file)
make_func_arg_explicit(file)
#> ✔ Made function arguments explicit in '/tmp/RtmplsgiIp/file218550873902.R'
readLines(file) |> cli::cli_code()
#> vapply(X = 1:9, FUN = function(x) x * 2, FUN.VALUE = numeric(length = 1))Use rename_func() to apply a naming style to function
definitions in a file.
file <- tempfile(fileext = ".R")
writeLines("this_is_a_function <- function() message('Hello, world')", file)
rename_func(file, style = "camelCase")
#> ✔ Renamed 1 function to "camelCase" style in '/tmp/RtmplsgiIp/file2185661e228d.R'
readLines(file) |> cli::cli_code()
#> thisIsAFunction <- function() message('Hello, world')detect_print_and_cat() reports print() and
cat() calls in one file.
package_print_and_cat() checks an entire package. Set
fix = TRUE to replace those calls in the selected file with
message(). As for the glue brace detection, the reported
source line is syntax highlighted.
detect_unmatched_nocov_marker() checks the
# nocov start and # nocov end markers of one
file, and package_unmatched_nocov_marker() checks a whole
package. A file may hold at most one marker of each kind, so every
further start or end is reported with its line and column, together with
a start or an end that has no counterpart and an end placed before the
start. Both counts must therefore match: an unclosed start is reported
as well, because covr::package_coverage() aborts on such a
range with a “range start but only range ends” error.
Detection is purely textual, so a marker inside a string literal or a roxygen comment is reported as well (as covr does); such a marker has no effect on coverage, so it is usually a leftover to remove. Nothing is rewritten — the offending comment is pointed at and the fix is left to you.
file <- tempfile(fileext = ".R")
writeLines(
c(
"# nocov start",
"f <- function(x) x + 1",
"# nocov start",
"# nocov end"
),
file
)
detect_unmatched_nocov_marker(file)
#> ✖ `nocov start` at line 3, column 1: only one `nocov start` is allowed per file, the first is `nocov start` at line 1, column 1.
#> # nocov start
#> ^^^^^^^^^^^^^
#> ✖ Found 1 mismatched `nocov` marker on line 3.A # nocov start that is never closed is reported as
well:
writeLines(c("# nocov start", "g <- function(y) y"), file)
detect_unmatched_nocov_marker(file)
#> ✖ `nocov start` at line 1, column 1 has no matching `nocov end`.
#> # nocov start
#> ^^^^^^^^^^^^^
#> ✖ Found 1 mismatched `nocov` marker on line 1.pkg <- file.path(tempdir(), "demo")
dir.create(file.path(pkg, "R"), recursive = TRUE, showWarnings = FALSE)
writeLines(
c("Package: demo", "Version: 0.0.1"),
file.path(pkg, "DESCRIPTION")
)
writeLines(c("g <- function(y) y", "# nocov end"), file.path(pkg, "R", "g.R"))
package_unmatched_nocov_marker(pkg)
#> 'R/g.R': 2:
#> ✖ `nocov end` at line 2, column 1 has no matching `nocov start`.
#> # nocov end
#> ^^^^^^^^^^^
#> ✖ Found 1 mismatched `nocov` marker in 1 of 1 file.CRAN does not allow a path-like argument of an exported function to
default to a concrete location such as "." or
"~/data.csv". detect_fn_arg_path() checks one
file and package_fn_arg_path() checks every .R
file of a package, using the exports declared in
NAMESPACE.
file <- tempfile(fileext = ".R")
writeLines(
c(
'read_csv <- function(path = "~/data.csv", n = 1L) NULL',
'read_all <- function(file = "data.txt") NULL',
'read_tsv <- function(path = NULL, n = 1L) NULL'
),
file
)
detect_fn_arg_path(file)
#> '/tmp/RtmplsgiIp/file218521146b3f.R':1:22 read_csv(): `path` defaults to
#> "~/data.csv"
#> read_csv <- function(path = "~/data.csv", n = 1L) NULL
#> ^^^^^^^^^^^^^^^^^^^
#> '/tmp/RtmplsgiIp/file218521146b3f.R':2:22 read_all(): `file` defaults to
#> "data.txt"
#> read_all <- function(file = "data.txt") NULL
#> ^^^^^^^^^^^^^^^^^
#> ✖ Found 2 arguments with a default path in '/tmp/RtmplsgiIp/file218521146b3f.R'An argument is reported when its name matches
arg_pattern and its default is not listed in
allow, so read_tsv() above is clean: its
default is NULL. An argument without a default is always
fine. Narrow the pattern when a loose match is not wanted:
detect_fn_arg_path(file, arg_pattern = "^path$")
#> '/tmp/RtmplsgiIp/file218521146b3f.R':1:22 read_csv(): `path` defaults to
#> "~/data.csv"
#> read_csv <- function(path = "~/data.csv", n = 1L) NULL
#> ^^^^^^^^^^^^^^^^^^^
#> ✖ Found 1 argument with a default path in '/tmp/RtmplsgiIp/file218521146b3f.R'detect_fn_arg_path() uses the active document when
path is NULL, and checks every top-level
function of that file unless exports or
export_patterns say which ones count as exported. Use the
package-wide function to see all the occurrences at once:
Convert between function() and the short
\() syntax.
convert_knitr_chunk_header() converts legacy R Markdown
chunk options to the current comment-based syntax.
file <- tempfile(fileext = ".Rmd")
writeLines(c("```{r, echo=TRUE, fig.width=10}", "x <- 1", "```"), file)
convert_knitr_chunk_header(file)#> ℹ Converting knitr chunk headers in '/tmp/RtmplsgiIp/file218557e8cb12.Rmd'
readLines(file) |> cli::cli_code()
#> ```{r}
#> #| echo = TRUE,
#> #| fig.width = 10
#> x <- 1
#> ```
convert_pipe() rewrites pipes in one file between
%>% and |>. Strings, comments, and the
other magrittr operators (%<>%, %$%) are
left unchanged.
file <- tempfile(fileext = ".R")
writeLines("mtcars %>% dplyr::filter(cyl > 4)", file)
convert_pipe(file, direction = "to_base")
#> ✔ Converted pipes in '/tmp/RtmplsgiIp/file2185130cf763.R' ("to_base")
readLines(file) |> cli::cli_code()
#> mtcars |> dplyr::filter(cyl > 4)Use package_convert_pipe() to process .R
and .r files below a package’s R/
directory.
convert_int_literals() adds L to bare
integer literals while leaving strings, comments, floating-point values,
and existing suffixes unchanged.
file <- tempfile(fileext = ".R")
writeLines("x <- seq_len(10) # length 10", file)
convert_int_literals(file)
#> ✔ Added explicit integer suffixes in '/tmp/RtmplsgiIp/file2185118bfecc.R'
readLines(file) |> cli::cli_code()
#> x <- seq_len(10L) # length 10Use package_convert_int_literals() to process
.R and .r files below a package’s
R/ and tests/ directories.
convert_nonascii_code() converts non-ASCII source to
Unicode escape sequences when needed for CRAN-compatible source files.
It accepts either a file path or an R expression.
Air 0.8.0 and newer can align the elements of a c() or
list() call column by column, giving the layout of
tibble::tribble(). The feature is switched on by the
experimental # fmt: table directive placed right above the
call, but air never re-flows the elements on its own: whatever sits on
one source line stays there.
use_air_table() does the two things air does not do by
itself. It runs air format, inserts the directive above
every unnamed c()/list() call that holds more
than min_length elements, and re-flows those elements so
that each row holds ncol of them; a second
air format run then pads the cells and aligns the
columns.
file <- tempfile(fileext = ".R")
writeLines(
'x <- c("alpha", "beta", "gamma", "delta", "epsilon", "zeta")',
file
)
use_air_table(file, ncol = 3L, min_length = 5L)
readLines(file) |> cli::cli_code()Keep in mind that:
air must be installed and on the PATH;
check_air_installed() reports the installation command for
each operating system.