Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
udpated vignette and readme, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Dec 22, 2016
1 parent b700cef commit c3911a7
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 180 deletions.
4 changes: 0 additions & 4 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ man-roxygen
CONTRIBUTING.md
README.html
Makefile
inst/vign/
vignettes/rbhl_vignette.md
vignettes/margins.sty
vignettes/cache/
^appveyor\.yml$
README.Rmd
NEWS.md
Expand Down
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ LazyData: true
VignetteBuilder: knitr
Imports:
httr (>= 1.1.0),
XML,
jsonlite (>= 0.9.19),
plyr,
tibble
xml2 (>= 1.0.0),
jsonlite (>= 1.1),
plyr (>= 1.8.4),
tibble (>= 1.2)
Suggests:
roxygen2 (>= 5.0.1),
testthat,
knitr,
covr
Expand Down
9 changes: 3 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@ export(bhl_partsearch)
export(bhl_subjectsearch)
export(bhl_titlesearchsimple)
export(getpages)
importFrom(XML,xmlParse)
importFrom(XML,xmlSize)
importFrom(XML,xmlValue)
importFrom(XML,xpathApply)
importFrom(XML,xpathSApply)
importFrom(httr,GET)
importFrom(httr,content)
importFrom(httr,stop_for_status)
importFrom(jsonlite,fromJSON)
importFrom(plyr,ldply)
importFrom(plyr,rbind.fill)
importFrom(xml2,read_xml)
importFrom(xml2,xml_find_all)
importFrom(xml2,xml_text)
7 changes: 3 additions & 4 deletions R/bhl_getsubjecttitles.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#' bhl_getsubjecttitles('diptera', 'json')
#' }

