Skip to contents

Converts bare integer literals to explicit integer form with an L suffix, e.g. seq_len(10) becomes seq_len(10L). Strings and comments are left unchanged.

convert_int_literals() operates on one R file. When path is NULL and RStudio is available, it uses the currently active document. package_convert_int_literals() walks selected directories of an R package and applies the same conversion to every .R / .r file found, except files generated by Rcpp::compileAttributes() or rextendr::document() (see exclude).

Usage

package_convert_int_literals(
  path = NULL,
  dirs = c("R"),
  recursive = TRUE,
  exclude = c("RcppExports.R", "extendr-wrappers.R"),
  ...
)

convert_int_literals(path = NULL, verbose = TRUE, ...)

Arguments

path

For convert_int_literals(), a character string specifying the R file to modify. If NULL and RStudio is available, the currently active document path is used.

For package_convert_int_literals(), a character string specifying the package root. If NULL, the current working directory is used.

dirs

Character vector of subdirectories relative to path to search. Used only by package_convert_int_literals(). Defaults to c("R").

recursive

Logical; recurse into subdirectories. Used only by package_convert_int_literals(). Default TRUE.

exclude

Character vector of file names to skip, matched case-insensitively against the file base name. Used only by package_convert_int_literals(). Defaults to the auto-generated wrapper files c("RcppExports.R", "extendr-wrappers.R"). Use NULL (or character()) to process all files.

...

Additional arguments. Currently unused and must be empty.

verbose

Logical; enable or disable per-file messages. Used only by convert_int_literals(). Default TRUE.

Value

convert_int_literals() invisibly returns the modified file path. package_convert_int_literals() invisibly returns a character vector of modified file paths.

Details

A token is treated as an integer literal when it is:

  • a decimal integer (10, 0, - is not part of the token), or

  • a hexadecimal integer (0xFF, 0X10),

and it is not:

  • already suffixed with L / l,

  • a floating-point number (10., 1.0, .5),

  • scientific notation, including a signed exponent (1e5, 1e-3, 1E-3),

  • a complex literal (10i),

  • adjacent to an identifier character (a-zA-Z0-9._).

Examples

if (FALSE) { # \dontrun{
# --- Single file ---
temp <- tempfile(fileext = ".R")
writeLines("tmp <- seq_len(10)", temp)
convert_int_literals(temp)
readLines(temp)
# "tmp <- seq_len(10L)"

# --- Entire package ---
tmp_pkg <- tempdir()
usethis::create_package(tmp_pkg, open = FALSE)
writeLines("foo <- seq_len(42)", file.path(tmp_pkg, "R/foo.R"))
package_convert_int_literals(tmp_pkg)
readLines(file.path(tmp_pkg, "R/foo.R"))
} # }