Merge branch 'transform-R-notebooks-to-Rmd' of https://github.com/Cau… #18
Workflow file for this run
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: Convert and Lint R Notebooks | |
on: | |
push | |
concurrency: | |
group: convert-lint-notebooks | |
cancel-in-progress: true | |
jobs: | |
convert-lint-notebooks: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.8' # Specify your Python version here | |
- name: Install nbstripout | |
run: | | |
python -m pip install --upgrade pip | |
pip install nbstripout | |
- name: Set up R | |
uses: r-lib/actions/setup-r@v2 | |
- name: Install rmarkdown, knitr, and lintr packages | |
run: | | |
R -e 'install.packages(c("rmarkdown", "knitr", "lintr", "xfun"), repos="https://cloud.r-project.org")' | |
- name: Strip outputs from .irnb files | |
run: | | |
for notebook in PM1/*.irnb; do | |
ipynb_notebook="${notebook%.irnb}.ipynb" | |
mv "$notebook" "$ipynb_notebook" | |
nbstripout "$ipynb_notebook" | |
mv "$ipynb_notebook" "$notebook" | |
done | |
- name: Convert .irnb to .Rmd and .R | |
run: | | |
R -e ' | |
files <- list.files(path = "PM1", pattern = "\\.irnb$", full.names = TRUE, recursive = FALSE) | |
lapply(files, function(input) { | |
rmarkdown::convert_ipynb(input) | |
rmd_file <- xfun::with_ext(input, "Rmd") | |
knitr::purl(rmd_file, output = xfun::with_ext(input, "R")) | |
}) | |
' | |
- name: Lint .Rmd files | |
id: lint | |
run: | | |
R -e ' | |
library(lintr) | |
linters <- with_defaults(line_length_linter = line_length_linter(120)) | |
rmd_files <- list.files(path = "PM1", pattern = "\\.Rmd$", full.names = TRUE) | |
results <- lapply(rmd_files, function(file) { | |
lints <- lint(file, linters) | |
if (length(lints) > 0) { | |
cat("Warnings found during linting:\n") | |
print(lints) | |
# stop("Linting failed with warnings") | |
} | |
}) | |
' | |
- name: Execute R scripts | |
id: execute | |
run: | | |
R -e ' | |
files <- list.files(path = "PM1", pattern = "\\.R$", full.names = TRUE, recursive = FALSE) | |
for (file in files) { | |
tryCatch( | |
{ | |
source(file) | |
}, | |
error = function(e) { | |
cat("Error found in file:", file, "\n") | |
cat("Error message:", e$message, "\n") | |
# stop("Execution failed due to an error in ", file) | |
} | |
) | |
} | |
' | |
- name: Zip .R files | |
run: | | |
mkdir r_scripts | |
mv PM1/*.R r_scripts/ | |
zip -r r_scripts.zip r_scripts | |
- name: Upload artifact | |
uses: actions/upload-artifact@v2 | |
with: | |
name: r-scripts | |
path: r_scripts.zip | |
- name: Delete .R files and zip | |
run: | | |
rm -rf r_scripts | |
rm r_scripts.zip | |
- name: Commit and push stripped .irnb and .Rmd files | |
run: | | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git add PM1/*.irnb PM1/*.Rmd | |
git commit -m 'Strip outputs from .irnb, convert to .Rmd, lint .Rmd files, and execute .R files' | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |