Search code examples
rgithubcronyamlgithub-actions

How to add a devtools package to cron job in GitHub actions


Since the DatawRappr package is only available on Github...

devtools::install_github("munichrocker/DatawRappr")

how can one add this to a cron job? For example, this is what I have now:

   - name: Install packages
    uses: r-lib/actions/setup-r-dependencies@v2
    with:
      packages: |
        any::dplyr 
        any::janitor
        any::tidyr

and have tried things like

devtools::DatawRappr

or

  - name: Install DatawRappr package
    run: Rscript -e 'devtools::install_github("munichrocker/DatawRappr")'

  - name: Install other packages
    uses: r-lib/actions/setup-r-dependencies@v2
    with:
      packages: |
        any::dplyr 
        any::janitor
        any::tidyr

but i'm at a loss!


Solution

  • See this example repo: https://github.com/the-mad-statter/action-run-r-script

    With run-r-script.yaml:

    on:
      push:
        branches: main
    
    jobs:
      run-r-script:
        runs-on: ubuntu-latest
        steps:
          - name: Set up R
            uses: r-lib/actions/setup-r@v2
    
          - name: Install packages
            uses: r-lib/actions/setup-r-dependencies@v2
            with:
              packages: |
                any::dplyr 
                any::janitor
                any::tidyr
                
          - name: Install DatawRappr package
            run: |
              Rscript -e "pak::pkg_install('munichrocker/DatawRappr')"
    
          - name: Check out repository
            uses: actions/checkout@v4
    
          - name: Run script
            run: Rscript -e 'source("my_r_script.R")'
    
          - name: Commit results
            run: |
              git config --local user.email "[email protected]"
              git config --local user.name "GitHub Actions"
              git add output.txt
              git commit -m 'ran my_r_script.R' || echo "No changes to commit"
              git push origin || echo "No changes to commit"
    

    Remember to set the repository Settings -> Actions -> General -> Workflow permissions to "Read and write permissions" if you intend to have the workflow write back to the repo.