This is a demonstration of the implementation of CodeCarbon (python) in R-based workflows to measure the energy consumption of individual snippets of code, scripts, and rendered documents (i.e., Rmd/qmd). This work is an extension to the pilot Green Digital Skills workshop by the Netherlands eScience center.
The suggested use of this repository starts with making sure that R, RStudio, and python are installed in your computer:
Install R and RStudio on your computer if you haven't done so. Note that these demonstrations were conducted under R version 4.4.0 and RStudio 2024.04.0.
Detailed instructions are provided here.
There are several ways to download the necessary material:
-
The simplest way is to download the ZIP file, unpack it, and place it in a familiar folder in your computer.
-
GitHub desktop is also a good option to copy this repository by following these instructions.
You should now have all the necessary files in your computer with an identical folder structure (described in the following section).
In the main directory, open the file named code-carbon-r.Rproj in RStudio. This will open RStudio in the default working directory of the project. You can now navigate through the folders on the right-bottom panel of R Studio.
Open the R folder. You should now see a file named code-carbon-Quarto.qmd (markdown document with embedded R and python code). Open this file to follow a demonstration of how to use CodeCarbon in R-based workflows. Individual R scripts are found in the R folder, which are sourced into the main document.
There is a sample R script you can reuse and adapt to test your code. Open the file code-carbon-script.R in the R folder. You may download, adapt and re-use this script.
The project structure distinguishes three kinds of folders:
- read-only (RO): not edited by either code or researcher
- human-writeable (HW): edited by the researcher only.
- project-generated (PG): folders generated when running the code; these folders can be deleted or emptied and will be completely reconstituted as the project is run.
.
├── .gitignore
├── CITATION.cff
├── LICENSE
├── README.md
├── code-carbon-r.Rproj
└── R <- Source code for this project (HW)
You can contact me or post a request in this repository in case you encounter any issues.
This project is licensed under the terms of the MIT License.
This project structure repository is adapted from the Utrecht University simple R project template, which builds upon the Good Enough Project Cookiecutter template by Barbara Vreede (2019).