Generative AI for the UseR
Slides: machlis.com/R-Ladies-Paris-2025
Repo: github.com/smach/RLadiesParis
3 Uses of LLMs With R Help you write R code Help you analyze data Add LLM functionality to your R code and apps
They are predictive systems that work on probabilities! That means:
How do we deal with this?
Check their results and keep a human in the loop!
btw R package
Easy way to copy & paste docs into a chatbot session
Slide from Tom Mock at Posit’s R in Pharma talk
If you want to find out more:
Watch the posit::conf(2025) presentation at
https://youtu.be/9ZW2tx5fHjk
As of now, the Positron Assistant only supports “Anthropic for chat and GitHub Copilot for inline code completions.” Anthropic API key needed and it’s pay per use. Can run up a bill ($4 in 30 minutes). Other providers are planned.
(Not necessarily optimized for R)
OpenAI API key required to run.
Databot!
https://positron.posit.co/databot.html
Setting > databot.researchPreviewAcknowledgment and type Acknowledged
Command Palette Ctrl-Shift-P > Open Databot
Posit demo of a dashboard with querychat: https://jcheng.shinyapps.io/sidebot/
For this all the following code using ellmer and ragnar, you’ll need an OpenAI API key to run it yourself
library(querychat)
my_data <- rio::import("data/UN_sample_data.csv")
my_dictionary <- "data/UN_sample_dictionary.md"
my_greeting <- "data/UN_sample_greeting.md"
querychat_config <- querychat_init(
data_source = my_data,
data_description = readLines(my_dictionary),
greeting = readLines(my_greeting),
create_chat_func = purrr::partial(ellmer::chat_openai, model = "gpt-4.1") )Maybe your data is too big to fit into an LLM context window.
Maybe your data does fit, but that can get expensive! (And slow. And it’s unclear whether all LLMs can handle information well at their full context windows.)
Retrieval Augmented Generation
Retrieval: Retrieve text chunks that are most similar to a user’s query
Augmented: Send those chunks to an LLM as context along with the question
Generation – LLM uses that text to generate its response.
https://www.infoworld.com/article/4020484/generative-ai-rag-comes-to-the-r-tidyverse.html
Or my 2-hour Workshop for Ukraine video + code (for a €20 donation to help Ukraine)
ellmer is the main tidyverse package for connecting to LLMs in R
library(ellmer)
# This is for extracting MULTIPLE of each from a text block
desired_data_structure_for_arrays <- type_array(
type_object(
date = type_string(
paste("Date in yyyy-mm-dd format. If year isn't mentioned, date that makes the most sense relative to today's date of", Sys.Date())
),
speaker_name = type_string(),
description = type_string(
"Brief description of the program"
)
)
)Why needed?
Start with conventional R functions
days_left_in_year <- function(current_date = as.character(Sys.Date()) ) {
current_date <- as.Date(current_date)
first_day_of_next_year <- lubridate::ceiling_date(Sys.Date(), unit = "year") |>
as.Date()
days_left <- first_day_of_next_year - current_date
return(as.integer(days_left))
}
days_left_in_year()
get_current_date <- function() {
return(as.character(Sys.Date()))
}Turn it into an ellmer tool
days_left_in_year_tool <- tool(
days_left_in_year,
name = "days_left_in_year",
description = "Returns the number of days left in the current year based on a given starting date. Starting date defaults to today's date",
arguments = list(
current_date = type_string(
"Date as a string in yyyy-mm-dd format",
required = FALSE
)
)
)
get_current_date_tool <- tool(
get_current_date,
name = "get_current_date",
description = "Gets the current date"
)Register the tools and re-run the chat
Harnessing LLMs for Data Analysis, Joe Cheng, CTO at Posit
Hadley Wickham’s Using LLMs with ellmer (for a €20 donation to help Ukraine)
posit::conf(2025) You’ll need to search to find AI-related talks https://www.youtube.com/playlist?list=PL9HYL-VRX0oTixlfDPCS5RW_F1pccERRe
R+AI Conference Videos on YouTube
May only be available for a few weeks https://www.youtube.com/playlist?list=PL4IzsxWztPdkm4lcgBilHQjBoFFysP81e
Bluesky: @smachlis.bsky.social
Mastodon: @smach@masto.machlis.com
GitHub: @smach
LinkedIn: in/sharonmachlis
Website: machlis.com
Sharon Machlis