R/arts_images.R

Defines functions arts_images

Documented in arts_images

#' arts_images
#' extract the primary picture of artworks matching parameters
#' Input the objectIDs from the previous function that you're interested
#' @param objectIDs eg. c(13875, 229930) for multiple IDs
#'
#' @return A table with the object ID, name, and photo
#'
#' @importFrom utils head write.csv
#' @export


arts_images <- function(objectIDs) {

  ## collect urls
  urls <- stringr::str_c("https://collectionapi.metmuseum.org/public/collection/v1/objects/", objectIDs)

  ## collect raw observations
  obs <- c()
  for(url in urls){
    request_result <- GET(url)

    ## check if there is a false ID that doesn't exist
    if(httr::http_error(request_result)){
      falseID <- stringr::str_sub(url, 66, )
      warning("objectID ")
      cat(falseID)
      warning(" does not exist! Check your typo :D
")
    } else {
      cont <- httr::content(request_result)
      obs[[length(obs)+1]] <- unlist(cont)
    }
  }


  ## collect focused observations
  images <- c()
  for (i in 1:length(obs)) {
    df <- obs[[i]] %>%
      t() %>%
      as.data.frame()

    images[[length(images)+1]] <- df[ , c("objectID",
                                          "objectName",
                                          "primaryImageSmall")]
  }

  ## neat dataframe
  table <- do.call(rbind, images)

  ## build links to images
  table$primaryImageSmall <- stringr::str_c("![](", table$primaryImageSmall, ")")

  ## print
  table %>% knitr::kable()

}
whj0911/Mets documentation built on Dec. 16, 2019, 12:49 a.m.