Ollama is a tool that allows you to run large language models locally on your machine. It provides a simple interface to interact with these models without needing to rely on external APIs. In R, we can use the ollamar package to interact with it.
Tip
There is also the rollama R package which provides a more R-like interface to Ollama, making it easier to work with in R scripts and applications. Nonetheles, the ollamar package is closely aligned with the official Ollama API and is more actively maintained. For this reason, we will use ollamar in this example.
To set it up, you can follow the instructions available in the R package vignette here. In brief, you need:
Install the ollamar package from CRAN.
Install the Ollama application on your machine.
Start the Ollama application and ensure it is running.
Once you have those three steps completed, you can try playing with different models. In this example, we will use the lamma3.2:1b model (Llama 3.2 with 1 billion parameters) to (a) generate some text using a prompt (like a chatbot), and (b) process text using embeddings.
10.1 Generating some text
Let’s start by calling ollama and verifying that the connection is up and running:
library(ollamar)
Attaching package: 'ollamar'
The following object is masked from 'package:stats':
embed
The following object is masked from 'package:methods':
show
# Checking that the connection is up and runningtest_connection()
Ollama local server running
<httr2_response>
GET http://localhost:11434/
Status: 200 OK
Content-Type: text/plain
Body: In memory (17 bytes)
Once this is working we can check the available models; in our case, the lamma3.2:1b model is already installed and available for use. You can check the available models with the following command:
To generate some text, we simply need to call the function generate() from the package. Let’s see what ollama says about INSNA Sunbelt! Since we are using a local LLM, the response will take a little bit longer than a typical API call; but the benefit is that we are running the model locally without passing our data to a large external server on the Cloud!
ans <-generate(model ="llama3.2:1b",prompt ="What is INSNA Sunbelt?")# Print the statusans
<httr2_response>
POST http://127.0.0.1:11434/api/generate
Status: 200 OK
Content-Type: application/json
Body: In memory (3271 bytes)
The model returns a response with status 200, which means that the request was successful. The generated text is stored in the text field of the response. We can extract and print it as follows:
resp_process(ans, "text")
[1] "INSNA (International Network for Strategic National Action) Sunbelt is a regional network of conservative and libertarian groups that focuses on promoting economic growth, limited government, and individual freedom in the Southern United States, particularly in states considered part of the \"Sun Belt.\"\n\nThe term \"Sunbelt\" refers to the region that includes Florida, Alabama, Georgia, Louisiana, Mississippi, South Carolina, North Carolina, Tennessee, Arkansas, Oklahoma, Kentucky, and West Virginia. These states are generally considered to be more economically liberal and less politically progressive compared to other parts of the country.\n\nINSNA was founded in 1984 by a group of conservative activists who were dissatisfied with the politics of the time in the South. The organization's goals include promoting free-market economic policies, reducing government spending, and supporting state-level libertarian or conservative initiatives.\n\nINSNA has been influential in shaping local and regional politics in the Sunbelt region, particularly in states like Florida, where it has played a significant role in the state's electoral politics and gubernatorial campaigns. The organization also engages in advocacy work on issues such as education reform, property rights, and judicial appointments.\n\nHowever, it's worth noting that INSNA has faced criticism for its perceived influence over state-level politics and its association with conservative or libertarian politicians who may not necessarily share INSNA's views on all issues."
This is the most basic functionality of the Ollama API. You can also use it for multimodal tasks, such as processing and generating text from images. The embeddings functionality is fantastic, and furthermore, a nice feature that may be of good use for many researchers. The next example tries to compare some text using the embeddings functionality of the LLM.
10.2 Using embeddings
For the embeddings component, we can simply use the embeddings() function from the ollamar package. This function allows us to generate embeddings for a given text, which can then be used for various tasks such as similarity comparisons, clustering, or as input features for machine learning models. The embeddings are a numerical representation of the text (or other types of data) that can capture semantic meaning and relationships between different pieces of text.
Note
To generate the agenda data stored in sunbelt2026_workshops.csv, we asked an AI (GitHub Copilot using Claude Sonnet 4.6) using the following prompt: “write an R script that reads the data in this file,, and generates a CSV dataset that contains the details of the events.” The file mentioned was an HTML file that I extracted from the INSNA Sunbelt 2026 website.
For this example, we will use INSNA Sunbelt 2026’s agenda and compare submissions by similarity based on their embeddings. Let’s start by loading the data and looking at a couple of rows:
library(data.table)
Attaching package: 'data.table'
The following object is masked from 'package:ollamar':
copy
title
<char>
1: Egocentric Network Analysis with R
2: Large Language Models, Text Embeddings, and Network Analysis
description
<char>
1: This workshop is an introduction to the R programming language and its tools to represent, manipulate, and analyze egocentric or personal network data. Topics include: introduction to ego-network research and data; data structures and network objects in R; visualizing ego-networks; calculating measures on ego-network composition and structure; converting ego-network measures to R functions; applying these functions to many ego-networks. The workshop relies on R tidyverse packages for data science, showing how they can be used to conduct common operations in ego-network analysis and scale those operations up to large collections of networks. We'll cover specific packages for network analysis (igraph, network, egor), data management (dplyr) and programming (purrr). No previous familiarity with R is required; participants only need a laptop with R and RStudio installed. This workshop has been taught for the past several years at different international conferences, including INSNA's Sunbelt and EUSN meetings. It draws on concepts and methods from the instructor's forthcoming book ""Analyzing personal networks using R"" (SAGE, in press). More details on the workshop's materials and requirements are here: raffaelevacca.com/egonet-r.
2: This 6-hour session offers a practical introduction to Python and the synthesis of network analysis with natural language processing methods, including large language models (LLMs). This begins with an introduction to text data management and preprocessing in Python. We introduce embedding, discussing its development in the early 2010s and demonstrating practical applications. We start with word embeddings and word2vec, and the evolution from static word embeddings to contextual token embeddings through transformer architecture (e.g., BERT). Recent developments in post-ChatGPT generative LLMs, and how such developments (e.g. reinforcement learning from human feedback, contrastive learning) have improved text embedding generation, will be explored with LLM2Vec. The second half will guide attendees through the construction and analysis of networks derived of text embeddings. This will include topic modelling (BERT and LLM2Vec), and approaches to constructing network representations derived of topic embeddings â\u0080\u0093 modelling the relational structure between the identified topics. The session will close with a demonstration of analyses which might be performed on the resulting network, such as backbone extraction (e.g., disparity filter, LANS) and the interpretation of the networksâ\u0080\u0099 features.
To use the embeddings model, we simply need to call the function embed() from the package. For this task, we will use the gemma embedding model (you can pull it using pull("embeddinggemma:300m")) Let’s generate embeddings for the titles and descriptions of the agenda items:
workshop_embeddings <-embed("embeddinggemma:300m", agenda[, description] )# Looking at the first two rows and first# five columns of the embeddingsworkshop_embeddings[1:2, 1:5]
With the embeddings, we can compute similarity between different agenda items. For example, we can calculate the euclidean distance between the embeddings, and subsequently identify similar workshops:
Looking at the first workshop (Egocentric Network Analysis with R), we can find the five most similar workshops based on their embeddings:
similar_ws_to_one <- ws_distances[1, ] |>sort() |>head(5) |>names() |>as.integer()# Which are similar?agenda[similar_ws_to_one, .(title)]
title
<char>
1: Egocentric Network Analysis with R
2: Exponential Family Random Graph Modeling (ERGMs) Using statnet
3: Understanding diffusion with NetdiffuseR
4: Valued Network Modeling with statnet
5: Continuous Time Network Dynamics with statnet
# Which are the least similar?least_similar_ws_to_one <--ws_distances[1, ] |>sort() |>head(5) |>names() |>as.integer()agenda[least_similar_ws_to_one, .(title)]
title
<char>
1: Beyond APIs: Collecting Web Data for Research Using the National Internet Observatory
2: Soccer Analytics and the FIFA World Cup 2026
3: Artificial Intelligence for the Scientist in a Hurry
4: Large Language Models, Text Embeddings, and Network Analysis
5: Seeing Science through Networks: Disciplinary Hardness and Softness
Which seems to check!
10.3 Final thoughts
Using Ollama we can leverage the power of LLMs (or SLMs in this case) locally on our machines. The ollama project also provides some capabilities to run larger models on the cloud, for which you would need to have an account with them. Multiple project around rely on tools such as Ollama to run LLMs locally, and the R package ollamar provides a simple interface to interact with these models. From the research perspective, this allow scientists to go beyond the simple exploitation of a Chat bot and use LLMs for more complex tasks such as embeddings, clustering, and other machine learning tasks.