Skip to content

Instantly share code, notes, and snippets.

@smach
Created January 12, 2025 15:05
Show Gist options
  • Save smach/97a11b8fb15770be97a0e124f7de4c60 to your computer and use it in GitHub Desktop.
Save smach/97a11b8fb15770be97a0e124f7de4c60 to your computer and use it in GitHub Desktop.
Search US Library of Congress images with R
# Uses httr and jsonlite packages and optional DT to display results
library(httr)
library(jsonlite)
search_loc_images <- function(query, max_results = 10) {
# Base URL
base_url <- "https://www.loc.gov/photos/"
# Query parameters
params <- list(
q = query,
fo = "json",
rights = "public",
c = max_results
)
# Send GET request
response <- GET(url = base_url, query = params)
# Check status code
if (status_code(response) == 200) {
# Parse JSON content
data <- content(response, as = "parsed", type = "application/json")
# Check if 'results' exist and has length > 0
if (!is.null(data$results) && length(data$results) > 0) {
# Extract relevant info
results_list <- lapply(data$results, function(item) {
list(
Title = if (!is.null(item$title)) item$title else "N/A",
Date = if (!is.null(item$date)) item$date else "N/A",
Description = if (!is.null(item$description) && length(item$description) > 0)
item$description[[1]]
else
"N/A",
`Image URL` = if (!is.null(item$image_url) && length(item$image_url) > 0)
item$image_url[[1]]
else
"N/A",
`LOC URL` = if (!is.null(item$url)) item$url else "N/A"
)
})
# Convert list of lists to a data frame
df <- do.call(rbind.data.frame, results_list)
return(df)
} else {
message("No results found.")
return(NULL)
}
} else {
message(paste("Request failed with status code", status_code(response)))
return(NULL)
}
}
# Example search:
results_df <- search_loc_images("New York City skyline", max_results = 10)
if (!is.null(results_df)) {
print(results_df)
datatable(results_df, escape = TRUE, options = list(pageLength = 10), filter = 'top')
} else {
cat("No data returned.\n")
}
# If you want a nice searchable table
library(dplyr)
library(DT)
results_df_for_table <- results_df |>
mutate(
Thumbnail = paste0('<img src="', Image.URL, '" width="100" height="100">'),
Thumbnail = paste0('<a href="', LOC.URL, '" target="_blank">', Thumbnail, '</a>'),
Title = paste0('<a href="', LOC.URL, '" target="_blank">', Title, '</a>')
) |>
select(Title, Thumbnail, Date, Description )
if (!is.null(results_df)) {
datatable(results_df_for_table, escape = FALSE, options = list(pageLength = 10), filter = 'top')
} else {
cat("No data returned.\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment