This project provides an end-to-end example: Dockerized R script that runs the NCI BCRA absolute.risk() (Gail Model), a FastAPI backend that calls the Docker container (or a local Rscript fallback), and a simple frontend served by FastAPI.
Files
gail_model.R— Rscript that callsBCRA::absolute.risk()and prints JSON.Dockerfile— Builds an image with R and the BCRA package; entrypoint runsgail_model.R.main.py— FastAPI application servingindex.htmland/predictendpoint which calls the R container or falls back to local Rscript.index.html— Simple frontend form and JS to POST to/predict.
Prerequisites
- Docker installed and running if you plan to use the Dockerized R image (recommended for isolation).
- Conda (miniconda/anaconda) to create the provided conda environment which also includes R.
- Python 3.8+ (the conda env provides Python)
Note: the conda environment now includes r-base so R is available inside the environment. The Python docker SDK is installed via requirements.txt.
Setup
-
Create and activate the conda environment (this installs Python, pip and R). Use conda-forge to ensure
r-baseis available:conda env create -f environment.yml -c conda-forge conda activate gail-env
-
Install Python dependencies in the conda env (the environment.yml already installs pip packages, but you can ensure by running):
pip install -r requirements.txt -
Install required R packages inside the conda env (this installs BCRA and helper packages):
Rscript -e "install.packages(c('BCRA','optparse','jsonlite'), repos='https://cloud.r-project.org')"
Run the FastAPI server
uvicorn main:app --reload --host 0.0.0.0 --port 8000Open http://localhost:8000/ in your browser, fill the form and submit.
Notes
-
The FastAPI
/predictendpoint runs the Docker image withdocker run --rm gail_model_containerand passes CLI args when Docker is available. If Docker is not installed or not on PATH, it falls back to runningRscript gail_model.Rlocally (ensure R and required packages are installed in that case). -
If you rename the Docker image, set the
GAIL_MODEL_IMAGEenvironment variable before starting FastAPI, e.g.:$env:GAIL_MODEL_IMAGE = 'my_gail_image' uvicorn main:app --reload
Troubleshooting
- If the R container fails due to missing packages during image build, try rebuilding and inspect build logs.
- If the FastAPI server cannot run docker, ensure the user has permission to run docker. On Windows using Docker Desktop, ensure Docker is running.
Security
- This example runs docker from the FastAPI process using the host Docker engine. In production, consider running the R computation in a more secure way (e.g., via an internal queue, separate service, or using an API wrapper around the R runtime) and validate/sanitize inputs carefully.
License
- Uses the BCRA package from CRAN (NCI). Follow the package license when using in production.