#' Browse a set of Hathi Trust IDs interactively at the Hathi Trust digital
#' library site
#'
#' @param htids A workset generated by [workset_builder], a character vector of
#' Hathi Trust IDs, or a data frame with a column named "htid" (and possibly
#' one named "id" as well for the page sequence).
#'
#' @return Opens a browser window at https://babel.hathitrust.org/ to the
#' specified Hathi Trust Id (and page, if given). If there is more than one
#' HTID in the workset, it will prompt you to open the next one.
#' @export
#' @importFrom utils browseURL
#'
#' @examples
#' \donttest{
#' if(interactive()) {
#' workset <- workset_builder(name = "Alexis de Tocqueville", pub_date = 1800:1850)
#' browse_htids(workset)
#' }
#' }
browse_htids <- function(htids) {
if(!interactive()) {
stop("This function can only be used in interactive mode")
}
UseMethod("browse_htids")
}
#' @export
browse_htids.hathi_workset <- function(htids) {
for(i in 1:nrow(htids)) {
id <- as.character(htids[i, 1])
if("id" %in% names(htids)) {
seq <- as.character(htids[i, 2]) %>%
stringr::str_remove(".+page-0?") %>%
as.numeric()
seq <- seq + 1
url <- stringr::str_glue("https://babel.hathitrust.org/cgi/pt?id={id}&seq={seq}")
} else {
url <- stringr::str_glue("https://babel.hathitrust.org/cgi/pt?id={id}")
}
browseURL(url)
resp <- ""
while(resp != "n") {
resp <- readline(stringr::str_glue("Press n to view next htid in browser, q to quit: "))
if(resp == "q") {
return(invisible())
}
}
}
}
#' @export
browse_htids.character <- function(htids) {
for(i in 1:length(htids)) {
id <- as.character(htids[i])
url <- stringr::str_glue("https://babel.hathitrust.org/cgi/pt?id={id}")
browseURL(url)
resp <- ""
while(resp != "n") {
resp <- readline(stringr::str_glue("Press n to view next htid in browser, q to quit: "))
if(resp == "q") {
return(invisible())
}
}
}
}
#' @export
browse_htids.data.frame <- function(htids) {
stopifnot(any(c("htid", "id") %in% names(htids)))
htids <- htids %>%
dplyr::select(dplyr::any_of(c("htid", "id")))
browse_htids.hathi_workset(htids)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.