You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I read in #452 that git integration is not a priority, but maybe a small hook that just styles the files that one has git added / staged would be a minimal, viable step into that direction. Maybe as a contribution to pre-commit.com?
Do you think it would be possible to shell-script styler in that way? I played around a bit, but ran into problems.
Reacted by bernie gray, Jack Wasey and Lorenz Walthert
I read in #452 that git integration is not a priority
Thanks for considering old issues. I did not mean I am strictly against it, but I at least won't develop it myself. If I understand correctly, the git hock would not be implemented in the source code of styler, right? I think it's a cool thing. I am not sure but I believe you could simply defer the path expansion to R?
It is unfortunate that pre-commit.com does not support R because the above script might have problems due portability across operation systems. Are you on Windows? I can try the script on my Mac. Maybe we can add support for R to pre-commit.com too, although that might be more work?
If you mean a bash-like command when you say executable, the answer is no. I don't know of any R package that does so to be honest. In almost all cases, people execute tools written in R in the R console (e.g. when using RStudio) or they invoke R from the command line with Rscript -e "styler::style_file(...)".
Pre-commits seem better placed somewhere like usethis, which contains development tools that wrap styler. Plus, that package already sets up a user's git for a project.
A less invasive approach would be simply to report whether files would be styled, thus preventing a commit. Since it is possible that styler could generate invalid code, at least some people would prefer a message, like codemeta gives, instead of doing it for you. It is also a slow process for a bigger package, although checking whether styling is needed is probably just as slow as actually performing it, until there is some caching in styler.
@jackwasey indeed that would be less invasive. Indeed a style_pkg() call as a pre-commit hook would be is quite slow currently, we have implemented neither multi-processing and caching. Please refer to #370 and #491 and #320.
Edit:* Of course, we'd only style the files changed, not the whole package, as drafted above.
Not sure, I think it's a bit hacky 😄. But thanks for looking into it @asottile. Yes, I was wondering about using language: script but I could not easily find an example so I just decided to go for language: python for a first pass. So if we choose to use language: script, how would that affect my repo structure? Could my repo at https://github.com/lorenzwalthert/styler_precommit then just contain a single script run-r-styler (with shebang, as you suggested above) instead of a whole package? Because I don't fully understand this part of the documentation and how this relates to your example above:
Script hooks provide a way to write simple scripts which validate files. The entry should be a path relative to the root of the hook repository.
Maybe then I don't even have to use python in my code because with R, we can use Rscript with shebang, so could I have
# !/usr/bin/env Rscript
# R code only
Correct?
Since you are in this thread, are there any recommendations regarding naming conventions of repositories and hook ids you'd suggest to use for styler so we can make it match conventions of pre-commit for other linters and style fixers? I saw there is autopep8 and flake support, so maybe check out those?
An advantage of using the python package could be that we'd be able to expose a command line executable for styler that could be used independently of pre-commit, which is not possible with an R package. Related: #494.
Maybe then I don't even have to use python in my code because with R, we can use Rscript with shebang, so could I have
# !/usr/bin/env Rscript
# R code only
Correct?
Yep! That would be even simpler :D
Since you are in this thread, are there any recommendations regarding naming conventions of repositories and hook ids you'd suggest to use for styler so we can make it match conventions of pre-commit for other linters and style fixers? I saw there is autopep8 and flake support, so maybe check out those?
There isn't currently any sort of naming convention -- though they can be added to our list of repositories on the website via this -- ends up here.
It's ~generally preferred to have it in the actual repo of the tool -- but in this case it's probably better separated since the versioning won't properly lock (which is what is ~mostly expected of other repos)
An advantage of using the python package could be that we'd be able to expose a command line executable for styler that could be used independently of pre-commit, which is not possible with an R package
Ah neat, hadn't even thought about that -- it's really surprising to me that R packages can't have commandline tools :S
If I had noticed any clearly relevant one, I would have written so initially ;-) "It's in Ruby" could be considered an advantage, but since academia choose Python and most R folks are more familiar with that, I didn't consider it relevant. Just a note for completeness sake.
@asottile I think when using language: script, another disadvantage is that the user has to manually make the script executable and the directory where it lays is something like ~/.cache/pre-commit/repof4lOTz/bin/executable, so it's hard to even give the user clear instructions when they are not experienced command line users. At least in my case that seemed necessary. Any workaround for this?
Solution: Just make the file executable and commit because the permission is an attribute of the file.
In case you were using the hook: There was a bug in styler (#521) that made it impossible to run the hook correctly on R files in different directories. It is fixed in #522 so if you want this to work correctly, get the dev version of styler.
Activity
katrinleinweber commentedon Jan 28, 2019
yields
Error in dirname(path) : object 'R' not found. Probably, because the files are of course in anR/subdirectory.lorenzwalthert commentedon Jan 29, 2019
Hi @katrinleinweber, thanks.
Thanks for considering old issues. I did not mean I am strictly against it, but I at least won't develop it myself. If I understand correctly, the git hock would not be implemented in the source code of styler, right? I think it's a cool thing. I am not sure but I believe you could simply defer the path expansion to R?
It is unfortunate that pre-commit.com does not support R because the above script might have problems due portability across operation systems. Are you on Windows? I can try the script on my Mac. Maybe we can add support for R to pre-commit.com too, although that might be more work?
katrinleinweber commentedon Jan 29, 2019
Thanks for contextualising the idea! Yes, that would certainly be more work.
I guessed so (over in pre-commit/pre-commit-hooks), but pre-commit's contributing docu says: "get the .pre-commit-hooks.yaml files added to popular linters". I guess that means styler as well ;-) OTOH, those files are present in so many more, normal projects.
Hm... I'll inquire about the process of adding R support on their end.
katrinleinweber commentedon Jan 29, 2019
Apparently, they need an
r.pyfile that acts as a module handling the hook installation & run within their Python package.asottile commentedon Jan 29, 2019
Exciting! Yeah the work to make this happen would be in two parts:
ras a language inpre-commit.pre-commit-hooks.yamlfile in this repositoryDoes
stylerprovide an executable when installed? and/or what's the usual interface that people run tools written in R?lorenzwalthert commentedon Feb 16, 2019
If you mean a bash-like command when you say executable, the answer is no. I don't know of any R package that does so to be honest. In almost all cases, people execute tools written in R in the R console (e.g. when using RStudio) or they invoke R from the command line with
Rscript -e "styler::style_file(...)".lorenzwalthert commentedon Feb 16, 2019
References
michaelquinn32 commentedon Feb 17, 2019
Pre-commits seem better placed somewhere like usethis, which contains development tools that wrap styler. Plus, that package already sets up a user's git for a project.
bfgray3 commentedon Apr 3, 2019
I'd like to add my vote in support of a pre-commit hook.
jackwasey commentedon Apr 12, 2019
A less invasive approach would be simply to report whether files would be styled, thus preventing a commit. Since it is possible that styler could generate invalid code, at least some people would prefer a message, like
codemetagives, instead of doing it for you. It is also a slow process for a bigger package, although checking whether styling is needed is probably just as slow as actually performing it, until there is some caching instyler.lorenzwalthert commentedon Apr 12, 2019
Also note this reference: #452.
lorenzwalthert commentedon Apr 12, 2019
@jackwasey indeed that would be less invasive. Indeed a
style_pkg()call as a pre-commit hook would be is quite slow currently, we have implemented neither multi-processing and caching. Please refer to #370 and #491 and #320.Edit:* Of course, we'd only style the files changed, not the whole package, as drafted above.
6 remaining items
lorenzwalthert commentedon May 12, 2019
Not sure, I think it's a bit hacky 😄. But thanks for looking into it @asottile. Yes, I was wondering about using
language: scriptbut I could not easily find an example so I just decided to go forlanguage: pythonfor a first pass. So if we choose to uselanguage: script, how would that affect my repo structure? Could my repo at https://github.com/lorenzwalthert/styler_precommit then just contain a single scriptrun-r-styler(with shebang, as you suggested above) instead of a whole package? Because I don't fully understand this part of the documentation and how this relates to your example above:Maybe then I don't even have to use python in my code because with R, we can use
Rscriptwith shebang, so could I haveCorrect?
Since you are in this thread, are there any recommendations regarding naming conventions of repositories and hook ids you'd suggest to use for styler so we can make it match conventions of pre-commit for other linters and style fixers? I saw there is autopep8 and flake support, so maybe check out those?
An advantage of using the python package could be that we'd be able to expose a command line executable for styler that could be used independently of pre-commit, which is not possible with an R package. Related: #494.
asottile commentedon May 12, 2019
Yep! That would be even simpler :D
There isn't currently any sort of naming convention -- though they can be added to our list of repositories on the website via this -- ends up here.
It's ~generally preferred to have it in the actual repo of the tool -- but in this case it's probably better separated since the versioning won't properly lock (which is what is ~mostly expected of other repos)
Ah neat, hadn't even thought about that -- it's really surprising to me that R packages can't have commandline tools :S
katrinleinweber commentedon May 12, 2019
1k thanks for getting this up and running :-) I followed #467 (comment) just now and it worked fine and committed the style-modifications.
If I had noticed any clearly relevant one, I would have written so initially ;-) "It's in Ruby" could be considered an advantage, but since academia choose Python and most R folks are more familiar with that, I didn't consider it relevant. Just a note for completeness sake.
lorenzwalthert commentedon May 12, 2019
@asottile I think when usinglanguage: script, another disadvantage is that the user has to manually make the script executable and the directory where it lays is something like~/.cache/pre-commit/repof4lOTz/bin/executable, so it's hard to even give the user clear instructions when they are not experienced command line users. At least in my case that seemed necessary. Any workaround for this?Solution: Just make the file executable and commit because the permission is an attribute of the file.
asottile commentedon May 12, 2019
You should be able to check it in executable
chmod +x script && git add scriptlorenzwalthert commentedon May 21, 2019
Summary:
Put a
.pre-commit-config.yamlin your repo root with this content:And you are all set. There is another hook for
devtools::document()and you are welcomed to contribute others. See https://github.com/lorenzwalthert/pre-commit-hooks.Edit: For this to work correctly in all cases (see below), you need the dev version of styler or the CRAN release after version 1.1.1.
lorenzwalthert commentedon May 21, 2019
Need to tweet / communicate this before closing.
katrinleinweber commentedon May 22, 2019
Thank you for building this @lorenzwalthert & @asottile :-)
asottile commentedon May 22, 2019
no problem! happy to help :D -- @lorenzwalthert did most of the heavy lifting to make it happen -- I just assisted :)
lorenzwalthert commentedon Jun 19, 2019
In case you were using the hook: There was a bug in styler (#521) that made it impossible to run the hook correctly on R files in different directories. It is fixed in #522 so if you want this to work correctly, get the dev version of styler.