Data Availability Plot #21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GitHub Actions workflow to generate a weekly data availability plot. | |
| # The plot shows which dates have origin-destination data available | |
| # and is pushed to the gh-pages branch so the README and pkgdown site can | |
| # reference a same-origin interactive HTML plot without rebuilding pkgdown. | |
| name: Data Availability Plot | |
| on: | |
| workflow_dispatch: # Allows manual triggering | |
| schedule: | |
| # Runs at 04:00 UTC on Saturday | |
| - cron: '0 4 * * 6' | |
| workflow_run: | |
| workflows: ["rostemplate-gh-pages"] | |
| types: [completed] | |
| jobs: | |
| generate-plot: | |
| runs-on: ubuntu-latest | |
| if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' | |
| permissions: | |
| contents: write | |
| env: | |
| GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: r-lib/actions/setup-r@v2 | |
| with: | |
| r-version: 'release' | |
| use-public-rspm: true | |
| - uses: r-lib/actions/setup-r-dependencies@v2 | |
| with: | |
| extra-packages: | | |
| local::. | |
| any::ggplot2 | |
| any::tidyr | |
| any::jsonlite | |
| any::ggiraph | |
| any::htmlwidgets | |
| needs: check | |
| - name: Generate Data Availability Plot | |
| run: | | |
| Rscript tools/generate_data_availability_plot.R data_availability.png | |
| shell: bash | |
| - name: Deploy Plot and Badges to gh-pages | |
| if: success() | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| test -f data_availability.png | |
| test -f data_availability.html | |
| test -f latest_v1.json | |
| test -f latest_v2.json | |
| # Save the generated image, interactive HTML, and JSON files | |
| cp data_availability.png /tmp/data_availability.png | |
| cp data_availability.html /tmp/data_availability.html | |
| cp latest_v1.json /tmp/latest_v1.json | |
| cp latest_v2.json /tmp/latest_v2.json | |
| git fetch origin gh-pages | |
| git checkout -B gh-pages origin/gh-pages | |
| # Copy files into a stable pkgdown-served directory. | |
| mkdir -p data-availability | |
| cp /tmp/data_availability.png data-availability/ | |
| cp /tmp/data_availability.html data-availability/ | |
| cp /tmp/latest_v1.json data-availability/ | |
| cp /tmp/latest_v2.json data-availability/ | |
| Rscript - <<'RS' | |
| index <- "index.html" | |
| if (file.exists(index)) { | |
| html <- paste(readLines(index, warn = FALSE), collapse = "\n") | |
| pattern <- paste0( | |
| '<div class="float">\\s*', | |
| '<img src="https://rOpenSpain\\.github\\.io/spanishoddata/data-availability/data_availability\\.png"[^>]*>', | |
| '\\s*<div class="figcaption">Dates for which origin-destination data is available</div>\\s*', | |
| '</div>' | |
| ) | |
| iframe <- paste0( | |
| '<iframe class="data-availability-plot" ', | |
| 'src="data-availability/data_availability.html" ', | |
| 'title="Dates for which origin-destination data is available" ', | |
| 'loading="lazy" scrolling="no" style="width:100%;aspect-ratio:10/6.2;height:auto !important;border:0;overflow:hidden;"></iframe>' | |
| ) | |
| patched <- sub(pattern, iframe, html, perl = TRUE) | |
| if (!identical(patched, html)) { | |
| writeLines(patched, index, useBytes = TRUE) | |
| } | |
| } | |
| RS | |
| git add --force index.html data-availability/data_availability.png data-availability/data_availability.html data-availability/latest_v1.json data-availability/latest_v2.json | |
| git commit -m "Update data availability plot and badges ($(date -u '+%Y-%m-%d'))" || echo "No changes to commit" | |
| git push origin gh-pages |