R
Language-specific information about the way that SonarQube Cloud supports the analysis of R.
Analysis overview
Sonar analyzes R so the same quality gates and governance extend to the code behind your most consequential decisions. R analysis supports reliability and maintainability rules, code metrics, copy-paste detection, and secrets scanning.
82 R rules: reliability and maintainability rules in the Sonar way quality profile
Scripts and notebooks analyzed together:
.Rfiles and the R chunks inside R Markdown files are both analyzedDuplication detection: copy-paste detection across your R code
lintr import: import lintr findings alongside Sonar issues
covr coverage import: import test coverage reports emitted by covr
Get started: if the project is already analyzed by SonarQube Cloud, your R code appears in the next analysis.
What you get
Every analysis of R code produces:
Issues from the rules enabled in the Sonar way profile.
Standard metrics: lines of code, comment density, functions, classes, statements, and cognitive complexity.
Duplication blocks: DRY violations across your R code become a number you can track in a quality gate.
Syntax highlighting in the code viewer.
To see or change which rules run, go to Quality Profiles > R > Sonar way. To customize, copy the profile rather than editing it, then assign the copy to your R projects.
Embedded R code
In R Markdown files, the analyzer considers R code chunks as embedded R code. A code chunk starts with ```{r} or ```{r, opts}, where opts represents optional chunk options.
All R code chunks in a single R Markdown file are analyzed as one unit because code chunks share context by default. For example, if the same string literal is duplicated in multiple chunks in the same file, SonarQube Cloud reports that duplication.
Some R rules are not run on embedded R code because they are not appropriate for R Markdown files or would produce excessive noise.
Syntax errors in the Markdown portion of an R Markdown file are reported as R parsing errors.
Importing external issues from lintr
You can import issue reports from the R linting tool lintr using the sonar.r.lintr.reportPaths analysis property.
This property expects JSON reports in GitLab's Code Quality report format. Generate compatible reports with lintr's gitlab_output output function.
The file paths in the reports must be relative to the directory configured with the sonar.sources property. For configuration details, see External analyzer reports.
Importing test coverage reports
You can import R test coverage reports in either of the following formats:
Cobertura format: Set the
sonar.r.cobertura.reportPathsproperty.Generic test coverage format: Set the
sonar.coverageReportPathsproperty. See Generic test data.
The widely used R coverage tool covr supports both SonarQube's generic coverage format and the Cobertura format. See R for R coverage-property details.
Troubleshooting
R shows zero lines of code
If Measures > Size shows no R row at all, your files are not being recognized as R. Check that the extensions are .R, .r, or .Rmd, then go to Administration > General Settings > Analysis Scope and confirm that sonar.exclusions does not match your R paths.
If Measures > Size shows R with 0 lines, the files were found but contain no analyzable code. Confirm that you are looking at the right branch.
A rule fires in your .R file but not in your .Rmd chunk
Rules about file or script structure are disabled for embedded R. Rules that would be noisy or meaningless inside a notebook chunk are excluded from embedded R analysis by design.
A parsing error points at prose, not code, in your .Rmd
Syntax errors in the Markdown portion of an R Markdown file surface as R parsing errors. Fix the malformed Markdown, usually an unterminated code fence, and re-analyze.
Last updated
Was this helpful?

