reformat_code: Format the code in the usage and examples sections

View source: R/build.R

reformat_codeR Documentation

Format the code in the usage and examples sections


The function tidy_source in the formatR package is used to polish the Rd files generated by roxygen2 in the usage and examples sections.


reformat_code(path, ...)



the path of the Rd file


other arguments passed to tidy_source


NULL; as a side effect, the original Rd file will be updated


If the usage or examples code is not syntactically correct, it will not be reformatted and a message will be printed on screen. One possible situation is the percent symbol %, which should be escaped even in the examples code (cf Writing R Extensions), and this can make the code syntactically incorrect, e.g. a %in% b should be a \%in\% b but the latter is not valid R code. Anyway, this function will try to unescape the percent symbols before reformating the code, then escape them.


Yihui Xie <>

See Also



rd.file = system.file("examples", "reformat_code_demo.Rd", package = "Rd2roxygen")
file.copy(rd.file, tempdir())
fmt.file = file.path(tempdir(), "reformat_code_demo.Rd")  ## show the raw Rd

reformat_code(fmt.file)  ## the formatted Rd

Rd2roxygen documentation built on May 29, 2024, 7:30 a.m.