actions icon indicating copy to clipboard operation
actions copied to clipboard

Create a workflow to render website with Rmarkdown

Open berthetclement opened this issue 9 months ago • 3 comments

Hello,

I'd like to create a static page or web site from an R project.

Currently I have a project that I started with Rstudio (New Projet->Create a simple R Markdown website).

My problem is that I'm forced to push my site on my github repos for the moment (the site is in docs/ and github deploits correctly from docs/ on master).

Is it possible with git to push on a gh-pages branch only the content of the site (the content of “_site” by default or docs/ for me)?

This would allow me to have a repository with the source code only.

Another question, I haven't tried it yet but I'd like to create a CI/CD that performs a rmarkdown::render_site() and deploys on gh-pages a bit like usethis::use_github_action(“pkgdown”).

Is this possible?

If you have any info on best practices with Rmarkdown simple website for optimal deployment, I'd love to hear from you.

I'll have a solution if what I'm asking for isn't suitable or intended for this. I can also transform my R project into a package and simply use {pkgdown} and deploy with usethis::use_github_action(“pkgdown”).

Thanks a lot!

berthetclement avatar Feb 14 '25 17:02 berthetclement

It is challenging to maintain a lot of workflows that are untested or lightly tested.

Nevertheless, I would be happy to review a PR for such a workflow.

I think it will be very close to https://github.com/r-lib/actions/blob/v2-branch/examples/bookdown.yaml or the other bookdown workflow, but instead of calling render_book(), you'd call rmarkdown::render().

I would probably use pak instead of renv for a new workflow, but YMMV.

gaborcsardi avatar Feb 19 '25 08:02 gaborcsardi

Thank you for your feedback, it's much appreciated.

Indeed, I have a workflow very similar to https://github.com/r-lib/actions/blob/v2-branch/examples/bookdown.yaml.

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples/blogdown.yaml
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  push:
    branches: [main, master]
  pull_request:
  workflow_dispatch:
  
name: render_site.yaml

permissions: read-all

jobs:
  renderrmarkdown:
    runs-on: ubuntu-latest
    # Only restrict concurrency for non-PR jobs
    concurrency:
      group: renderrmarkdown-${{ github.event_name != 'pull_request' || github.run_id }}
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - uses: r-lib/actions/setup-pandoc@v2

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

      - uses: r-lib/actions/setup-renv@v2

      - name: Build site
        run: rmarkdown::render_site()
        shell: Rscript {0}

      - name: Deploy to GitHub pages 🚀
        if: github.event_name != 'pull_request'
        uses: JamesIves/[email protected]
        with:
          clean: false
          branch: gh-pages
          folder: docs

But unfortunately I have a problem (with Pandoc) when I build the site with github actions. Currently, i only try with renv to use r-lib/actions/setup-renv@v2.

My issue :

Run rmarkdown::render_site()
  rmarkdown::render_site()
  shell: /usr/local/bin/Rscript {0}
  env:
    GITHUB_PAT: ***
    R_LIBS_USER: /home/runner/work/_temp/Library
    TZ: UTC
    _R_CHECK_SYSTEM_CLOCK_: FALSE
    NOT_CRAN: true
    RSPM: https://packagemanager.posit.co/cran/__linux__/noble/latest
    RENV_CONFIG_REPOS_OVERRIDE: https://packagemanager.posit.co/cran/__linux__/noble/latest
    RENV_PATHS_ROOT: /home/runner/work/_temp/renv
- The project is out-of-sync -- use `renv::status()` for details.

