R-ecology-lesson icon indicating copy to clipboard operation
R-ecology-lesson copied to clipboard

See if styler could be used to automatically format content (and PRs)

Open fmichonneau opened this issue 7 years ago • 3 comments

https://github.com/r-lib/styler

it would reduce nitpicking on contributions while maintaining consistent formatting across the lesson

fmichonneau avatar Jan 31 '18 15:01 fmichonneau

Also, implementing this would support git-novice/09-conflict's advice to "use code style tools".

katrinleinweber avatar Nov 11 '19 16:11 katrinleinweber

I believe there is now a github action template for auto-styling pull requests: https://github.com/r-lib/actions/tree/master/examples#commands-workflow It can be added to the repository with usethis::use_github_action_pr_commands(open = TRUE)

Teebusch avatar Dec 03 '20 22:12 Teebusch

I've modified the workflow from the example a bit. I believe, we need something like this -- it automatically styles the .Rmd and .R files for each new PR. At least, that's what I think it does. I haven't tested it...

# github/workflows/format-r-code.yml

name: format-r-code
on: [pull_request]
jobs:
  style:
    runs-on: macOS-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v2
      - uses: r-lib/actions/pr-fetch@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - uses: r-lib/actions/setup-r@v1
      - name: Install dependencies
        run: Rscript -e 'install.packages("styler")'
      - name: Style
        run: Rscript -e 'styler::style_dir(filetype = c("R", "Rmd"))'
      - name: commit
        run: |
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Actions"
          git add \*.R
          git add \*.Rmd
          git commit -m 'Style'
      - uses: r-lib/actions/pr-push@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

(reference for style_dir() function)

Teebusch avatar Jul 08 '21 13:07 Teebusch