Package 'openalexSnowball'

Title: Snowball searches for OpenAlex using the openalexPro pipeline
Description: Perform snowball searches on the OpenAlex citation graph using openalexPro's on-disk processing pipeline and store results in Parquet.
Authors: Rainer M Krug [aut, cre] (ORCID: <https://orcid.org/0000-0002-7490-0066>)
Maintainer: Rainer M Krug <[email protected]>
License: MIT + file LICENSE
Version: 0.15.6
Built: 2026-11-24 21:51:16 UTC
Source: https://github.com/openalexPro/openalexSnowball

Help Index


A function to perform a snowball search and convert the result to a tibble/data frame.

Description

A function to perform a snowball search and convert the result to a tibble/data frame.

Usage

pro_snowball(
  identifier = NULL,
  doi = NULL,
  snapshot = NULL,
  max_results = 100000L,
  workers = 1L,
  chunk_limit = NULL,
  select = NULL,
  endpoint = "https://api.openalex.org",
  duckdb_config = NULL,
  resume = FALSE,
  keep_intermediates = FALSE,
  output = tempfile(fileext = ".snowball"),
  verbose = FALSE
)

Arguments

identifier

Character vector of openalex identifiers.

doi

Character vector of dois.

snapshot

Path to a local OpenAlex snapshot (either a root directory containing ⁠parquet/⁠, or the ⁠parquet/⁠ directory itself). When supplied, the whole snowball is built offline from the snapshot instead of the OpenAlex API; when NULL (the default) behaviour is unchanged.

Snapshot mode requires the indexes built by openalexSnapshot::build_corpus_index() and openalexSnapshot::build_citation_index(), plus openalexSnapshot::build_doi_index() if keypapers are given as DOIs.

The output construct is identical – ⁠nodes/⁠ and ⁠edges/⁠ partitioned the same way, readable by read_snowball() – but the node columns differ, because snapshot records are not API records. Snapshot nodes carry whatever the works corpus holds plus oa_input and relation; there is no page column, which is an API pagination artefact. Results are also frozen at the snapshot's vintage rather than live.

max_results

Snapshot mode only: refuse to expand a keypaper with more citing works than this. A heavily cited work can have hundreds of thousands of citers, and extracting records for all of them would read most of the corpus.

workers

Number of parallel workers. Default 1, which is sequential and reproduces the previous behaviour exactly.

Both paths gain from raising it, but in different places. API mode parallelises across the chunked query URLs – pro_query() chunks cites/cited_by at 50 ids, so a snowball over many keypapers becomes many URLs that were previously fetched one at a time – and across the JSON-to-parquet conversion. Snapshot mode parallelises reading the corpus files that the node set is scattered over, which is the dominant cost once the result set is large.

Raising it buys little for a snowball over a handful of keypapers, where fixed costs dominate; it matters at hundreds or thousands. Be aware that in API mode more workers means more concurrent requests, so keep it within the OpenAlex rate limit for your key.

chunk_limit

API mode only: how many keypaper ids go into one filter URL. NULL (the default) derives a value from workers.

openalexPro::pro_query() splits cites/cited_by filters into URLs of chunk_limit ids, and pro_request() fetches those URLs in parallel – but pro_query() has no knowledge of workers, so the fixed 50-id default can leave workers idle: 100 keypapers over 6 workers is two chunks and four idle processes. The derived value targets roughly twice as many chunks as workers, giving the scheduler slack to balance with, since chunks are split by id count while cost follows result volume.

At workers = 1 the derived value is 50, so behaviour is unchanged.

select

Snapshot mode only: which node columns to keep. NULL (the default) keeps every column the corpus holds, matching the API path.

This is the single biggest cost in snapshot mode. Works records carry ~51 columns of deeply nested structs, and extracting all of them dominates the run: measured over 427 nodes, 177.8 s for all columns against 16.7 s for three – a 10.6x difference, or 20.3x combined with workers = 6. If you only need the citation graph and a little metadata, name those columns. abstract alone is 11.12 GB of the corpus and is worth dropping on its own. See the pooling section below for why this matters and what else to do about it.

id and referenced_works are always retained regardless: the first identifies nodes, the second is what the edge extraction unnests.

endpoint

API mode only: base URL of the OpenAlex API. Defaults to "https://api.openalex.org".

Point this at a self-hosted OpenAlex instance (OurResearch publish the production stack as ourresearch/openalex-elastic-api) or at a mock server in tests. The API semantics are unchanged – the same cites / cited_by filters and cursor pagination – so only the host differs, and a self-hosted instance carries no rate limit, which is what makes large workers values worthwhile.

Trailing slashes are stripped. Ignored in snapshot mode, where no request is made.

duckdb_config

DuckDB settings for the two connections this function opens. NULL (the default) uses snowball_duckdb_config()'s defaults, optionally overridden field-by-field by getOption("openalexSnowball.duckdb_config").

The defaults matter at scale. DuckDB's own are a memory_limit of ~80% of system RAM per instance and a spill directory relative to the working directory – both wrong when several pro_snowball() calls run from a worker pool. Declare how many you run at once and the budget is divided accordingly:

options(openalexSnowball.duckdb_config = list(concurrency = 4))
resume

Continue an interrupted run in an existing output instead of deleting it. Default FALSE, which preserves the previous behaviour exactly.

Stages already completed are skipped, and within the fetch stage only the query chunks that did not finish are re-requested. Resuming with different keypapers, snapshot, endpoint, max_results or select is an error: it would merge two different snowballs into one output. workers, verbose and duckdb_config may differ freely, which is the point – "resume with fewer workers and a smaller memory limit" is the usual reason to resume.

Note that the default output lives under tempdir() and is removed when the R session ends, so cross-session resume needs an explicit, persistent ⁠output =⁠.

keep_intermediates

Keep the ⁠*_json⁠ / ⁠*_parquet⁠ working directories instead of deleting them once ⁠edges/⁠ is written. Default FALSE. Useful for debugging or for repeated resumes.

output

parquet dataset; default: temporary directory.

verbose

Logical indicating whether to show a verbose information. Defaults to FALSE

Value

The folder of the results containing multiple subfolders.

Snapshot mode – pool your keypapers into one call

In snapshot mode almost all the time goes on retrieving the node records, not on finding which works the snowball contains. Measured over 100 random keypapers against the full corpus:

phase time share
get_citing() -- citation index 7.3 s 7%
get_cited() -- referenced_works 1.2 s 1%
record retrieval 95.5 s 92%

The indexes are not the bottleneck, and neither is the id-to-location lookup. The cost is the scattered read that follows it. Those 100 keypapers produced 3,121 nodes living in 1,222 of the corpus's 2,127 parquet files – 57% of the corpus, at an average of 2.6 wanted rows per file opened. Each file still costs an open, a footer parse and a row-group decompression.

Two things follow.

Name the columns you need. ⁠select=⁠ cannot reduce how many files are opened, but it cuts the bytes decompressed inside each one, which is where its large speed-up comes from.

Pass all keypapers to one call rather than looping. The file set saturates: it cannot exceed 2,127 however many keypapers you give, and 100 keypapers already reach 1,222 of them. Cost is therefore strongly concave in the number of keypapers – ten times the keypapers costs roughly twice the time, not ten times. A loop of 200 separate calls re-pays the scattered-read cost 200 times over largely the same files:

# avoid -- pays the corpus-wide scattered read 200 times
for (grp in groups) pro_snowball(identifier = grp, snapshot = snap, ...)

# prefer -- one scattered read, one deduplicated result
pro_snowball(identifier = unlist(groups), snapshot = snap, ...)

The caveat is that pooling produces one snowball. oa_input, relation, is_keypaper / is_citing / is_cited and edge_type are all defined relative to the keypaper set of that call, so a pooled run cannot tell you which of your original groups a given node came from. If you need the groups kept apart, pool anyway and re-derive membership afterwards by joining the edges back to each group's keypaper ids – that is far cheaper than re-reading the corpus per group.

This applies only to ⁠snapshot=⁠. API mode is network-bound and scales roughly linearly in keypapers; see workers and chunk_limit there.

Node roles

id is a key in ⁠nodes/⁠: every OpenAlex work appears exactly once, whichever backend produced it. A work can nonetheless hold more than one role in the same snowball – a keypaper that cites another keypaper is both keypaper and citing – so the roles are recorded as three independent flags:

  • is_keypaper – the work is one of the supplied keypapers.

  • is_citing – the work cites at least one keypaper.

  • is_cited – the work is cited by at least one keypaper.

Any combination can be TRUE, and at least one always is. The flags describe a work's role in this snowball, not a property of the work.

sb <- read_snowball(out, return_data = TRUE)

subset(sb$nodes, is_citing & is_cited)   # works in both directions
subset(sb$nodes, !is_keypaper)           # the snowballed neighbourhood

relation is kept alongside them and is the hive partition key of ⁠nodes/⁠, but it records only the highest-precedence role (keypaper > citing > cited) and is therefore lossy: a work that is both citing and cited reports relation = "citing", so relation == "cited" does not find every cited work. Filter on the booleans; use relation only for partition pruning.

Both backends behave identically here – the flags are computed at node assembly, after the API or snapshot rows have been collected.


A function to extract the edges from a parquet database containing the nodes

Description

A function to extract the edges from a parquet database containing the nodes

Usage

pro_snowball_extract_edges(
  nodes = NULL,
  output = tempfile(fileext = ".snowball"),
  duckdb_config = NULL,
  verbose = FALSE
)

Arguments

nodes

Path to the nodes parquet dataset

output

output folder, in which the parquet database containing the edges called edges will be savedp default: temporary directory.

duckdb_config

DuckDB settings for the edge-extraction connection. See snowball_duckdb_config().

verbose

Logical indicating whether to show a verbose information. Defaults to FALSE

Value

A list containing 2 elements:

  • nodes: dataframe with publication records. The last column oa_input indicates whether the work was one of the input identifier(s).

  • edges: publication link dataframe of 2 columns ⁠from, to⁠ such that a row ⁠A, B⁠ means A -> B means A cites B. In bibliometrics, the "citation action" comes from A to B.

Examples

## Not run: 

snowball_docs <- pro_snowball(
   identifier = c("W2741809807", "W2755950973"),
   citing_params = list(from_publication_date = "2022-01-01"),
   cited_by_params = list(),
   verbose = TRUE
)

# Identical to above, but searches using paper DOIs

snowball_docs_doi <- oa_snowball(
   doi = c("10.1016/j.joi.2017.08.007", "10.7717/peerj.4375"),
   citing_params = list(from_publication_date = "2022-01-01"),
   cited_by_params = list(),
   verbose = TRUE
)

## End(Not run)

A function to get the nodes for a snowball search

Description

A function to get the nodes for a snowball search

Usage

pro_snowball_get_nodes(
  identifier = NULL,
  doi = NULL,
  limit = NULL,
  snapshot = NULL,
  max_results = 100000L,
  workers = 1L,
  chunk_limit = NULL,
  select = NULL,
  endpoint = "https://api.openalex.org",
  duckdb_config = NULL,
  resume = FALSE,
  cleanup = TRUE,
  prepared = FALSE,
  output = tempfile(fileext = ".snowball"),
  verbose = FALSE
)

Arguments

identifier

Character vector of openalex identifiers.

doi

Character vector of dois.

limit

If citedOnly only works cited by the keypaper are retrieved, citingOnly retrieves only works citing the keypaper. Default: NULL where all will be retrieved. 'none' is equal to NULL

snapshot

Path to a local OpenAlex snapshot (either a root directory containing ⁠parquet/⁠, or the ⁠parquet/⁠ directory itself). When supplied, nodes are gathered offline from the snapshot instead of the OpenAlex API. Requires the indexes built by openalexSnapshot::build_corpus_index() and build_citation_index(). Default NULL (use the API).

max_results

Snapshot mode only: refuse to expand a keypaper with more citing works than this. See openalexSnapshot::get_citing().

workers

Number of parallel workers. Default 1 (sequential).

chunk_limit

API mode only: ids per filter URL. NULL (default) derives one from workers; see pro_snowball().

select

Snapshot mode only: node columns to keep. See pro_snowball().

endpoint

API mode only: base URL of the OpenAlex API. See pro_snowball().

duckdb_config

DuckDB settings for the assembly connection. See snowball_duckdb_config().

resume

Keep an existing output and continue from where a previous run stopped, instead of deleting it. Default FALSE.

cleanup

Remove the intermediate ⁠*_json⁠ / ⁠*_parquet⁠ directories once the nodes are assembled. Default TRUE. pro_snowball() passes FALSE and cleans up after edge extraction instead, so that a failure there is still resumable.

prepared

Internal. TRUE means the caller has already created or cleaned output and this function must not delete it.

output

parquet dataset; default: temporary directory.

verbose

Logical indicating whether to show a verbose information. Defaults to FALSE

Value

Path to the nodes parquet dataset


Read snowball from Parquet Dataset

Description

This function reads a snowball from Apache Parquet format and returns a list containing nodes and edges, which can be either Arrow Datasets or tibbles.

Usage

read_snowball(
  snowball = NULL,
  edge_type = c("core", "extended", "outside"),
  return_data = FALSE,
  shorten_ids = FALSE,
  meta = FALSE
)

Arguments

snowball

The directory of the Parquet files as poppulater by pro_snowball().

edge_type

type of the returned edges. Possible values are:

  • core: only edges from or to the keypapers are selected

  • extended, only edges between the nodes are selected (this includes core edges)

  • outside: only edges where either the from or the to is not in nodes multiple are allowed.

return_data

Logical indicating whether to return an ArrowObject representing the corpus (default) or a tibble containing the whole corpus shou,d be returned.

shorten_ids

If TRUE the ids will be shortened, i.e. the part ⁠https://openalex.org/⁠ will be removed

meta

Also return provenance as a third list element meta: how the snowball was produced (api or snapshot), the snapshot path and the vintage of the citation index it was built from, the resolved keypapers, and package versions. Defaults to FALSE, which keeps the return shape list(nodes, edges). The sidecar is written to disk either way.

Value

A list containing two elements: nodes and edges, which are either ArrowObject representing the corpus or tibbles containing the data (plus meta when meta = TRUE).

nodes has one row per OpenAlex work – id is a key – and carries the role columns described below.

Node roles

A work can hold more than one role in the same snowball: a keypaper that cites another keypaper is both a keypaper and a citing work, and a work can both cite one keypaper and be cited by another. Because id is a key, those roles cannot each get their own row, so they are three independent flags on the single row:

  • is_keypaper – the work is one of the supplied keypapers.

  • is_citing – the work cites at least one keypaper.

  • is_cited – the work is cited by at least one keypaper.

Any combination can be TRUE, and at least one always is. Filter on these:

sb <- read_snowball(path, return_data = TRUE)

subset(sb$nodes, is_citing)              # everything citing a keypaper
subset(sb$nodes, is_citing & is_cited)   # works in both directions
subset(sb$nodes, !is_keypaper)           # the snowballed neighbourhood

relation is also present and is the hive partition key of ⁠nodes/⁠, but it records only the highest-precedence role (keypaper > citing > cited). A work that is both citing and cited reports relation = "citing", so relation == "cited" does not find every cited work – is_cited does. Use relation for partition pruning and the booleans for filtering.

The flags describe a work's role in this snowball, not a property of the work: the same paper can be is_cited in one snowball and is_citing in another.


Configuration for the DuckDB connections used by a snowball

Description

pro_snowball() opens DuckDB twice – once to assemble ⁠nodes/⁠, once to extract ⁠edges/⁠. This builds the settings applied to both.

Usage

snowball_duckdb_config(
  memory_limit = "auto",
  temp_directory = "auto",
  threads = "auto",
  preserve_insertion_order = FALSE,
  partitioned_write_max_open_files = NULL,
  max_temp_directory_size = NULL,
  concurrency = 1L
)

Arguments

memory_limit

DuckDB memory_limit. "auto" (the default) derives a budget from physical RAM and concurrency; NULL leaves DuckDB's own default of ~80% of RAM; anything else is passed through (e.g. "8GB").

temp_directory

Root for DuckDB's spill files. "auto" (the default) uses a private directory under tempdir(). A path you supply is treated as a root: a ⁠<pid>-<output hash>/<stage>⁠ leaf is always appended, so setting one path for a whole worker pool stays safe. NULL leaves DuckDB's .tmp, which concurrent callers share – not recommended.

threads

⁠SET threads⁠. "auto" divides the available cores by concurrency; NULL leaves DuckDB's default.

preserve_insertion_order

Defaults to FALSE. Row order on disk is not part of this package's contract – read_snowball() sorts nodes by ⁠(desc(oa_input), id)⁠ and edges by ⁠(from, to)⁠ – and preserving it makes the partitioned writes buffer.

partitioned_write_max_open_files

NULL (default) leaves DuckDB's 100. Do not raise it here: ⁠nodes/⁠ and ⁠edges/⁠ have three partitions each, so 100 is already ~33x what is needed, and each open file carries a write buffer. (openalexSnapshot raises it to 512, but that write fans out over hundreds of partitions.)

max_temp_directory_size

NULL (default) leaves DuckDB's 90% of free disk.

concurrency

How many pro_snowball() calls you intend to run at the same time. Used to divide the memory and thread budgets. There is no portable way to detect sibling R processes, so this is declared rather than discovered – set it once for a worker pool: options(openalexSnowball.duckdb_config = list(concurrency = 4)).

Details

Settings resolve per field, not per object: an explicit argument field beats the same field of getOption("openalexSnowball.duckdb_config"), which beats the package default. So setting one field in the option does not silently discard the safe defaults for the others, and passing duckdb_config = list(threads = 2) at a call site does not discard a session-wide memory budget.

Note that memory_limit is a fairness and blast-radius control rather than a correctness one: lowering it does not make a query fit, it makes a failure arrive sooner and stops one run from starving the others.

Value

A list of class openalexSnowball_duckdb_config.

Examples

snowball_duckdb_config(memory_limit = "8GB", concurrency = 4)