r-lib/lintr · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈
output: rmarkdown::github_document: pandoc_args: ["--wrap=none"]
lintr 
{lintr} 提供 R 的静态代码分析。它检查对给定样式的遵循情况,识别语法错误和可能的语义问题,然后向您报告,以便您采取行动。
例如,给定一个文件 bad.R,其内容如下:
my_func = function(x){
if(x > 0){
print("Positive")
}
else {
print("Not positive")
}
return(T)
}
在此文件上运行 lintr::lint() 将产生以下输出:
lintr::lint("bad.R")
bad.R:1:9: style: [assignment_linter] Use one of <-, <<- for assignment, not =.
my_func = function(x){
^
bad.R:1:22: style: [brace_linter] There should be a space before an opening curly brace.
my_func = function(x){
^
bad.R:1:22: style: [paren_body_linter] Put a space between a right parenthesis and a body expression.
my_func = function(x){
^
bad.R:2:4: style: [indentation_linter] Indentation should be 2 spaces but is 4 spaces.
if(x > 0){
~^
bad.R:2:7: style: [spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.
if(x > 0){
^
bad.R:2:14: style: [brace_linter] There should be a space before an opening curly brace.
if(x > 0){
^
bad.R:2:14: style: [paren_body_linter] Put a space between a right parenthesis and a body expression.
if(x > 0){
^
bad.R:3:8: style: [indentation_linter] Indentation should be 4 spaces but is 8 spaces.
print("Positive")
~~~^
bad.R:4:4: style: [indentation_linter] Indentation should be 2 spaces but is 4 spaces.
}
~^
bad.R:5:5: style: [brace_linter] `else` should come on the same line as the previous `}`.
else {
^~~~
bad.R:6:8: style: [indentation_linter] Indentation should be 4 spaces but is 8 spaces.
print("Not positive")
~~~^
bad.R:7:4: style: [indentation_linter] Indentation should be 2 spaces but is 4 spaces.
}
~^
bad.R:8:5: style: [return_linter] Use implicit return behavior; explicit return() is not needed.
return(T)
^~~~~~
bad.R:8:13: style: [T_and_F_symbol_linter] Use TRUE instead of the symbol T.
return(T)
~^
bad.R:10:1: style: [trailing_blank_lines_linter] Remove trailing blank lines.
^
{lintr} 与 {styler} 包 互补,后者会自动重新格式化代码,从而消除 {lintr} 能够检测到的一些问题。
安装
从 CRAN 安装稳定版本:
install.packages("lintr")
或者来自 GitHub 的开发版本:
# install.packages("remotes")
remotes::install_github("r-lib/lintr")
用法
然后你可以创建一个配置文件并运行选定的 linter:
lintr::use_lintr(type = "tidyverse")
# in a project:
lintr::lint_dir()
# in a package:
lintr::lint_package()
要查看每个配置中包含的 linter 列表:
# tidyverse (default)
names(lintr::linters_with_defaults())
# full
names(lintr::all_linters())
配置 GitHub Actions
{usethis} 提供了用于生成 GitHub Actions 的 lint 工作流的辅助函数:
# in a project:
usethis::use_github_action("lint-project")
# in a package:
usethis::use_github_action("lint")
您还可以在持续集成过程中或在您的 IDE 或文本编辑器中运行 lintr。有关更多详细信息,请参阅 vignette("continuous-integration") 和 vignette("editors")。
在未进行进一步配置的情况下,这将运行 默认 linters。请参阅 vignette("lintr") 以了解如何修改这些默认设置。
行为准则
请注意,lintr 项目是附带 贡献者行为准则 发布的。通过为该项目做出贡献,您同意遵守其条款。