Skip to contents

Introduction

The openalexPro package enables downloading and processing large scholarly datasets from OpenAlex. Unlike openalexR which processes everything in memory, openalexPro writes results to disk, enabling analysis of datasets too large to fit in RAM.

Processing uses DuckDB for efficient queries without loading entire datasets into memory.

Architecture Overview

flowchart TD
    OA[("OpenAlex<br/>api.openalex.org · content.openalex.org")]
    SNAP[("OpenAlex Snapshot<br/>bulk ndjson.gz")]

    PQ["pro_query()<br/>build URL · validate filters · chunk IDs<br/>or OQL / OQO query"]
    PC["pro_count()"]
    PRL["pro_rate_limit_status()<br/>pro_validate_credentials()"]

    AC["api_call()  ·  internal helper<br/>retry · backoff · error handling"]

    subgraph PF["pro_fetch()  —  convenience wrapper"]
        PR["pro_request()<br/>download metadata pages → JSON"]
        PRP["pro_request_parquet()<br/>JSON → partitioned Parquet<br/>reconstruct abstract · build citation"]
        PR --> PRP
    end

    PDC["pro_download_content()<br/>download PDFs / TEI XML"]

    SNP["openalexSnapshot package<br/>snapshot_to_parquet() · build_corpus_index()<br/>lookup_by_id()"]

    OUT[("Parquet dataset<br/>read_corpus() · DuckDB · arrow")]
    FILES[("PDF / TEI XML files<br/>on disk")]

    OA <--> AC
    PQ -->|URL| PR
    PQ -->|URL| PC

    PR -.->|via| AC
    PDC -.->|via| AC
    PRL -.->|via| AC
    PC -.->|via| AC

    PRP --> OUT
    PDC --> FILES

    SNAP --> SNP --> OUT

    style OA    fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
    style SNAP  fill:#fff3e0,stroke:#e65100,stroke-width:2px
    style OUT   fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px
    style FILES fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px
    style AC    fill:#cce5ff,stroke:#0066cc,stroke-width:2px

Colour Meaning
Blue OpenAlex online resource
Orange OpenAlex Snapshot (local bulk input)
Green Output data (Parquet dataset, PDF/XML files)
Light blue api_call() — internal HTTP helper shared by all API-calling functions

Two Approaches

openalexPro offers two approaches depending on your needs:

flowchart TD
    Start([Start]) --> Choice{Need fine<br/>control?}

    Choice -->|No| Simple[Simple Approach:<br/>pro_fetch]
    Choice -->|Yes| Advanced[Advanced Approach:<br/>Individual functions]

    Simple --> Result1[Parquet Dataset]
    Advanced --> Result2[Parquet Dataset]

    style Simple fill:#e1f5e1
    style Advanced fill:#cce5ff

Approach When to Use
Simple: pro_fetch() Quick analysis, standard workflow, getting started
Advanced: Individual functions Custom processing from downloaded json, more control, advanced workflows

Simple Approach: pro_fetch()

For most use cases, pro_fetch() handles everything in one call:

flowchart LR
    subgraph Input
        URL[Query URL]
    end

    subgraph pro_fetch[pro_fetch]
        PR[pro_request<br/>Download JSON]
        PRP[pro_request_parquet<br/>Convert]
        PR --> PRP
    end

    subgraph Output[Project Folder]
        JSON[json/<br/>deleted after conversion]
        PARQUET[parquet/]
    end

    URL --> pro_fetch
    pro_fetch --> Output

    style pro_fetch fill:#cce5ff
    style Output fill:#e1f5e1

Example

library(openalexPro)

# Build query
url <- pro_query(
  entity = "works",
  search = "climate change adaptation",
  from_publication_date = "2023-01-01",
  type = "article",
  select = c(
    "ids",
    "title",
    "publication_year",
    "cited_by_count",
    "authorships"
  )
)

# Fetch everything in one call
parquet_path <- pro_fetch(
  query_url = url,
  project_folder = "climate_research",
  progress = TRUE
)

