flairR
: An R Wrapper for Accessing Flair NLP Library
flaiR is an R package that provides convenient access to flairNLP/flair, a powerful Python-based NLP toolkit developed by Humboldt University of Berlin. flaiR package is maintained by Yen-Chieh Liao (University of Birmingham) and Stefan Müller from Next Generation Energy Systems and Text and Policy Research Group at UCD.
Through flaiR, R users can easily utilize and combine various word embeddings, train deep learning models, and fine-tune the latest transformer models from Hugging Face, bridging advanced NLP techniques with popular quantitative text analysis toolkits like quanteda in the R environment.
Required Software
- Python 3.9 - 3.11 (Python 3.10 recommended and Python 3.8 is deprecated for Flair NLP usage.)
- R >= 4.2.0
- Rstudio
Note:
Python 3.10 is recommended for flaiR R package due to its stable compatibility with the latest official Flair NLP release and #3560. While Flair NLP, which is built on PyTorch, supports Python 3.9 - 3.12, we specifically recommend Python 3.10 for optimal stability with PyTorch. Python 3.8 is deprecated for Flair NLP usage, and Python 3.12+ is not recommended due to compatibility issues, as noted in the PyTorch installation guide and Python’s official development guide.
Operation Systems
OS | R Versions | Python Version |
---|---|---|
Mac | 4.3.2, 4.2.0, |
3.10.x, 3.9 |
Windows | 4.0.5, Latest | 3.10.x, 3.9 |
Ubuntu | 4.3.2, 4.2.0, 4.2.1 | 3.10.x, 3.9 |
*: On R 4.2.1, particularly when using the Matrix package on ARM 64 architecture Macs (M1/M2), compatibility issues with gfortran may occur. It’s recommended to avoid this combination.
Fast Install Python and flaiR
with reticulate
If you haven’t installed Python or have an incompatible Python version:
Step 1 Check Python configuration:
# Check Python version and configuration
install.packages("reticulate")
library(reticulate)
reticulate::py_config()
Example output showing incompatible Python version: Version 3.8 is deprecated, needs upgrade
python: /Users/username/Library/r-miniconda/bin/python
libpython: /Users/username/Library/r-miniconda/lib/libpython3.10.dylib
pythonhome: /Users/username/Library/r-miniconda
version: 3.8
Step 2 Install Python 3.10 using reticulate:
# Install and load reticulate package
install_python(version = "3.10")
Verify the new Python installation:
Expected output after installation:
python: /Users/username/Library/r-miniconda/bin/python
libpython: /Users/username/Library/r-miniconda/lib/libpython3.10.dylib
pythonhome: /Users/username/Library/r-miniconda
version: `3.10`
Step 3 To install flaiR R package
# Install remotes if not already installed
install.packages("remotes")
# Install flaiR from GitHub
remotes::install_github("davidycliao/flaiR", force = TRUE)
#> flaiR: An R Wrapper for Accessing Flair NLP 0.13.1
This will ensure you have the correct Python version and all necessary dependencies for flaiR.
Installation with Docker
Intel/AMD Processors:
# Pull image
docker pull ghcr.io/davidycliao/flair-rstudio:latest
# Run container
docker run -d -p 8787:8787 --user root --name flair-rstudio ghcr.io/davidycliao/flair-rstudio:latest
Apple Silicon (M1/M2 Mac):
# Pull image
docker pull --platform linux/amd64 ghcr.io/davidycliao/flair-rstudio:latest
# Run container
docker run -d -p 8787:8787 --platform linux/amd64 --user root --name flair-rstudio ghcr.io/davidycliao/flair-rstudio:latest
After running these commands in terminal (or powershell), open your browser and navigate to http://localhost:8787
to access RStudio. For detailed installation instructions, please visit Quick Start Guide.
Some Useful Cases
For more detailed tutorial instructions, please visit Flair NLP and flaiR for Social Science.
Contribution and Open Source
R developers who want to contribute to flaiR
are welcome – flaiR is an open source project. We warmly invite R users who share similar interests to join in contributing to this package. Please feel free to shoot me an email us to collaborate on the task. Contributions – whether they be comments, code suggestions, tutorial examples, or forking the repository – are greatly appreciated. Please note that the flaiR
is released with the Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
The primary communication channel for R users can be found here. Please feel free to share your insights on the Discussion page and report any issues related to the R interface in the Issue section. If the issue pertains to the actual implementation of Flair in Python, please submit a pull request to the offical flair NLP.