Code
::pkg_install("lintr") pak
Tony D
March 15, 2025
pacakge for R code optimization
{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
<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
^
/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 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
Styling 1 files:
test.R ✔
────────────────────────────────────────
Status Count Legend
✔ 1 File unchanged.
ℹ 0 File changed.
✖ 0 Styling threw an error.
────────────────────────────────────────
After
---
title: "R code optimization with lintr and styler"
author: "Tony D"
execute:
warning: false
error: false
date: "2025-03-15"
categories:
- Tool
- R
image: "images/my screenshots 2.png"
---
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
```{r}
pak::pkg_install("lintr")
```
```{r}
library(lintr)
```
```{r}
lint("
a = 123
c=5
")
```
```{r}
lint("test.R")
```
# 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
```{r}
pak::pkg_install("styler")
```
```{r}
library(styler)
```
```{r}
style_text("
a = 3
small_Car='Toyota'
Big=330
")
```
## Before
```{r}
#| eval: false
library("dplyr")
Good <- 1
applePie <- Good + 1
Peter <- d + 1
```
## Auto formating
```{r filename="test.R"}
style_file("test.R")
```
After
```{r}
#| eval: false
library("dplyr")
Good <- 1
applePie <- Good + 1
Peter <- d + 1
```
# reference