# Analyze with DuckDB
library(duckdb)
con <- dbConnect(duckdb())
results <- dbGetQuery(
  con,
  "
  SELECT title, cited_by_count
  FROM read_parquet('climate_research/parquet/**/*.parquet')
  ORDER BY cited_by_count DESC
  LIMIT 10
"
)
dbDisconnect(con)

Advanced Approach: Individual Functions

For more control, use the functions separately:

flowchart TD
    subgraph Step1[Step 1: Query]
        PQ[pro_query]
        PC[pro_count]
    end

    subgraph Step2[Step 2: Download]
        PR[pro_request]
    end

    subgraph Step3[Step 3: Convert]
        PRP[pro_request_parquet]
    end

    subgraph Step4[Step 4: Analyze]
        DB[(DuckDB)]
    end

    PQ -->|URL| PC
    PC -->|Check size| PR
    PQ -->|URL| PR
    PR -->|JSON files| PRP
    PRP -->|Parquet dataset| DB

    style Step1 fill:#e1f5e1
    style Step2 fill:#cce5ff
    style Step3 fill:#fff3cd
    style Step4 fill:#e1f5e1

Step 1: Build and Validate Query

library(openalexPro)

# Build the query URL
url <- pro_query(
  entity = "works",
  search = "CRISPR gene therapy",
  from_publication_date = "2020-01-01",
  type = "article",
  is_oa = TRUE,
  select = c(
    "ids",
    "title",
    "abstract_inverted_index",
    "publication_year",
    "cited_by_count",
    "authorships",
    "topics"
  )
)

# Check how many results (recommended before large downloads)
count <- pro_count(url)
print(paste("Results:", count$count))

Step 2: Download JSON from API

# Download with progress bar
pro_request(
  query_url = url,
  output = "data/json",
  pages = 10000, # Max pages per query (default: 100000)
  progress = TRUE, # Show progress bar
  workers = 1 # Parallel workers for a list of queries
)

Output Structure

data/json/
├── 00_in.progress      # Deleted when complete
├── results_page_1.json
├── results_page_2.json
├── results_page_3.json
└── ...

Step 3: Convert to Parquet

# Convert JSON to Parquet with parallel processing
pro_request_parquet(
  input_json = "data/json",
  output = "data/parquet",
  progress = TRUE,
  workers = 4, # Use 4 parallel workers
  delete_input = FALSE # Keep JSON files
)

What Happens During Conversion

Each JSON file becomes one Parquet file, converted with DuckDB. For works, two columns are added (enrich = TRUE, the default):

flowchart LR
    subgraph Input[JSON from OpenAlex]
        IAI[abstract_inverted_index]
        Auth[authorships]
        Other[other fields...]
    end

    subgraph Transform[pro_request_parquet]
        T1[Reconstruct abstract<br/>from inverted index]
        T2[Generate citation string<br/>Smith et al. 2023]
        T3[Add page field<br/>for provenance]
    end

    subgraph Output[Parquet Output]
        Abstract[abstract: full text]
        Citation[citation: string]
        Page[page: identifier]
        OtherOut[other fields...]
    end

    IAI --> T1 --> Abstract
    Auth --> T2 --> Citation
    Other --> T3 --> Page
    Other --> OtherOut

    style Transform fill:#fff3cd

Files that fail to convert are retried once on their own; by default (on_error = "error") remaining failures stop the run and name the files. With delete_input = TRUE, the JSON files are only deleted when every file converted, and resume = TRUE converts only what is still missing.

Schema Harmonization

Different OpenAlex records can have different field structures. pro_request_parquet() infers one schema from a sample of files and applies it to all of them:

flowchart TD
    subgraph Input[JSON Files with Varying Schemas]
        F1["File 1: location = struct"]
        F2["File 2: location = string"]
        F3["File 3: location = null"]
    end

    subgraph Process[Schema Harmonization]
        Sample[Sample records<br/>from all files]
        Infer[Infer unified schema<br/>using DuckDB]
        Apply[Apply schema<br/>to all files]
    end

    subgraph Output[Parquet Dataset]
        Unified["Unified schema<br/>location = struct"]
    end

    F1 --> Sample
    F2 --> Sample
    F3 --> Sample
    Sample --> Infer
    Infer --> Apply
    Apply --> Unified

    style Process fill:#cce5ff
    style Output fill:#e1f5e1

