Skip to content

Commit

Permalink
Merge pull request #16 from ukgovdatascience/feature/pre-commit-r
Browse files Browse the repository at this point in the history
Feature: Pre-commit R
  • Loading branch information
ESKYoung authored Mar 4, 2021
2 parents bd1bb81 + ca33560 commit 42436ca
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Here's a few changes you **must** do once you've created your new project:

- Set up a Python virtual environment — there are **many** ways to [set up a virtual environment][pluralsight], so
we'll let you decide what's best for you!
- Git is not set up by default — open your terminal, navigate to your new project, run `git init` to set it up
- Git is not set up by default — open your terminal, navigate to your new project, run `git init` in your shell to set it up.
- Install the packages necessary by running `make requirements` in your shell.

## Changes to consider post-creation

Expand Down
4 changes: 3 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
"project_name": "Your new project name",
"repo_name": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",
"overview": "Brief overview of your project.",
"project_version": "0.0.1"
"project_version": "0.0.1",

"add_r_precommit_hooks": ["No", "Yes"]
}
1 change: 1 addition & 0 deletions {{ cookiecutter.repo_name }}/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source('startup.R')
4 changes: 4 additions & 0 deletions {{ cookiecutter.repo_name }}/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,7 @@ data/processed/*

# Ignore the `.secrets` file
.secrets

# Ignore r artifacts
*.Renviron
*.Rhistory
4 changes: 4 additions & 0 deletions {{ cookiecutter.repo_name }}/.lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters: with_defaults(
line_length_linter = line_length_linter(120),
commented_code_linter = NULL
)
18 changes: 18 additions & 0 deletions {{ cookiecutter.repo_name }}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,22 @@ repos:
- id: check-added-large-files
args: ["--maxkb=5120"]
- id: end-of-file-fixer
exclude: '\.Rd'
- id: trailing-whitespace
{% if cookiecutter.add_r_precommit_hooks == "Yes" %}
# R specific hooks: https://github.com/lorenzwalthert/precommit
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.1.2
hooks:
- id: style-files
args: [--style_pkg=styler, --style_fun=tidyverse_style]
- id: roxygenize
# codemeta must be above use-tidy-description when both are used
# - id: codemeta-description-updated
- id: use-tidy-description
- id: lintr
exclude: renv/activate.R
- id: readme-rmd-rendered
- id: parsable-R
- id: no-browser-statement
{% endif -%}
20 changes: 20 additions & 0 deletions {{ cookiecutter.repo_name }}/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Package: {{ cookiecutter.repo_name }}
Title: {{ cookiecutter.project_name }}
Version: {{ cookiecutter.project_version }}
Authors@R:
person(given = "{{ cookiecutter.organisation_name }}",
role = c("aut", "cre"),
email = "{{ cookiecutter.contact_email }}")
Description: {{ cookiecutter.overview }}
License: MIT + file LICENSE
Imports:
docopt,
git2r,
lintr,
styler,
usethis
Encoding: UTF-8
Language: en-GB
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.0.0
6 changes: 5 additions & 1 deletion {{ cookiecutter.repo_name }}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

## Getting started

To be added.
To start contributing to `{{ cookiecutter.repo_name }}`, run the following commands in your shell:

``` shell script
make requirements
```

### Requirements

Expand Down
15 changes: 14 additions & 1 deletion {{ cookiecutter.repo_name }}/docs/structure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ coverage run -m pytest
coverage html
```

a code coverage report in HTML will be produced on the code in the `src` folder. This HTMl report can be accessed at
a code coverage report in HTML will be produced on the code in the `src` folder. This HTML report can be accessed at
`htmlcov/index.html`.

### `.envrc`
Expand Down Expand Up @@ -137,6 +137,19 @@ Alternatively, to install the requirements file along with pre-commit hooks, run
make requirements
```

### `DESCRIPTION`
R-specific. Information related to the project including the name, authors and packages necessary for the project.

### `startup.R`
R-specific. Installs necessary packages specified in the `DESCRIPTION` file upon starting R via `.Rprofile`.

### `.Rprofile`
R-specific. Initialisation file that runs automatically when starting R.

### `.lintr`
R-specific. Configuration file for styling R code that's used by pre-commit hooks to check R code.


[code-of-conduct]:../contributor_guide/CODE_OF_CONDUCT.md
[contributing]: ../contributor_guide/CONTRIBUTING.md
[coverage]: https://coverage.readthedocs.io/
Expand Down
7 changes: 7 additions & 0 deletions {{ cookiecutter.repo_name }}/startup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if (length(grep(pattern = "devtools", x = utils::installed.packages()[, 1])) == 0) {
Sys.setenv(R_PROFILE_USER = "/dev/null")
utils::install.packages(pkgs = "devtools", Ncpus = 1, repos = "https://cran.ma.imperial.ac.uk/")
# install packages from DESCRIPTION file
devtools::install()
Sys.unsetenv("R_PROFILE_USER")
}

0 comments on commit 42436ca

Please sign in to comment.