bhl_getsubjecttitles <- function(subject = NULL, as = "table", key = NULL, ...)
{
args <- bhlc(list(op = "GetSubjectTitles", apikey = check_key(key), format = as_f(as),
subject = subject))
bhl_getsubjecttitles <- function(subject = NULL, as = "table", key = NULL, ...) {
args <- bhlc(list(op = "GetSubjectTitles", apikey = check_key(key),
format = as_f(as), subject = subject))
bhl_GET(as, args, ...)
}
6 changes: 3 additions & 3 deletions R/bhl_gettitleitems.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' bhl_gettitleitems(1726, as='list')
#' }

bhl_gettitleitems <- function(titleid, as = "table", key = NULL, ...)
{
args <- bhlc(list(op = "GetTitleItems", apikey = check_key(key), format = as_f(as), titleid=titleid))
bhl_gettitleitems <- function(titleid, as = "table", key = NULL, ...) {
args <- bhlc(list(op = "GetTitleItems", apikey = check_key(key),
format = as_f(as), titleid = titleid))
bhl_GET(as, args, ...)
}
13 changes: 7 additions & 6 deletions R/getpages.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
#'
#' @export
#' @param itemid the item id (character). Required
#' @param key Your BHL API key, either enter, or loads from your \code{.Renviron} as \code{BHL_KEY}
#' or from \code{.Rprofile} as \code{bhl_key}.
#' @param key Your BHL API key, either enter, or loads from your
#' \code{.Renviron} as \code{BHL_KEY} or from \code{.Rprofile} as
#' \code{bhl_key}
#' @param ... Curl options passed on to \code{\link[httr]{GET}}
#' @examples \dontrun{
#' books <- bhl_booksearch(title='Selborne', lname='White', volume=2, edition='new', year=1825,
#' collectionid=4, language='eng')
#' getpages(itemid=16800)
#' books <- bhl_booksearch(title='Selborne', lname='White', volume=2,
#' edition='new', year=1825, collectionid=4, language='eng')
#' getpages(itemid = 16800)
#' }

getpages <- function(itemid, key = NULL, ...){
res <- bhl_getitempages(itemid, ...)
out <- lapply(res$data$PageID, function(x){
out <- lapply(res$PageID, function(x){
tmp <- bhl_getpageocrtext(page = x)
if (grepl("OCR text unavailable for this page", tmp))
NULL
Expand Down
11 changes: 6 additions & 5 deletions R/rbhl-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#' You need an API key to use the Biodiversity Heritage Library API. Get your
#' BHL API key at \url{http://www.biodiversitylibrary.org/getapikey.aspx}.
#' Put your API key in your .Rprofile file using e.g.,
#' `options(BioHerLibKey = "YOURBHLAPIKEY")`, and the functions within this package
#' will be able to use your API key without you having to enter it every time
#' you run a search.
#' `options(BioHerLibKey = "YOURBHLAPIKEY")`, and the functions within
#' this package will be able to use your API key without you having to enter
#' it every time you run a search.
#'
#' @importFrom httr GET content stop_for_status
#' @importFrom jsonlite fromJSON
#' @importFrom plyr ldply rbind.fill
#' @importFrom XML xmlSize xpathSApply xpathApply xmlParse xmlValue
#' @importFrom plyr rbind.fill
#' @importFrom xml2 xml_find_all read_xml xml_text
#' @name rbhl-package
#' @aliases rbhl
#' @docType package
Expand All @@ -20,6 +20,7 @@
NULL

#' Data.frame of all the BHL API methods from the BHL website.
#'
#' @name rbhlmethods
#' @docType data
#' @keywords datasets
Expand Down
25 changes: 1 addition & 24 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
query2message <- function(url, x) {
mylist <- list()
for (i in 1:length(x)) {
mylist[i] <- paste(names(x[i]), '=', x[i][[1]], sep = '')
}
paste('API call:\n', paste(url, "?", paste(mylist, collapse = "&"), sep = ''))
}

return_results <- function(x, y, z) {
if (y == 'raw') {
return( x )
} else if (y == 'list') {
return( jsonlite::fromJSON(I(x)) )
} else {
if (z == "json") {
return(jsonlite::fromJSON(I(x)))
} else {
return(XML::xmlTreeParse(I(x)))
}
}
}

check_key <- function(x) {
tmp <- if (is.null(x)) Sys.getenv("BHL_KEY", "") else x
if (tmp == "") getOption("bhl_key", stop("need an API key for BHL")) else tmp
Expand All @@ -29,7 +7,7 @@ bhl_GET <- function(as, args, ...){
out <- GET(bhl_url(), query = args, ...)
stop_for_status(out)
res <- switch(as,
xml = xmlSize(xpathSApply(xmlParse(content_utf8(out)), "//Result")[[1]]),
xml = length(xml_children(xml_find_all(read_xml(content_utf8(out)), "//Result"))),
json = length(jsonlite::fromJSON(content_utf8(out))$Result),
list = length(jsonlite::fromJSON(content_utf8(out))$Result),
table = length(jsonlite::fromJSON(content_utf8(out))$Result))
Expand All @@ -52,7 +30,6 @@ todf <- function(x){
} else {
do.call(rbind.fill, lapply(bhlc(temp), data.frame))
}
#structure(list(data = tmp), class = "bhldf")
tibble::as_data_frame(tmp)
}
}
Expand Down
97 changes: 57 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ The default is usually `table`:

```r
bhl_authorsearch(name='dimmock')
#> <bhl data> [2, 12]
#> CreatorID Name Role Numeration Unit Title Location
#> 1 1970 Dimmock, George, NA
#> 2 8126 Dimmock, George, NA
#> Variables not shown: FullerForm (chr), Relationship (lgl), TitleOfWork
#> (lgl), Dates (chr), CreatorUrl (chr)
#> # A tibble: 6 × 12
#> CreatorID Name Role Numeration Unit Title Location
#> * <int> <chr> <lgl> <chr> <chr> <chr> <chr>
#> 1 189035 Dimmock, Anna Katherina NA
#> 2 59023 Dimmock, G NA
#> 3 189042 Dimmock, Geo NA
#> 4 189021 Dimmock, George NA
#> 5 1970 Dimmock, George, NA
#> 6 8126 Dimmock, George, NA
#> # ... with 5 more variables: FullerForm <chr>, Relationship <lgl>,
#> # TitleOfWork <lgl>, Dates <chr>, CreatorUrl <chr>
```

list output
Expand All @@ -67,10 +72,10 @@ list output
```r
bhl_authorsearch(name='dimmock', as='list')$Result[[1]]
#> $CreatorID
#> [1] 1970
#> [1] 189035
#>
#> $Name
#> [1] "Dimmock, George,"
#> [1] "Dimmock, Anna Katherina"
#>
#> $Role
#> NULL
Expand All @@ -97,26 +102,26 @@ bhl_authorsearch(name='dimmock', as='list')$Result[[1]]
#> NULL
#>
#> $Dates
#> [1] "1852-"
#> [1] ""
#>
#> $CreatorUrl
#> [1] "http://www.biodiversitylibrary.org/creator/1970"
#> [1] "http://www.biodiversitylibrary.org/creator/189035"
```

XML output


```r
bhl_authorsearch(name='dimmock', as='xml')
#> [1] "<?xml version=\"1.0\" encoding=\"utf-8\"?><Response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Status>ok</Status><Result><Creator><CreatorID>1970</CreatorID><Name>Dimmock, George,</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates>1852-</Dates><CreatorUrl>http://www.biodiversitylibrary.org/creator/1970</CreatorUrl></Creator><Creator><CreatorID>8126</CreatorID><Name>Dimmock, George,</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates>1852-1930</Dates><CreatorUrl>http://www.biodiversitylibrary.org/creator/8126</CreatorUrl></Creator></Result></Response>"
#> [1] "<?xml version=\"1.0\" encoding=\"utf-8\"?><Response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Status>ok</Status><Result><Creator><CreatorID>189035</CreatorID><Name>Dimmock, Anna Katherina</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates /><CreatorUrl>http://www.biodiversitylibrary.org/creator/189035</CreatorUrl></Creator><Creator><CreatorID>59023</CreatorID><Name>Dimmock, G</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates /><CreatorUrl>http://www.biodiversitylibrary.org/creator/59023</CreatorUrl></Creator><Creator><CreatorID>189042</CreatorID><Name>Dimmock, Geo </Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates /><CreatorUrl>http://www.biodiversitylibrary.org/creator/189042</CreatorUrl></Creator><Creator><CreatorID>189021</CreatorID><Name>Dimmock, George</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates /><CreatorUrl>http://www.biodiversitylibrary.org/creator/189021</CreatorUrl></Creator><Creator><CreatorID>1970</CreatorID><Name>Dimmock, George,</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates>1852-</Dates><CreatorUrl>http://www.biodiversitylibrary.org/creator/1970</CreatorUrl></Creator><Creator><CreatorID>8126</CreatorID><Name>Dimmock, George,</Name><Numeration /><Unit /><Title /><Location /><FullerForm /><Dates>1852-1930</Dates><CreatorUrl>http://www.biodiversitylibrary.org/creator/8126</CreatorUrl></Creator></Result></Response>"
```

JSON output


```r
bhl_authorsearch(name='dimmock', as='json')
#> [1] "{\"Status\":\"ok\",\"ErrorMessage\":null,\"Result\":[{\"CreatorID\":1970,\"Name\":\"Dimmock, George,\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"1852-\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/1970\"},{\"CreatorID\":8126,\"Name\":\"Dimmock, George,\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"1852-1930\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/8126\"}]}"
#> [1] "{\"Status\":\"ok\",\"ErrorMessage\":null,\"Result\":[{\"CreatorID\":189035,\"Name\":\"Dimmock, Anna Katherina\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/189035\"},{\"CreatorID\":59023,\"Name\":\"Dimmock, G\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/59023\"},{\"CreatorID\":189042,\"Name\":\"Dimmock, Geo \",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/189042\"},{\"CreatorID\":189021,\"Name\":\"Dimmock, George\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/189021\"},{\"CreatorID\":1970,\"Name\":\"Dimmock, George,\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"1852-\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/1970\"},{\"CreatorID\":8126,\"Name\":\"Dimmock, George,\",\"Role\":null,\"Numeration\":\"\",\"Unit\":\"\",\"Title\":\"\",\"Location\":\"\",\"FullerForm\":\"\",\"Relationship\":null,\"TitleOfWork\":null,\"Dates\":\"1852-1930\",\"CreatorUrl\":\"http://www.biodiversitylibrary.org/creator/8126\"}]}"
```

## Get title metadata
Expand Down Expand Up @@ -146,9 +151,18 @@ bhl_gettitlemetadata(titleid = 1726, items = TRUE, as="list")$Result$Items
#> [[1]]$Year
#> NULL
#>
#> [[1]]$CopySpecificInformation
#> NULL
#>
#> [[1]]$Contributor
#> [1] "MBLWHOI Library"
#>
#> [[1]]$RightsHolder
#> NULL
#>
#> [[1]]$ScanningInstitution
#> NULL
#>
#> [[1]]$Sponsor
#> [1] "MBLWHOI Library"
#>
Expand Down Expand Up @@ -197,24 +211,26 @@ bhl_gettitlemetadata(titleid = 1726, items = TRUE, as="list")$Result$Items

```r
bhl_booksearch(title='Selborne', lname='White', volume=2, edition='new', year=1825, collectionid=4, language='eng')
#> <bhl data> [1, 22]
#> # A tibble: 1 × 22
#> TitleID BibliographicLevel
#> * <int> <chr>
#> 1 32868
#> Variables not shown: FullTitle (chr), ShortTitle (lgl), SortTitle (lgl),
#> PartNumber (chr), PartName (chr), CallNumber (lgl), Edition (chr),
#> PublisherPlace (chr), PublisherName (chr), PublicationDate (chr),
#> PublicationFrequency (lgl), Doi (lgl), TitleUrl (chr), Authors
#> (list), Subjects (lgl), Identifiers (lgl), Collections (lgl),
#> Variants (lgl), Items (list), Notes (lgl)
#> # ... with 20 more variables: FullTitle <chr>, ShortTitle <lgl>,
#> # SortTitle <lgl>, PartNumber <chr>, PartName <chr>, CallNumber <lgl>,
#> # Edition <chr>, PublisherPlace <chr>, PublisherName <chr>,
#> # PublicationDate <chr>, PublicationFrequency <lgl>, Doi <lgl>,
#> # TitleUrl <chr>, Authors <list>, Subjects <lgl>, Identifiers <lgl>,
#> # Collections <lgl>, Variants <lgl>, Items <list>, Notes <lgl>
```

## Search titles


```r
bhl_titlesearchsimple('husbandry')
#> <bhl data> [151, 22]
#> # A tibble: 153 × 22
#> TitleID BibliographicLevel
#> * <int> <chr>
#> 1 25997 Monograph/Item
#> 2 44403 Monograph/Item
#> 3 27062 Monograph/Item
Expand All @@ -225,33 +241,34 @@ bhl_titlesearchsimple('husbandry')
#> 8 58205 Monograph/Item
#> 9 51946 Monograph/Item
#> 10 55665 Monograph/Item
#> .. ... ...
#> Variables not shown: FullTitle (chr), ShortTitle (chr), SortTitle (chr),
#> PartNumber (chr), PartName (chr), CallNumber (lgl), Edition (chr),
#> PublisherPlace (chr), PublisherName (chr), PublicationDate (chr),
#> PublicationFrequency (chr), Doi (lgl), TitleUrl (lgl), Authors (lgl),
#> Subjects (lgl), Identifiers (lgl), Collections (lgl), Variants (lgl),
#> Items (lgl), Notes (lgl)
#> # ... with 143 more rows, and 20 more variables: FullTitle <chr>,
#> # ShortTitle <chr>, SortTitle <chr>, PartNumber <chr>, PartName <chr>,
#> # CallNumber <lgl>, Edition <chr>, PublisherPlace <chr>,
#> # PublisherName <chr>, PublicationDate <chr>,
#> # PublicationFrequency <chr>, Doi <lgl>, TitleUrl <lgl>, Authors <lgl>,
#> # Subjects <lgl>, Identifiers <lgl>, Collections <lgl>, Variants <lgl>,
#> # Items <lgl>, Notes <lgl>
```

## Get languages


```r
bhl_getlanguages()
#> <bhl data> [68, 2]
#> LanguageCode LanguageName
#> 1 AFR Afrikaans
#> 2 ARA Arabic
#> 3 ARC Aramaic
#> 4 BUL Bulgarian
#> 5 BUR Burmese
#> 6 CAR Carib
#> 7 CAT Catalan
#> 8 CEL Celtic (Other)
#> 9 CHI Chinese
#> 10 HRV Croatian
#> .. ... ...
#> # A tibble: 69 × 2
#> LanguageCode LanguageName
#> * <chr> <chr>
#> 1 AFR Afrikaans
#> 2 ARA Arabic
#> 3 ARC Aramaic
#> 4 MAP Austronesian (Other)
#> 5 BUL Bulgarian
#> 6 BUR Burmese
#> 7 CAR Carib
#> 8 CAT Catalan
#> 9 CEL Celtic (Other)
#> 10 CHI Chinese
#> # ... with 59 more rows
```

## Meta
Expand Down
4 changes: 2 additions & 2 deletions inst/vign/rbhl_vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ knitr::opts_chunk$set(
comment = "#>",
warning = FALSE,
message = FALSE,
cache = TRUE
cache = FALSE
)
```

rbhl Introduction
======
=================

`rbhl` is an R interface to the Biodiversity Heritage Library.

Expand Down
Loading

0 comments on commit c3911a7

Please sign in to comment.