October 7, 2022

– Advertisement –

[This article was first published on R – datawookie, and kindly contributed to R-bloggers], (You can report a problem about the content of this page here) Want to share your content on R-Blogger? Click here if you have a blog, or click here if you don’t.

– Advertisement –

– Advertisement –

In the previous post we saw how to implement linter and styler in a Python project. Now we are going to do the same for the R project. We will be using the {precommit} R package to make setup easier.

establish

– Advertisement –

{precommit} Install the package.

install.packages(“precommit”) setup

Set up a precommit for a project.

precommit::use_precommit()

This will create a .pre-commit-config.yaml configuration file and add it to the .Rbuildignore, if present.

The contents of the .pre-commit-config.yaml file should look something like this (I’ve removed the comment for brevity):

repo:- repo:https://github.com/lorenzwalthert/precommit rev:v0.3.2.9001 hook:- id:style-files args: [–style_pkg=styler, –style_fun=tidyverse_style]
– id: roxygenize – id: use-tidy-description – id: Spell checking deprecated: > (?x)^( .*\.[rR], .*\.feather| .*\.jpeg| .*\.pdf| .*\.png| .*\.py| .*\.rdata| .*\.rds| .*\.rds| .*\.Rproj| .*\.sh| (.*/|)\.gitignore| (.*/|)\.gitlab-ci\.yml| (.*/|)\.lintr| (.*/|)\.pre-commit-.*| (.*/|)\.Rbuildignore| , Renviron| (.*/|)\.Rprofile| (.*/|)\.travis\.yml| (.*/|)appveyor\.yml| (.*/|) namespace| (.*/|)renv/settings\.dcf| (.*/|)renv\.lock| (.*/|)wordlist| \.github/workflows/.*| data/.*| )$ – id: linter – id: readme-rmd-render – id: parseable -r – id: no-browser-statement – id: no-debug-statement – id: dps-in-desk – repo: https:// /github.com/pre-commit/pre-commit-hooks rev:v4.3.0 Hooks: – id: check-added-large-files args: [‘–maxkb=200’]
– id: file-content-sorter Files: ‘^\.Rbuildignore$’ – id: end-of-file-fixer Deprecated: ‘\.Rd’ – repo: https://github.com/pre-commit-ci /pre-commit-ci-config rev:v1.5.1 Hooks: -id:check-pre-commit-ci-configuration -repo:local hooks: -id:forbidden-to-commit name:do not commit common R artifacts : Cannot commit. History, .RData, .Rds or .rds. Language: Failed Files: ‘\.(Rhistory|RData|Rds|rds)$’ # `Excluded: ` to allow committing to specific files ci:autoupdate_schedule:monthly

See also  Russian Doll Code Refactoring | r-bloggers

If you are using {roxygen2} you may be prompted to run the following:

precommit::snippet_generate(‘additional-deps-roxygenize’)

This will probably generate additional instructions about the changes you want to make to the id: roxygenize key in the .pre-commit-config.yaml file. implement them.

You may also want to add trailing-whitespace and check-yaml rules to .pre-commit-config.yaml under https://github.com/pre-commit/pre-commit-hooks repository.

to promise

After configuring {precommit}, stage the .pre-commit-config.yaml and .Rbuildignore files and then try to commit. You may need to work quite hard to get all the checks to pass, especially if there is a lot of code in the repository. Just be methodical in addressing each and every error raised by hook processes.

You may get error about not finding /usr/lib/R/Rscript . This means that the pre-commit is looking in the wrong place for the pattern. I just made a link from /usr/lib/R/Rscript to the actual location of the transcript.

{linter} package

The {lintr} package will perform static analysis on your R code and help identify syntactic problems.

establish

{linter} Install.

install.packages(“lintr”) Configure

Create a .lintr file in the project root.

Linter::use_lintr()

You can update the .lintr file so that {lintr} treats your files.

Linters: linters_with_defaults(line_length_linter(120), object_usage_linter = NULL, object_name_linter(c(“snake_case”, “SNAKE_CASE”)), commented_code_linter = NULL ) Exclusions: list() encoding: “UTF-8”

You may want to add “dotted.case” as another argument to object_name_linter() .

except code

You can exclude parts of code from linting by adding nolint hints as comments.

# The following line of code will be ignored by {lintr}. n By default the {precommit} package will invoke the {styler} package and will apply the Tidyverse style (via tidyverse_style).

establish

See also  R User Group Gainesville: Experimenting With New Event Formats | r-bloggers

{styler} Install.

install.packages(“styler”) thrives

With the {lintr} and {styler} packages installed and kicked off on each commit via the pre-commit framework you can be assured that the code you push is syntactically correct and consistently formatted.

related

Source link

– Advertisement –