Skip to content
24 changes: 24 additions & 0 deletions examples/README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Package workflows:
- [`lint`](#lint-workflow) - Run `lintr::lint_package()` on an R package.
- [`pr-commands`](#commands-workflow) - Adds `/document` and `/style` commands for pull requests.
- [`pkgdown`](#build-pkgdown-site) - Build a [pkgdown] site for an R package and deploy it to [GitHub Pages].
- [`document`](#document-package) - Run `roxygen2::roxygenise()` on an R package.
- [`style`](#style-package) - Run `styler::style_pkg()` on an R package.

RMarkdown workflows:

Expand Down Expand Up @@ -164,6 +166,28 @@ The inclusion of [`workflow_dispatch`](https://docs.github.com/en/actions/learn-
print_yaml("pkgdown.yaml")
```

## Document package

`usethis::use_github_action("document")`

This example documents an R package whenever a file in the `R/` directory changes, then commits and pushes the changes
to the same branch.

```{r echo = FALSE, results = "asis"}
print_yaml("document.yaml")
```

## Style package

`usethis::use_github_action("document")`

This example styles the R code in a package, then commits and pushes the changes
to the same branch.

```{r echo = FALSE, results = "asis"}
print_yaml("style.yaml")
```

## Build bookdown site

`usethis::use_github_action("bookdown")`
Expand Down
111 changes: 111 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Package workflows:
- [`pkgdown`](#build-pkgdown-site) - Build a
[pkgdown](https://pkgdown.r-lib.org/) site for an R package and
deploy it to [GitHub Pages](https://pages.github.com/).
- [`document`](#document-package) - Run `roxygen2::roxygenise()` on an
R package.
- [`style`](#style-package) - Run `styler::style_pkg()` on an R
package.

RMarkdown workflows:

Expand Down Expand Up @@ -525,6 +529,113 @@ jobs:
Rscript -e 'pkgdown::build_site(preview = FALSE, install = FALSE)'
```

## Document package

`usethis::use_github_action("document")`

This example documents an R package whenever a file in the `R/`
directory changes, then commits and pushes the changes to the same
branch.

``` yaml
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ["R/**"]
pull_request:
paths: ["R/**"]

name: Document

jobs:
document:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: Commit and push changes
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m "Update documentation" || echo "No changes to commit"
git pull --ff-only || echo "No remote changes"
git push origin || echo "No changes to commit"
```

## Style package

`usethis::use_github_action("document")`

This example styles the R code in a package, then commits and pushes the
changes to the same branch.

``` yaml
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ["R/**"]
pull_request:
paths: ["R/**"]

name: Style

jobs:
style:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::styler

- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: Commit and push changes
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add R/\*
git commit -m "Style code" || echo "No changes to commit"
git pull --ff-only || echo "No remote changes"
git push origin || echo "No changes to commit"
```

## Build bookdown site

`usethis::use_github_action("bookdown")`
Expand Down
44 changes: 44 additions & 0 deletions examples/document.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ["R/**"]
pull_request:
paths: ["R/**"]

name: Document

jobs:
document:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2
needs: roxygen2

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: Commit and push changes
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m "Update documentation" || echo "No changes to commit"
git pull --ff-only
git push origin
68 changes: 68 additions & 0 deletions examples/style.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
paths: ["R/**", "tests/**", "vignettes/**"]
pull_request:
paths: ["R/**", "tests/**", "vignettes/**"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These paths cover the majority of files but may exclude some given a non-traditional package structure. Would it be better to go by file type instead? For example:
paths: ["**.r", "**.R", "**.rmd", "**.Rmd", "**.rmarkdown", "**.Rmarkdown", "**.Rnw", "**.rnw"]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems more in line with what we actually want, true. Plus ".Rprofile". Also, I don't know exactly what your use case is but since you seem to already use pre-commit, you can also just run it in the cloud with https://pre-commit.ci (it's not free though for private repos I think) or GitHub Actions to enforce the style, as described in https://github.com/lorenzwalthert/precommit/blob/main/NEWS.md#precommit-v020. That would save us from trouble like

  • excluding files.
  • identical config for pre-commit hook and CI run, e.g. for scope, file filter etc.
  • identical environment (package versions etc.), since hooks with pre-commit.com use {renv} to specify hook dependencies.
  • no additional config file to maintain.
  • planned support for platforms other than GitHub to become more platform agnostic.
  • ...

Maybe we should update the docs of {precommit} to point out better that the CI integration can also be used without a local installation.

Anyways, for the cache, I just realised it's better to not use the hash of the commit as a key, because I don't think the cache mechanism will be able to pick up the latest SHA and use that cache. Instead, it will probably just fall back to some random cache key, or alphabetical order. Hence, I think it's better to make the cache date dependent, with fallbacks to yyyy-mm-dd, yyyy-mm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing up running pre-commit on the cloud. I had looked at that briefly but then decided to just run locally. I will certainly look into it! Happy to make doc suggestions as well if I get confused—pre-commit has been a super useful tool!

That seems more in line with what we actually want, true. Plus ".Rprofile"

No harm in including as well but I can't think of too many workflows where someone would push .Rprofile. I think the only time I have pushed is when using {renv} (which is then a single line of code)...

Anyways, for the cache, I just realised it's better to not use the hash of the commit as a key, because I don't think the cache mechanism will be able to pick up the latest SHA and use that cache. Instead, it will probably just fall back to some random cache key, or alphabetical order. Hence, I think it's better to make the cache date dependent, with fallbacks to yyyy-mm-dd, yyyy-mm.

Did a bit more digging into this. Github says here that:

If there are multiple partial matches for a restore key, the action returns the most recently created cache.

So leveraging the SHA should be fine because it can track the most recently created SHA.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So leveraging the SHA should be fine because it can track the most recently created SHA.

Thanks, if I was just capable of reading the docs 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha no worries 😃


name: Style

jobs:
style:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup R
uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Install dependencies
uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::styler
needs: styler

- name: Enable styler cache
run: styler::cache_activate()
shell: Rscript {0}

- name: Determine cache location
id: styler-location
run: |
cat(
"##[set-output name=location;]",
styler::cache_info(format = "tabular")$location,
"\n",
sep = ""
)
shell: Rscript {0}

- name: Cache styler
uses: actions/cache@v2
with:
path: ${{ steps.styler-location.outputs.location }}
key: ${{ runner.os }}-styler-${{ github.sha }}
restore-keys: |
${{ runner.os }}-styler-
${{ runner.os }}-

- name: Style
run: styler::style_pkg(filetype = c(".R", ".Rmd", ".Rmarkdown", ".Rnw"))
shell: Rscript {0}

- name: Commit and push changes
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add R/\*
git commit -m "Style code" || echo "No changes to commit"
git pull --ff-only
git push origin