Learn R Programming

llmflow

Overview

llmflow provides a framework for automated data analysis through the integration of Large Language Models (LLMs) with R. Built on the ReAct (Reasoning and Acting) architecture, the package enables iterative problem-solving by alternating between reasoning steps and code execution, allowing LLMs to autonomously analyze data, handle errors, and refine solutions.

Key Features

  • ReAct Architecture: Iterative reasoning and acting loops for complex analytical tasks
  • Automated Code Execution: LLM-generated R code runs in isolated sessions with error handling
  • Intelligent Error Recovery: Automatic retry mechanisms with progressive error escalation
  • Session Persistence: Maintain R session state across multiple LLM interactions
  • Structured Output: JSON schema validation for reliable, parseable responses
  • RAG Integration: Retrieval-augmented generation with R function documentation

Installation

From R-universe:

install.packages("llmflow", repos = c("https://zaoqu-liu.r-universe.dev", "https://cloud.r-project.org"))

From GitHub:

# install.packages("pak")
pak::pak("Zaoqu-Liu/llmflow")

Requirements

The package requires ellmer for LLM communication:

install.packages("ellmer")

Quick Start

library(llmflow)
library(ellmer)

# Initialize LLM client
llm <- chat_openai(model = "gpt-4o")

# Automated analysis with ReAct workflow
result <- AutoFlow(
 react_llm = llm,
 task_prompt = "Perform linear regression of mpg on hp and wt using mtcars"
)

Core Functions

FunctionDescription
AutoFlow()Complete workflow combining RAG and ReAct
react_r()ReAct loop for iterative problem solving
response_to_r()Execute LLM-generated R code
response_as_json()Structured JSON output with schema validation
retrieve_docs()Retrieve R function documentation for RAG

Usage Examples

ReAct Workflow

result <- react_r(
 chat_obj = llm,
 task = "Calculate correlation matrix for iris numeric columns",
 max_turns = 10,
 verbose = TRUE
)

# Access results
result$final_answer
result$code_summary$complete_script

JSON Response with Schema Validation

schema <- list(
 type = "object",
 properties = list(
   analysis_type = list(type = "string"),
   findings = list(type = "array", items = list(type = "string"))
 ),
 required = c("analysis_type", "findings")
)

response <- response_as_json(
 chat_obj = llm,
 prompt = "Summarize the iris dataset",
 schema = schema,
 schema_strict = TRUE
)

Code Generation and Execution

result <- response_to_r(
 chat_obj = llm,
 prompt = "Create a scatter plot of mpg vs hp from mtcars",
 pkgs_to_use = c("ggplot2"),
 return_mode = "full"
)

License

GPL (>= 3)

Citation

Liu Z (2026). llmflow: Reasoning and Acting Workflow for Automated Data Analysis.
R package version 3.0.1, https://github.com/Zaoqu-Liu/llmflow

Author

Zaoqu Liu

Copy Link

Version

Install

install.packages('llmflow')

Version

3.0.2

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Zaoqu Liu

Last Published

January 31st, 2026

Functions in llmflow (3.0.2)

save_code_to_file

Save extracted code to file
extract_json

Extract and parse JSONs from a string (LLM response)
extract_python_code

Extract Python code from a string
package_function_schema

Create JSON Schema for Package Function Validation
package_extraction_prompt

Generate Function Extraction Prompt for LLM Analysis
extract_chat_history

Extract chat history from ellmer chat object
extract_bash_code

Extract Bash/Shell code from a string
extract_sql_code

Extract SQL code from a string
AutoFlow

AutoFlow - Automated R Analysis Workflow with LLM
extract_code

Generic function to extract code of any specified language
extract_javascript_code

Extract JavaScript code from a string
extract_function_examples

Extract Examples from a Package Function
extract_r_code

Extract R code from a string
response_to_r

Response to R code generation and execution with session continuity
retrieve_docs

Retrieve and Format R Function Documentation for LLM Consumption
react_using_r

ReAct (Reasoning and Acting) using R code execution - Optimized Version
react_r

Simplified interface - Enhanced react_r
prompt_from_history

Build prompt from chat history
response_as_json

Get JSON response from LLM with validation and retry