Learn R Programming

handlr (version 0.3.1)

HandlrClient: HandlrClient

Description

handlr client, read and write to and from all citation formats

Arguments

Public fields

path

(character) non-empty if file path passed to initialize

string

(character) non-empty if string (non-file) passed to initialize

parsed

after read() is run, the parsed content

file

(logical) TRUE if a file passed to initialize, else FALSE

ext

(character) the file extension

format_guessed

(character) the guessed file format

doi

(character) the DOI, if any found

Methods


Method print()

print method for HandlrClient objects

Usage

HandlrClient$print(x, ...)

Arguments

x

self

...

ignored


Method new()

Create a new HandlrClient object

Usage

HandlrClient$new(x, format = NULL, ...)

Arguments

x

(character) a file path (the file must exist), a string containing contents of the citation, a DOI, or a DOI as a URL. See Details.

format

(character) one of citeproc, ris, bibtex, codemeta, cff, or NULL. If NULL, we attempt to guess the format, and error if we can not guess

...

curl options passed on to crul::verb-GET

Returns

A new HandlrClient object


Method read()

read input

Usage

HandlrClient$read(format = NULL, ...)

Arguments

format

(character) one of citeproc, ris, bibtex, codemeta, cff, or NULL. If NULL, we attempt to guess the format, and error if we can not guess

...

further args to the writer fxn, if any


Method write()

write to std out or file

Usage

HandlrClient$write(format, file = NULL, ...)

Arguments

format

(character) one of citeproc, ris, bibtex, schema_org, rdfxml, codemeta, or cff

file

a file path, if NULL to stdout. for format=ris, number of files must equal number of ris citations

...

further args to the writer fxn, if any


Method as_df()

convert data to a data.frame using handl_to_df()

Usage

HandlrClient$as_df()

Returns

a data.frame


Method clone()

The objects of this class are cloneable with this method.

Usage

HandlrClient$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

The various inputs to the x parameter are handled in different ways:

  • file: contents read from file, we grab file extension, and we guess format based on combination of contents and file extension because file extensions may belie what's in the file

  • string: string read in, and we guess format based on contents of the string

  • DOI: we request citeproc-json format from the Crossref API

  • DOI url: we request citeproc-json format from the Crossref API

Examples

Run this code
# read() can be run with format specified or not
# if format not given, we attempt to guess the format and then read
z <- system.file('extdata/citeproc.json', package = "handlr")
(x <- HandlrClient$new(x = z))
x$read()
x$read("citeproc")
x$parsed

# you can run read() then write()
# or just run write(), and read() will be run for you if possible
z <- system.file('extdata/citeproc.json', package = "handlr")
(x <- HandlrClient$new(x = z))
cat(x$write("ris"))

# read from a DOI as a url
if (interactive()) {
  (x <- HandlrClient$new('https://doi.org/10.7554/elife.01567'))
  x$parsed
  x$read()
  x$parsed
  x$write('bibtex')
}

# read from a DOI
if (interactive()) {
  (x <- HandlrClient$new('10.7554/elife.01567'))
  x$parsed
  x$read()
  x$write('bibtex')
}

# read in citeproc, write out bibtex
z <- system.file('extdata/citeproc.json', package = "handlr")
(x <- HandlrClient$new(x = z))
x$path
x$ext
x$read("citeproc")
x$parsed
x$write("bibtex")
f <- tempfile(fileext = ".bib")
x$write("bibtex", file = f)
readLines(f)
unlink(f)

# read in ris, write out ris
z <- system.file('extdata/peerj.ris', package = "handlr")
(x <- HandlrClient$new(x = z))
x$path
x$format_guessed
x$read("ris")
x$parsed
x$write("ris")
cat(x$write("ris"))

# read in bibtex, write out ris
(z <- system.file('extdata/bibtex.bib', package = "handlr"))
(x <- HandlrClient$new(x = z))
x$path
x$format_guessed
if (requireNamespace("bibtex", quietly = TRUE)) {
x$read("bibtex")
x$parsed
x$write("ris")
cat(x$write("ris"))
}

# read in bibtex, write out RDF XML
if (requireNamespace("bibtex", quietly = TRUE) && interactive()) {
  (z <- system.file('extdata/bibtex.bib', package = "handlr"))
  (x <- HandlrClient$new(x = z))
  x$path
  x$format_guessed
  x$read("bibtex")
  x$parsed
  x$write("rdfxml")
  cat(x$write("rdfxml"))
}

# codemeta
(z <- system.file('extdata/codemeta.json', package = "handlr"))
(x <- HandlrClient$new(x = z))
x$path
x$format_guessed
x$read("codemeta")
x$parsed
x$write("codemeta")

# cff: Citation File Format
(z <- system.file('extdata/citation.cff', package = "handlr"))
(x <- HandlrClient$new(x = z))
x$path
x$format_guessed
x$read("cff")
x$parsed
x$write("codemeta")

# > 1 citation
z <- system.file('extdata/citeproc-many.json', package = "handlr")
(x <- HandlrClient$new(x = z))
x$parsed
x$read()
x$parsed
## schmea org
x$write("schema_org")
## bibtex
x$write("bibtex")
## bibtex to file
f <- tempfile(fileext=".bib")
x$write("bibtex", f)
readLines(f)
unlink(f)
## to RIS
x$write("ris")
### only one per file, so not combined
files <- replicate(2, tempfile(fileext=".ris"))
x$write("ris", files)
lapply(files, readLines)

# handle strings instead of files
z <- system.file('extdata/citeproc-crossref.json', package = "handlr")
(x <- HandlrClient$new(x = readLines(z)))
x$read("citeproc")
x$parsed
cat(x$write("bibtex"), sep = "\n")

Run the code above in your browser using DataLab