Step 4: Analyze with DuckDB

library(duckdb)
library(DBI)

con <- dbConnect(duckdb())

# Top cited papers
top_cited <- dbGetQuery(
  con,
  "
  SELECT title, publication_year, cited_by_count
  FROM read_parquet('data/parquet/**/*.parquet')
  WHERE cited_by_count IS NOT NULL
  ORDER BY cited_by_count DESC
  LIMIT 20
"
)

# Papers per year
by_year <- dbGetQuery(
  con,
  "
  SELECT publication_year, COUNT(*) as count
  FROM read_parquet('data/parquet/**/*.parquet')
  GROUP BY publication_year
  ORDER BY publication_year
"
)

# Search abstracts
keyword_search <- dbGetQuery(
  con,
  "
  SELECT title, abstract, cited_by_count
  FROM read_parquet('data/parquet/**/*.parquet')
  WHERE abstract LIKE '%machine learning%'
  ORDER BY cited_by_count DESC
  LIMIT 10
"
)

dbDisconnect(con)

Handling Large Datasets

Parallel Processing

flowchart TD
    subgraph Chunked[Chunked Query: 150 DOIs]
        C1[chunk_1: 50 DOIs]
        C2[chunk_2: 50 DOIs]
        C3[chunk_3: 50 DOIs]
    end

    subgraph Workers[4 Parallel Workers]
        W1[Worker 1]
        W2[Worker 2]
        W3[Worker 3]
        W4[Worker 4]
    end

    subgraph Output[Output Folders]
        O1[chunk_1/]
        O2[chunk_2/]
        O3[chunk_3/]
    end

    C1 --> W1
    C2 --> W2
    C3 --> W3
    W1 --> O1
    W2 --> O2
    W3 --> O3

    style Workers fill:#cce5ff

# Large DOI list - automatically chunked
dois <- readLines("my_dois.txt") # 1000+ DOIs

urls <- pro_query(
  entity = "works",
  doi = dois,
  select = c("ids", "title", "cited_by_count")
)

# Parallel download: each chunk goes to the next free worker
pro_request(
  query_url = urls,
  output = "data/json",
  workers = 4, # 4 parallel downloads
  progress = TRUE
)

# Parallel Parquet conversion
pro_request_parquet(
  input_json = "data/json",
  output = "data/parquet",
  workers = 4, # 4 parallel conversions
  progress = TRUE
)

Managing Disk Space

For very large datasets, delete the JSON files after conversion (pro_fetch() does this by default). They are kept if any file failed to convert:

pro_request_parquet(
  input_json = "data/json",
  output = "data/parquet",
  delete_input = TRUE # Remove JSON files after a complete conversion
)

Fewer, Larger Parquet Files

By default pro_request_parquet() writes one Parquet file per API page (up to 200 records). That keeps the conversion parallel and resumable, but a large query becomes thousands of small files, which are slow to read. compact_parquet combines each query’s page files into files of up to that many records once the conversion is complete; pro_compact_parquet() does the same for an existing dataset:

pro_fetch(query_url = urls, project_folder = "data", compact_parquet = 250000)

# or, for a dataset converted before
pro_compact_parquet("data/parquet", rows = 250000)

Measured for 1.1 million works (5,460 page files): compaction took about 20 seconds; afterwards aggregates and lookups by ID ran 35 to 50 times faster, reading full records with DuckDB 4 times faster, and the dataset was 40% smaller. Files of 50,000 to 1 million records read equally fast.

  • Records, columns and their order do not change; the page column stays.
  • Each query (partition) is compacted on its own. A compacted file holds whole consecutive pages and is named after them, e.g. results_pages_1-1250.parquet.
  • Files are compacted one at a time; an interrupted compaction is completed by running it again (or resume = TRUE), and resume never converts a compacted page again.
  • If a query cannot be compacted, a warning says so and its page files stay.
  • Indexes built by openalexSnapshot point to files and row numbers: pro_compact_parquet() stops if such an index exists next to the dataset.

