0

I have created a virtual environment with rvenv, so that I now have rvenv/ in my project root. I have also installed styler, and would like to have a pre-commit hook that will apply it to R code.

From here: https://github.com/lorenzwalthert/precommit/blob/master/.pre-commit-hooks.yaml is the following:

-   id: style-files
    name: style-files
    description: style files with styler
    args: [--style_pkg=styler, '--style_transformers=tidyverse_style(scope = "tokens")']
    entry: inst/bin/style-files
    language: script
    files: '(\.R|\.Rmd|\.Rnw|\.r|\.rmd|\.rnw)$'

I'm confused about the path that should be given to entry:, in this snip it's a path to a global version of styler (or at least, I'm assuming so). But I would like to be able to use the version that I have installed in the virtual environment I assume.

My question is - how to go about doing this. If I shouldn't be using the version installed in renv/ then I'm happy to hear (and use) whatever the best practice is around creating a pre-commit hook to style R files that will work on mine and others systems.

Edit

Following the answer below worked, I had to install docopt as well (as outlined here).

baxx
  • 3,956
  • 6
  • 37
  • 75

1 Answers1

3

Usually you rely on the remote repository to provide the configuration (such that you don't need all of the args / entry / etc. setup

for example if you want to use style-files from the repository you've listed you'd set this in your .pre-commit-config.yaml:

repos:
-   repo: https://github.com/lorenzwalthert/precommit
    rev: v0.1.2
    hooks: 
    -   id: style-files

from there you can customize args / etc.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thanks - I'm a little confused as I was under the impression that instead of the `precommit` repository I thought that I would be adding the `styler` repository? Having added the above hook I'm still not able to style an R file using precommit, I will update the OP to reflect this – baxx Sep 01 '20 at 22:43
  • @baxx the {precommit} hooks use the language `script`, see [pre-commit docs](https://pre-commit.com/#supported-languages). This type of hooks don't support virtual environments as written in the docs. In your case, the global R installation and packages are used. To use renv (properly), one needed to implement it as a supported language first, similar to how [conda was implemented](https://github.com/pre-commit/pre-commit/pull/1232). I am not sure virtual envs are shared between pre-commit and the project's source code, maybe Anthony can clarify. – Lorenz Walthert Sep 10 '20 at 12:26