Skip to content

Commit

Permalink
#ropensci review #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ottlngr committed Jun 26, 2017
1 parent bf8df39 commit 2f38c20
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 180 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bib2df
Type: Package
Title: Parse a BibTeX File to a data.frame
Version: 0.2
Version: 0.2.1
Authors@R: c(person("Philipp", "Ottolinger", email = "philipp@ottolinger.de", role = c("aut", "cre")),
person("Thomas", "Leeper", email = "thosjleeper@gmail.com", role = "ctb"),
person("Maëlle", "Salmon", email = "maelle.salmon@yahoo.se", role = "ctb"))
Expand All @@ -12,7 +12,6 @@ License: GPL-3
LazyData: TRUE
Imports:
dplyr,
plyr,
stringr,
humaniformat
Suggests:
Expand Down
5 changes: 1 addition & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(bib2df)
export(bib2df_gather)
export(bib2df_read)
export(bib2df_tidy)
export(df2bib)
import(dplyr)
importFrom(dplyr,bind_rows)
importFrom(humaniformat,format_period)
importFrom(humaniformat,format_reverse)
importFrom(humaniformat,parse_names)
importFrom(plyr,rbind.fill)
importFrom(stats,complete.cases)
importFrom(stringr,str_extract)
importFrom(stringr,str_match)
Expand Down
9 changes: 7 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# bib2df 0.2.1

*


# bib2df 0.2

