API requests with httr2

R
API
Published

November 20, 2023

httr2 1.0 was recently released. I used it earlier this month in my API explorations to understand what the Python requests equivalent is in R. Python and requests are so convenient that I had not used R yet to access an API.

I became aware of Semantic Scholar, a database of scientific literature, with a nice API.

So here is an exploration of my Semantic Scholar records (ID 51362391) in R with httr2.

Code
library(tidyverse)
library(httr2)
library(jsonlite)

API request

API · Fetch paper and author data

author_id <- 51362391

url <-
  str_glue(
    "https://api.semanticscholar.org/graph/v1/author/{author_id}",
    "?fields=name,papers,papers.year,papers.title,papers.citationCount,papers.authors"
  )

resp <-
  request(url) |>
  req_perform()
dt <-
  resp_body_string(resp) |>
  fromJSON()

print(dt[["name"]])
[1] "Holger Döring"

Publications

papers <-
  dt[["papers"]] |>
  mutate(
    id_short = str_sub(paperId, 1, 5),
    title_short = str_sub(title, 1, 30),
    authors_n = map_int(authors, \(.x) nrow(.x))
  )

Number of publications at Semantic Scholar

nrow(papers)
[1] 24

Publications

papers |>
  select(id_short, year, `cita.` = citationCount, `auth.` = authors_n, title_short)
id_short year cita. auth. title_short
a9408 2022 0 1 Formal binary logic, tidal for
c229b 2022 0 1 Beyond the light-barrier -fast
e3034 2022 0 1 Occams Razor-No Dark Matter in
37135 2021 0 1 Lorentzian SRT-Transformation
84d1f 2021 1 3 Germany: From Stable Coalition
8bd34 2020 9 3 Institutional constraints on c
1b9e9 2019 11 2 Party Positions from Wikipedia
e9e28 2019 51 2 Party Facts: A database of pol
f93aa 2019 0 1 Gathering and Managing Data on
69fe5 2016 5 1 Mapping established democracie
4175b 2015 27 2 Is Proportional Representation
85957 2015 0 3 How do European democracies co
a9372 2015 32 2 Revisiting the left cabinet sh
34e81 2014 1 2 Jean-Claude Juncker’s new Euro
1273f 2013 61 2 Who Gets into Government? Coal
31eff 2013 31 1 The collective action of data
c01fd 2012 2 3 Ministerial Careers and ’The M
c4f64 2011 0 2 Why are States with Pr More Re
e3cbe 2010 0 1 Collaborative Data Collection
f5948 2009 0 1 Political representation in Eu
2ebbf 2008 37 2 Electoral and Mechanical Cause
ec86a 2008 1 1 Evaluating Scripting Languages
14ab0 2007 70 1 The Composition of the College
9674d 2006 3 2 Divided Government European St

Authors

Authors by paper

authors <-
  papers |>
  select(id_short, year, authors) |>
  unnest(authors)

authors
id_short year authorId name
a9408 2022 51362391 Holger Döring
c229b 2022 51362391 Holger Döring
e3034 2022 51362391 Holger Döring
37135 2021 51362391 Holger Döring
84d1f 2021 94263396 M. Debus
84d1f 2021 51362391 Holger Döring
84d1f 2021 143708020 Alejandro Ecker
8bd34 2020 119358998 Maria Thürk
8bd34 2020 145985059 Johan Hellström
8bd34 2020 51362391 Holger Döring
1b9e9 2019 145315358 M. Herrmann
1b9e9 2019 51362391 Holger Döring
e9e28 2019 51362391 Holger Döring
e9e28 2019 1685592 Sven Regel
f93aa 2019 51362391 Holger Döring
69fe5 2016 51362391 Holger Döring
4175b 2015 51362391 Holger Döring
4175b 2015 2072035416 P. Manow
85957 2015 144008442 Jonathan Bright
85957 2015 51362391 Holger Döring
85957 2015 145474393 Conor Little
a9372 2015 51362391 Holger Döring
a9372 2015 115016266 Hanna Schwander
34e81 2014 82184129 A. Wonka
34e81 2014 51362391 Holger Döring
1273f 2013 51362391 Holger Döring
1273f 2013 145985059 Johan Hellström
31eff 2013 51362391 Holger Döring
c01fd 2012 144008442 Jonathan Bright
c01fd 2012 51362391 Holger Döring
c01fd 2012 145474393 Conor Little
c4f64 2011 2072035416 P. Manow
c4f64 2011 51362391 Holger Döring
e3cbe 2010 51362391 Holger Döring
f5948 2009 51362391 Holger Döring
2ebbf 2008 2072035416 P. Manow
2ebbf 2008 51362391 Holger Döring
ec86a 2008 51362391 Holger Döring
14ab0 2007 51362391 Holger Döring
9674d 2006 2072035416 P. Manow
9674d 2006 51362391 Holger Döring