R code optimization with lintr and styler

Tool
R
Author

Tony D

Published

March 15, 2025

An introduction to lintr and styler for R code optimization, with examples of how to use them for static code analysis and formatting.

This document introduces two essential R packages for code optimization: lintr and styler. It explains how lintr performs static code analysis to help you identify and fix style inconsistencies, syntax errors, and other potential issues in your R code. The document also demonstrates how styler can automatically format your code to adhere to the tidyverse style guide, ensuring consistency and readability. You’ll find practical examples of how to use both packages to improve the quality of your R code.

pacakge for R code optimization

lintr

{lintr} provides static code analysis for R. It checks for adherence to a given style, identifying syntax errors and possible semantic issues, then reports them to you so you can take action. Watch lintr in action in the following animation:

https://github.com/r-lib/lintr

Code
pak::pkg_install("lintr")
Code
library(lintr)
Code
lint("
a = 123
    c=5
")
<text>:2:3: style: [assignment_linter] Use one of <-, <<- for assignment, not =.
a = 123
  ^
<text>:3:4: style: [indentation_linter] Indentation should be 0 spaces but is 4 spaces.
    c=5
~~~^
<text>:3:6: style: [assignment_linter] Use one of <-, <<- for assignment, not =.
    c=5
     ^
<text>:3:6: style: [infix_spaces_linter] Put spaces around all infix operators.
    c=5
     ^
Code
lint("test.R")
/Users/jinchaoduan/Documents/Project/Tech-blog/posts/r_code_optimization/test.R:3:1: style: [object_name_linter] Variable and function name style should match snake_case or symbols.
Good <- 1
^~~~
/Users/jinchaoduan/Documents/Project/Tech-blog/posts/r_code_optimization/test.R:4:1: style: [object_name_linter] Variable and function name style should match snake_case or symbols.
applePie <- Good + 1
^~~~~~~~
/Users/jinchaoduan/Documents/Project/Tech-blog/posts/r_code_optimization/test.R:5:1: style: [object_name_linter] Variable and function name style should match snake_case or symbols.
Peter <- d + 1
^~~~~

styler

styler formats your code according to the tidyverse style guide (or your custom style guide) so you can direct your attention to the content of your code. It helps to keep the coding style consistent across projects and facilitate collaboration. You can access styler through

https://github.com/r-lib/styler

Code
pak::pkg_install("styler")
Code
library(styler)
Code
style_text("

a = 3

small_Car='Toyota'
Big=330


")
a <- 3

small_Car <- "Toyota"
Big <- 330

Before

Code
library("dplyr")

   Good <- 1
applePie <- Good + 1
    Peter <- d + 1

Auto formating

Code
test.R
style_file("test.R")
Styling  1  files:
 test.R ✔ 
────────────────────────────────────────
Status  Count   Legend 
✔   1   File unchanged.
ℹ   0   File changed.
✖   0   Styling threw an error.
────────────────────────────────────────

After

Code
library("dplyr")

Good <- 1
applePie <- Good + 1
Peter <- d + 1

reference