* Standardized return value for `bib2df()` (#5, @leeper)
* Added functionality to write data.frame back to BibTeX file (`df2bib()`) (#5, @leeper)
* Adds a testthat-based tests for both bib2df() and df2bib() (#5, @leeper)
* Added functionality to write `data.frame` back to BibTeX file (`df2bib()`) (#5, @leeper)
* Adds a testthat-based tests for both `bib2df()` and `df2bib()` (#5, @leeper)
* Added functionality of the `humaniformat` package to split up names (#4)
10 changes: 5 additions & 5 deletions R/bib2df-package.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' bib2df: Parse a BibTeX file to data.frame
#' bib2df: Parse a BibTeX file to \code{data.frame}
#'
#' This package provides functions to parse and write BibTeX files. BibTeX files
#' can be parsed to data.frame to make them accessible with popular tools like
#' dplyr, tidyr, ggplot2 and many more.
#' can be parsed to \code{data.frame} to make them accessible with popular tools like
#' \code{dplyr}, \code{tidyr}, \code{ggplot2} and many more.
#'
#' BibTeX entries represented in a data.frame can be altered in a familar way and
#' BibTeX entries represented in a \code{data.frame} can be altered in a familiar way and
#' written back to a valid BibTeX file.
#'
#' To learn more about bib2df, start with the vignettes:
#' To learn more about \code{bib2df}, start with the vignettes:
#' \code{browseVignettes(package = "bib2df")}
#' @docType package
#' @name bib2df-package
Expand Down
8 changes: 4 additions & 4 deletions R/bib2df.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @title Parse a BibTeX file to a \code{data.frame}..
#' @description The BibTeX file is read, parsed, tidied and written to a \code{data.frame}..
#' @details Uses the internal \code{bib2df_read()}, \code{bib2df_gather()} and \code{bib2df_tidy()}. No magic, just a wrapper.
#' @title Parse a BibTeX file to a \code{data.frame}
#' @description The BibTeX file is read, parsed, tidied and written to a \code{data.frame}
#' @details For simplicity \code{bib2df()} unifies the reading, parsing and tidying of a BibTeX file while being aware of a standardized output format, different BibTeX styles and missing values in the BibTeX file.
#' @param file character, path to a .bib file.
#' @param separate_names logical, should authors' and editors' names be separated into first and given name?
#' @return A \code{data.frame}.
Expand All @@ -10,7 +10,7 @@
#' bib2df(path)
#' @seealso \code{\link{df2bib}}
#' @export
bib2df <- function(file, separate_names = c(FALSE, TRUE)) {
bib2df <- function(file, separate_names = FALSE) {
bib <- bib2df_read(file)
bib <- bib2df_gather(bib)
bib <- bib2df_tidy(bib, separate_names)
Expand Down
28 changes: 9 additions & 19 deletions R/bib2df_gather.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
#' @title Parse a .bib file.
#' @description \code{bib2df_gather} parses a .bib file and returns each BibTeX-item as a row in a \code{data.frame}.
#' @details \code{bib2df_gather} extracts the BibTeX entry types, fields and the respective values. Each item is presented as a row in a data_frame whereas the entry type (column \code{CATEGORY}) is added to each row.
#' @param bib, a character vector resulting from \code{bib2df_read()}.
#' @return A \code{data.frame}.
#' @author Philipp Ottolinger
#' @importFrom stringr str_match
#' @importFrom stringr str_extract
#' @importFrom plyr rbind.fill
#' @importFrom dplyr bind_rows
#' @importFrom stats complete.cases
#' @export bib2df_gather
#' @examples
#' path <- system.file("extdata", "biblio.bib", package = "bib2df")
#' bib <- bib2df_read(path)
#' bib <- bib2df_gather(bib)

bib2df_gather <- function(bib) {
from <- which(!is.na(str_match(bib, "@")))
to <- c(from[-1], length(bib))
if (!length(from)) {
return(empty)
}
itemslist <- mapply(function(x,y) return(bib[x:y]), x = from, y = to - 1)
itemslist <- mapply(function(x, y) return(bib[x:y]), x = from, y = to - 1)
keys <- lapply(itemslist,
function(x) {
str_extract(x[1], "(?<=@\\w{1,50}\\{)((.*)){1}(?=,)")
Expand Down Expand Up @@ -71,23 +61,23 @@ bib2df_gather <- function(bib) {
items <- mapply(cbind, categories, values)
items <- lapply(items,
function(x) {
x <- cbind(toupper(x[,1]), x[,2])
x <- cbind(toupper(x[, 1]), x[, 2])
}
)
items <- lapply(items,
function(x) {
x[complete.cases(x),]
x[complete.cases(x), ]
}
)
items <- mapply(function(x,y) {
items <- mapply(function(x, y) {
rbind(x, c("CATEGORY", y))
},
x = items, y = fields)
items <- lapply(items, t)
items <- lapply(items,
function(x) {
colnames(x) <- x[1,]
x <- x[-1,]
colnames(x) <- x[1, ]
x <- x[-1, ]
return(x)
}
)
Expand All @@ -98,7 +88,7 @@ bib2df_gather <- function(bib) {
return(x)
}
)
dat <- rbind.fill(c(list(empty), items))
dat <- bind_rows(c(list(empty), items))
dat <- as_data_frame(dat)
dat$BIBTEXKEY <- unlist(keys)
dat
Expand Down
12 changes: 1 addition & 11 deletions R/bib2df_read.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
#' @title Read a .bib file.
#' @description \code{bib2df_read()} reads a .bib file to a character vector.
#' @details \code{bib2df_read()} uses \code{base::readLines()} to read from a local .bib file. UTF-8 compatibility is ensured.
#' @param file character, path to a .bib file.
#' @return A character vector.
#' @author Philipp Ottolinger
#' @importFrom stringr str_replace_all
#' @export bib2df_read
#' @examples
#' path <- system.file("extdata", "biblio.bib", package = "bib2df")
#' bib <- bib2df_read(path)
#' bib

bib2df_read <- function(file) {
bib <- readLines(file)
bib <- str_replace_all(bib, "[^[:graph:]]", " ")
Expand Down
15 changes: 1 addition & 14 deletions R/bib2df_tidy.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
#' @title Tidy a bib2df \code{data.frame}..
#' @description \code{bib2df_tidy()} aims to tidy a \code{data.frame}. resulting from \code{bib2df_gather()}.
#' @details If multiple Authors or Editors are supplied, the respective character string is split up into a list. The year of publication is converted to \code{as.numeric()}. The \code{CATEGORY} column moves to the very left of the tibble.
#' @param bib, resulting from \code{bib2df_gather()}.
#' @param separate_names logical, should authors' and editors' names be separated into first and given name?
#' @author Philipp Ottolinger
#' @return A \code{data.frame}.
#' @export bib2df_tidy
#' @import dplyr
#' @importFrom humaniformat format_reverse
#' @importFrom humaniformat format_period
#' @importFrom humaniformat parse_names
#' @examples
#' path <- system.file("extdata", "biblio.bib", package = "bib2df")
#' bib <- bib2df_read(path)
#' bib <- bib2df_gather(bib)
#' bib <- bib2df_tidy(bib)
#' bib

bib2df_tidy <- function(bib, separate_names = c(FALSE, TRUE)) {
AUTHOR <- EDITOR <- YEAR <- CATEGORY <- NULL
if ("AUTHOR" %in% colnames(bib)) {
Expand Down
10 changes: 5 additions & 5 deletions R/df2bib.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @title Export a BibTeX data.frame to a .bib file.
#' @description The BibTeX data.frame is written to a .bib file
#' @param x data.frame, returned by \code{\link{df2bib}}.
#' @title Export a BibTeX \code{data.frame} to a .bib file
#' @description The BibTeX \code{data.frame} is written to a .bib file
#' @param x \code{data.frame}, returned by \code{\link{df2bib}}.
#' @param file character, path to a .bib file.
#' @return \code{file} as a character string, invisibly.
#' @author Thomas J. Leeper
Expand All @@ -16,12 +16,12 @@ df2bib <- function(x, file) {
paste0(substr(string, 1, 1),
tolower(substr(string, 2, nchar(string) )))
}
naReplace <- function(df) {
na_replace <- function(df) {
df[is.na(df)] <- ""
return(df)
}
if (class(x$AUTHOR[[1]]) == "data.frame") {
x$AUTHOR <- lapply(x$AUTHOR, naReplace)
x$AUTHOR <- lapply(x$AUTHOR, na_replace)
x$AUTHOR <- lapply(x$AUTHOR,
function(x) {
paste(x$last_name,
Expand Down
10 changes: 5 additions & 5 deletions man/bib2df-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/bib2df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 0 additions & 28 deletions man/bib2df_gather.Rd

This file was deleted.

28 changes: 0 additions & 28 deletions man/bib2df_read.Rd

This file was deleted.

32 changes: 0 additions & 32 deletions man/bib2df_tidy.Rd

This file was deleted.

6 changes: 3 additions & 3 deletions man/df2bib.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/testthat/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ test_that("df2bib() works", {
expect_true(file.exists(df2bib(bib, bib2 <- tempfile())))
expect_true(identical(bib, bib2df(bib2)))
})

Loading

0 comments on commit 2f38c20

Please sign in to comment.