Progress Tracking

All functions create a 00_in.progress file during processing:

flowchart LR
    Start([Start]) --> Create[Create<br/>00_in.progress]
    Create --> Process[Processing...]
    Process --> Success{Success?}
    Success -->|Yes| Delete[Delete<br/>00_in.progress]
    Success -->|No| Keep[Keep file<br/>indicates incomplete]

    style Delete fill:#e1f5e1
    style Keep fill:#f8d7da

Check if processing completed:

# Check if download is complete
if (file.exists("data/json/00_in.progress")) {
  message("Download still in progress or was interrupted!")
} else {
  message("Download complete")
}

To complete an interrupted run, call the same function again with resume = TRUE; only unfinished queries (or unconverted files) are processed:

pro_fetch(query_url = urls, project_folder = "data", resume = TRUE)

Workflow Examples

Example 1: Institution Analysis

Analyze research output from multiple institutions:

# MIT, Stanford, Harvard
institutions <- c("I63966007", "I97018004", "I136199984")

# Build queries for each
urls <- lapply(institutions, function(inst) {
  pro_query(
    entity = "works",
    `institutions.id` = inst,
    from_publication_date = "2020-01-01",
    type = "article",
    select = c("ids", "title", "publication_year", "cited_by_count", "topics")
  )
})
names(urls) <- c("MIT", "Stanford", "Harvard")

# Download each
for (name in names(urls)) {
  pro_fetch(
    query_url = urls[[name]],
    project_folder = file.path("institutions", name),
    progress = TRUE
  )
}

# Compare with DuckDB
library(duckdb)
con <- dbConnect(duckdb())

comparison <- dbGetQuery(
  con,
  "
  SELECT
    'MIT' as institution,
    COUNT(*) as papers,
    AVG(cited_by_count) as avg_citations
  FROM read_parquet('institutions/MIT/parquet/**/*.parquet')
  UNION ALL
  SELECT
    'Stanford',
    COUNT(*),
    AVG(cited_by_count)
  FROM read_parquet('institutions/Stanford/parquet/**/*.parquet')
  UNION ALL
  SELECT
    'Harvard',
    COUNT(*),
    AVG(cited_by_count)
  FROM read_parquet('institutions/Harvard/parquet/**/*.parquet')
"
)

dbDisconnect(con)

Example 2: Citation Network

Build a citation network for a set of papers:

# Start with seed papers
seed_dois <- c(
  "10.1038/nature12373",
  "10.1126/science.1259855"
)

# Get seed papers
seed_url <- pro_query(
  entity = "works",
  doi = seed_dois,
  select = c("ids", "title", "cited_by_count", "referenced_works")
)

pro_fetch(
  query_url = seed_url,
  project_folder = "citation_network/seeds"
)

# Get papers that cite the seeds
for (doi in seed_dois) {
  citing_url <- pro_query(
    entity = "works",
    cites = paste0("https://doi.org/", doi),
    select = c("ids", "title", "publication_year", "cited_by_count")
  )

  pro_fetch(
    query_url = citing_url,
    project_folder = paste0("citation_network/citing_", gsub("/", "_", doi))
  )
}

Example 3: Time Series Analysis

Track research trends over time:

# Get publication counts by year using group_by
url <- pro_query(
  entity = "works",
  search = "artificial intelligence",
  from_publication_date = "2000-01-01",
  group_by = "publication_year"
)

# For group_by queries, use pro_request directly
pro_request(
  query_url = url,
  output = "ai_trends/json",
  progress = TRUE
)

# The JSON contains aggregated counts, not individual works

Example 4: Topic Frequencies

Count which research topics a set of works is about:

url <- pro_query(
  entity = "works",
  search = "climate change",
  from_publication_date = "2023-01-01",
  type = "article",
  select = c("ids", "title", "topics")
)

pro_fetch(
  query_url = url,
  project_folder = "topic_analysis"
)

