Add rpy2 to the dependencies

- add a Jupyter notebook that allows to install all project-external
  dependencies regarding R and R packages
- adjust the GitHub Action workflow to also install R and the R packages
  used within the project
- add a `init_r` module that initializes all R packages globally
  once the `urban_meal_delivery` package is imported
This commit is contained in:
Alexander Hess 2021-01-11 12:24:15 +01:00
commit b0f2fdde10
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
10 changed files with 2152 additions and 52 deletions

View file

@ -69,6 +69,8 @@ class Config:
ALEMBIC_TABLE = 'alembic_version'
ALEMBIC_TABLE_SCHEMA = 'public'
R_LIBS_PATH = os.getenv('R_LIBS')
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<configuration>'
@ -117,6 +119,12 @@ def make_config(env: str = 'production') -> Config:
if config.DATABASE_URI is None and not os.getenv('TESTING'):
warnings.warn('Bad configurartion: no DATABASE_URI set in the environment')
# Some functionalities require R and some packages installed.
# To ensure isolation and reproducibility, the projects keeps the R dependencies
# in a project-local folder that must be set in the environment.
if config.R_LIBS_PATH is None and not os.getenv('TESTING'):
warnings.warn('Bad configuration: no R_LIBS set in the environment')
return config

View file

@ -0,0 +1,28 @@
"""Initialize the R dependencies.
The purpose of this module is to import all the R packages that are installed
into a sub-folder (see `config.R_LIBS_PATH`) in the project's root directory.
The Jupyter notebook "research/r_dependencies.ipynb" can be used to install all
R dependencies on a Ubuntu/Debian based system.
"""
from rpy2.rinterface_lib import callbacks as rcallbacks
from rpy2.robjects import packages as rpackages
# Suppress R's messages to stdout and stderr.
# Source: https://stackoverflow.com/a/63220287
rcallbacks.consolewrite_print = lambda msg: None # pragma: no cover
rcallbacks.consolewrite_warnerror = lambda msg: None # pragma: no cover
# For clarity and convenience, re-raise the error that results from missing R
# dependencies with clearer instructions as to how to deal with it.
try: # noqa:WPS229
rpackages.importr('forecast')
rpackages.importr('zoo')
except rpackages.PackageNotInstalledError: # pragma: no cover
msg = 'See the "research/r_dependencies.ipynb" notebook!'
raise rpackages.PackageNotInstalledError(msg) from None