Artificial Intelligence and coding in R
An attempt at a tutorial to install a language model (LLM) directly on your own computer and use it, for instance, to help you write R code, for free, with no account, and without sending your data to a remote server.
Why run an AI model locally?
Running a model "locally" means executing it on your own machine rather than on a company's servers (like ChatGPT or Claude). The upsides: it's free, it works offline, your data stays private, and it's a great way to understand concretely how these tools work. The trade-off: quality depends on the size of the model your hardware can handle.
Hardware requirements
An LLM must be loaded into memory to produce text, so the main constraint is available memory. To generate decent R scripts, aim for a model of at least 3 to 7 billion parameters (written 3B to 7B).
- Without a graphics card: plan for roughly 7 GB of free RAM for a 7B model.
- With a graphics card: it needs at least 7 GB of VRAM, and computation is much faster on a GPU.
Rule of thumb: a smaller model (0.5B–1.5B) will run on almost any machine but gives more limited results. It's up to you to find the best compromise for your computer.
The tutorial in 3 steps
- Install a local LLM server
These tools handle downloading and running models, with a ChatGPT-style chat interface. The simplest place to start is Ollama. User-friendly alternatives: LM Studio and Open WebUI. Download, install, and you're ready.
- Download a model
Once Ollama is installed, open a terminal and pull a model specialised in code. For example:
ollama pull qwen2.5-coder:3bReplace
3bwith the size you want (0.5b,1.5b,3b,7b…) depending on your hardware. - Ask your questions
Launch the model and chat with it like any regular chatbot:
ollama run qwen2.5-coder:3bYou can then ask, for example: "Write me an R script that loads a CSV file, computes the mean per group and draws a boxplot." Always review and test the generated code, a small model can make mistakes.
Which models to try?
For help with R code, two good families to start with:
- Qwen2.5-Coder, available in several sizes (0.5B, 1.5B, 3B, 7B and more). Results often start to get interesting from 3B onwards, but this is worth testing.
- Mistral, the models from the French company, also available through Ollama and LM Studio.
Tip: start small, judge the quality of the answers, then scale up if your machine can keep up.
Going further
- Building a local AI setup on your laptop, a step-by-step guide (Medium, 🇬🇧)
- The complete guide to building your free local AI (Reddit r/selfhosted, 🇬🇧)
- Run AI models locally, even on a mid-tier laptop (dev.to, 🇬🇧)
- Video tutorial, installing an AI locally (YouTube)
- Introducing AI in RStudio (Posit blog, 🇬🇧)
- Using AI to write R code (YouTube)
A critical look: the ecological cost of AI
Using these tools, whether locally or in the cloud, is not neutral: training and running large models consume enormous amounts of energy and water and emit CO₂. A few readings to keep a critical mind (in French):
- L'insoutenable coût écologique du boom de l'IA (Reporterre, 🇫🇷)
- IA : un puits sans fond de dépenses en énergie, en eau et en CO₂ (Mediapart, 🇫🇷)
- Enjeux numériques, l'empreinte environnementale de l'IA (Cairn, 🇫🇷)
- Le Monde diplomatique article (July 2025) (🇫🇷)
Put together by Antoine Géré.