Run R Script #15
This file contains 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
name: Run R Script | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Runs daily at midnight | |
workflow_dispatch: | |
jobs: | |
run-r-script: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Install deps | |
run: sudo apt install libcurl4-openssl-dev | |
- name: Set up R | |
uses: r-lib/actions/setup-r@v2 | |
with: | |
r-version: '4.1.0' # Specify the R version you need | |
- name: Set RENV_PATHS_ROOT | |
shell: bash | |
run: | | |
echo "RENV_PATHS_ROOT=${{ runner.temp }}/renv" >> $GITHUB_ENV | |
- name: Install and activate renv | |
run: | | |
install.packages("renv") | |
renv::activate() | |
shell: Rscript {0} | |
- name: Get R and OS version | |
run: | | |
cat(sprintf("OS_VERSION=%s\n", sessionInfo()$running), append=TRUE, file=Sys.getenv("GITHUB_ENV")); | |
cat(sprintf("R_VERSION=%s\n", R.Version()$version.string), append=TRUE, file=Sys.getenv("GITHUB_ENV")); | |
shell: Rscript {0} | |
- name: Restore Renv package cache | |
uses: actions/cache@v4 | |
with: | |
path: ${{ env.RENV_PATHS_ROOT }} | |
key: ${{ env.OS_VERSION }}-${{ env.R_VERSION }}-${{ inputs.cache-version }}-${{ hashFiles('renv.lock') }} | |
restore-keys: ${{ env.OS_VERSION }}-${{ env.R_VERSION }}-${{ inputs.cache-version }}- | |
- name: Install R packages if not cached | |
# Rscript -e 'packages <- c("data.table", "httr", "ggplot2", "jsonlite"); installed <- rownames(installed.packages()); to_install <- setdiff(packages, installed); if (length(to_install) > 0) install.packages(to_install, repos="https://cloud.r-project.org")' | |
run: | | |
install.packages(c("httr", "ggplot2", "jsonlite", "data.table")); | |
shell: Rscript {0} | |
- name: Cache data files | |
uses: actions/cache@v4 | |
with: | |
path: | | |
demanda_diaria_Edesur.txt | |
demanda_diaria_GBA.txt | |
key: ${{ runner.os }}-data | |
# key: ${{ runner.os }}-data-${{ hashFiles('demanda_diaria_Edesur.txt', 'demanda_diaria_GBA.txt') }} | |
restore-keys: ${{ runner.os }}-data | |
- name: Run R script | |
run: | | |
Rscript demanda_diaria_API_MEM.R | |
- name: Cache updated data files | |
uses: actions/cache/save@v4 | |
with: | |
path: | | |
demanda_diaria_Edesur.txt | |
demanda_diaria_GBA.txt | |
# key: ${{ runner.os }}-data-${{ hashFiles('demanda_diaria_Edesur.txt', 'demanda_diaria_GBA.txt') }} | |
key: ${{ runner.os }}-data | |
- name: Save plot | |
run: | | |
datetime=$(date -u +"%Y%m%dT%H%M%SZ") | |
mv demanda_diaria.svg demanda_diaria_${datetime}.svg | |
echo "datetime=${datetime}" >> $GITHUB_ENV | |
echo "Plot saved as demanda_diaria_${datetime}.svg" | |
- name: Upload plot as artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ github.run_id }}_demanda_diaria_${{ env.datetime }} | |
path: demanda_diaria_${{ env.datetime }}.svg | |