# Unnest topics and count with DuckDB
library(duckdb)
con <- dbConnect(duckdb())

topic_counts <- dbGetQuery(
  con,
  "
  WITH unnested AS (
    SELECT
      id,
      unnest(topics) as topic
    FROM read_parquet('topic_analysis/parquet/**/*.parquet')
  )
  SELECT
    topic.display_name as topic_name,
    topic.field.display_name as field_name,
    COUNT(*) as count
  FROM unnested
  GROUP BY topic.display_name, topic.field.display_name
  ORDER BY count DESC
  LIMIT 50
"
)

dbDisconnect(con)

Example 5: Downloading Full-Text Content

OpenAlex provides full-text PDFs (~60 M works) and Grobid TEI XML (~43 M works) via content.openalex.org. Use pro_download_content() to retrieve them. Each file costs $0.01.

# Step 1: find works that have a PDF available
url <- pro_query(
  entity = "works",
  `has_content.pdf` = TRUE,
  `best_oa_location.license` = c("cc-by", "cc-by-sa"),
  from_publication_date = "2023-01-01",
  type = "article",
  select = c("ids", "title", "publication_year")
)

# Step 2: download metadata to get the work IDs
pro_fetch(
  query_url = url,
  project_folder = "oa_works"
)

# Step 3: read IDs from the parquet dataset
library(duckdb)
con <- dbConnect(duckdb())
ids <- dbGetQuery(
  con,
  "SELECT id FROM read_parquet('oa_works/parquet/**/*.parquet')"
)$id
dbDisconnect(con)

# Step 4: download full-text PDFs
results <- pro_download_content(
  ids     = ids,
  output  = "oa_works/pdfs",
  format  = "pdf",
  workers = 4         # parallel downloads
)

# results is a data frame: id | file | status | message
table(results$status)
# ok        not_found  error
# 892       45         3

pro_download_content() never aborts on partial failures — inspect the status column to identify any files that need retrying.

For TEI XML (structured full text parsed by Grobid):

results_xml <- pro_download_content(
  ids    = ids,
  output = "oa_works/xml",
  format = "grobid-xml"
)

Data Pipeline Summary

flowchart TD
    subgraph API[OpenAlex API]
        OA[(250M+ Works)]
    end

    subgraph Query[1. Query Building]
        PQ[pro_query]
        PC[pro_count]
        PQ --> PC
    end

    subgraph Download[2. Download]
        PR[pro_request]
        JSON[(JSON Files<br/>~200 records/file)]
    end

    subgraph Convert[3. Convert]
        PRP[pro_request_parquet]
        Abstract[Abstract reconstruction]
        Citation[Citation generation]
        Schema[Schema harmonization]
        PARQUET[(Parquet Dataset<br/>Columnar, compressed)]
    end

    subgraph Analyze[4. Analyze]
        DUCK[(DuckDB)]
        R[R/tidyverse]
        Python[Python/pandas]
    end

    OA --> PR
    PQ --> PR
    PR --> JSON
    JSON --> PRP
    PRP --> Abstract
    PRP --> Citation
    PRP --> Schema
    Abstract --> PARQUET
    Citation --> PARQUET
    Schema --> PARQUET
    PARQUET --> DUCK
    PARQUET --> R
    PARQUET --> Python

    style API fill:#e1f5e1
    style Query fill:#cce5ff
    style Download fill:#fff3cd
    style Convert fill:#e1f5e1
    style Analyze fill:#cce5ff

Function Quick Reference

Function Purpose Key Parameters
pro_query() Build API URL entity, search, filters, select, oql, oqo
pro_count() Get result count query_url
pro_fetch() All-in-one download query_url, project_folder, workers, download_workers, resume
pro_request() Download JSON query_url, output, pages, workers, resume
pro_request_parquet() Convert JSON to Parquet input_json, output, workers, resume
pro_validate_credentials() Test credentials (optional helper) api_key
pro_rate_limit_status() Check rate limit usage & remaining budget api_key, verbose
pro_download_content() Download full-text PDFs or TEI XML ids, output, format, workers

See Also