Rendering: ./dashboard.Rmd
1/11                  
[2](https://github.com/rte-antares-rpackage/rPackagesRte/actions/runs/13416350141/job/37478027901#step:6:2)/11 [setup]          
3/11                  
4/11 [unnamed-chunk-1]
5/11                  
6/11 [unnamed-chunk-2]
7/11                  
8/11 [unnamed-chunk-[3](https://github.com/rte-antares-rpackage/rPackagesRte/actions/runs/13416350141/job/37478027901#step:6:3)]
9/11                  
10/11 [unnamed-chunk-[4](https://github.com/rte-antares-rpackage/rPackagesRte/actions/runs/13416350141/job/37478027901#step:6:4)]
Error in `format$format == "pandoc" && nzchar(format$caption)`:
! 'length = 6' in coercion to 'logical(1)'
Backtrace:
  1. rmarkdown::render_site()
  2. generator$render(...)
  3. base::sapply(...)
  4. base::lapply(X = X, FUN = FUN, ...)
  [5](https://github.com/rte-antares-rpackage/rPackagesRte/actions/runs/13416350141/job/37478027901#step:6:5). rmarkdown (local) FUN(X[[i]], ...)
     ...
 27. knitr (local) value_fun(ev$value, ev$visible)
 2[8](https://github.com/rte-antares-rpackage/rPackagesRte/actions/runs/13416350141/job/37478027901#step:6:8). knitr (local) fun(x, options = options)
 31. formattable:::knit_print.formattable(x, ...)
 33. formattable:::knit_print_formattable.data.frame(x, ...)
 34. base::isTRUE(format$format == "pandoc" && nzchar(format$caption))
Execution halted
Error: Process completed with exit code 1.

I can build the site locally without any problem (using renv or not):

sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.utf8  LC_CTYPE=French_France.utf8    LC_MONETARY=French_France.utf8 LC_NUMERIC=C                  
[5] LC_TIME=French_France.utf8    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] yaml_2.3.8          shiny_1.8.1.1       htmltools_0.5.8.1   gfonts_0.2.0        particlesjs_0.1.1   shufflecards_0.0.4 
 [7] jsonlite_1.8.8      httr_1.4.7          gh_1.4.1            formattable_0.2.1   flexdashboard_0.6.2

loaded via a namespace (and not attached):
 [1] xfun_0.43         httr2_1.0.1       bslib_0.7.0       purrr_1.0.2       colorspace_2.1-0  vctrs_0.6.5      
 [7] utf8_1.2.4        rlang_1.1.4       jquerylib_0.1.4   pillar_1.9.0      later_1.3.2       httpcode_0.3.0   
[13] glue_1.7.0        rappdirs_0.3.3    uuid_1.2-0        lifecycle_1.0.4   munsell_0.5.1     gtable_0.3.5     
[19] fontawesome_0.5.2 htmlwidgets_1.6.4 memoise_2.0.1     evaluate_0.23     knitr_1.46        fastmap_1.1.1    
[25] httpuv_1.6.15     crosstalk_1.2.1   curl_6.1.0        fansi_1.0.6       ggiraph_0.8.12    Rcpp_1.0.12      
[31] xtable_1.8-4      renv_1.0.11       scales_1.3.0      promises_1.3.0    cachem_1.0.8      mime_0.12        
[37] systemfonts_1.0.6 ggplot2_3.5.1     digest_0.6.35     grid_4.2.2        cli_3.6.2         tools_4.2.2      
[43] magrittr_2.0.3    sass_0.4.9        tibble_3.2.1      crul_1.4.2        crayon_1.5.2      pkgconfig_2.0.3  
[49] rmarkdown_2.29    rstudioapi_0.16.0 R6_2.5.1          gitcreds_0.1.2    compiler_4.2.2   

I think that for this to work, I'd turn my project into a package and use https://github.com/r-lib/actions/blob/v2-branch/examples/pkgdown.yaml or maybe test quarto ....

In any case, I publish my static site from the /docs master but it's not satisfactory.

Thanks for your feedback!

berthetclement avatar Feb 20 '25 09:02 berthetclement

This is coming from your code in the Rmd, so I suggest you try something very simple first, just to get the tooling right, and then use the full Rmd.

It might be easier to debug this interactively, one action that can help you with that is https://github.com/r-hub/actions/tree/main/debug-shell

gaborcsardi avatar Feb 20 '25 09:02 gaborcsardi