Skip to content

Commit

Permalink
Merge pull request #136 from antaldaniel/master
Browse files Browse the repository at this point in the history
See enhancement issue #128
  • Loading branch information
antagomir authored Jan 17, 2019
2 parents 78a8a4f + 193e005 commit 496c461
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 7 deletions.
15 changes: 8 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ Depends:
Imports:
broom,
classInt,
dplyr,
httr,
dplyr,
httr,
jsonlite,
RColorBrewer,
readr,
sf,
sp,
readr,
sf,
sp,
stringi,
stringr,
tibble,
tidyr,
countrycode
countrycode,
RefManageR
Suggests:
Cairo,
ggplot2,
Expand All @@ -56,4 +57,4 @@ BugReports: https://github.com/ropengov/eurostat/issues
VignetteBuilder: knitr
NeedsCompilation: no
Repository: CRAN
RoxygenNote: 6.1.0
RoxygenNote: 6.1.1
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(cut_to_classes)
export(dic_order)
export(eurotime2date)
export(eurotime2num)
export(get_bibentry)
export(get_eurostat)
export(get_eurostat_dic)
export(get_eurostat_geospatial)
Expand All @@ -18,6 +19,8 @@ export(label_eurostat_vars)
export(search_eurostat)
import(methods)
importFrom(RColorBrewer,brewer.pal)
importFrom(RefManageR,BibEntry)
importFrom(RefManageR,toBiblatex)
importFrom(broom,tidy)
importFrom(classInt,classIntervals)
importFrom(dplyr,"%>%")
Expand Down Expand Up @@ -47,3 +50,4 @@ importFrom(tidyr,gather_)
importFrom(tidyr,separate)
importFrom(utils,data)
importFrom(utils,download.file)
importFrom(utils,toBibtex)
102 changes: 102 additions & 0 deletions R/get_bibentry.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#' @title Create A Data Bibliography
#' @description Creates a bibliography from selected Eurostat data files,
#' including last Eurostat update, URL access data, and optional keywords
#' set by the user.
#' @param code A Eurostat data code or a vector of Eurostat data codes as
#' character or factor.
#' @param keywords A list of keywords to be added to the entries. Defaults
#' to \code{NULL}.
#' @format Default is \code{'Biblatex'}, alternatives are \code{'bibentry'}
#' or \code{'Bibtex'} (not case sensitive.)
#' @importFrom RefManageR BibEntry toBiblatex
#' @importFrom utils toBibtex
#' @export
#' @author Daniel Antal, Przemyslaw Biecek
#' @return a bibentry, Bibtex or Biblatex object.
#' @examples
#' \dontrun{
#' my_biblography <- get_bibentry (
#' code = c("tran_hv_frtra", "t2020_rk310","tec00001") ,
#' keywords = list ( c("railways", "freight", "transport"),
#' c("railways", "passengers", "modal split") ),
#' format = "Biblatex" )
#'
#' readLines ( my_bibliograhy, "eurostat_data.bib")
#' }


get_bibentry <- function(
code,
keywords = NULL,
format = "Biblatex" ) {

if ( ! any( class (code) %in% c("character", "factor")) ) {
stop("The code(s) must be added as character vector")
}
if ( !is.null(keywords) & ! class (keywords) == "list" ) {
stop("If keyword(s) are added, they must be added as a list.")
}

code <- as.character(code)
format <- tolower(as.character(format))

if ( ! format %in% c("bibentry", "bibtex", "biblatex")) {
warning ( "The ", format, " is not recognized, will return Biblatex as
default.")
format <- 'biblatex'
}

code = c("tran_hv_frtra", "t2020_rk310", "tec00001")
toc <- get_eurostat_toc()
toc <- toc[toc$code %in% code, ]
toc <- toc[! duplicated(toc), ]

urldate <- as.character(Sys.Date())

if (nrow(toc) == 0) {
warning(paste0("Code ",code, "not found"))
return()
}

eurostat_id <- paste0( toc$code, "_",
gsub("\\.", "-", toc$`last update of data`))

for ( i in 1:nrow(toc) ) {

if ( !is.null(keywords) ) { #if user entered keywords
if ( length(keywords)<i ) { #last keyword not entered
keyword_entry <- NULL } else if ( nchar(keywords)[i] > 0 ) { #not empty keyword entry
keyword_entry <- paste( keywords[[i]], collapse = ', ' )
}
} else {
keyword_entry <- NULL
}

entry <- RefManageR::BibEntry(
bibtype = "misc",
key = eurostat_id[i],
title = paste0(toc$title[i]," [",code[i],"]"),
url = paste0("https://ec.europa.eu/eurostat/web/products-datasets/-/",code[i]),
language = "en",
year = paste0(toc$`last update of data`[1]),
publisher = "Eurostat",
author = "Eurostat",
keywords = keyword_entry,
urldate = urldate
)

if ( i > 1 ) {
entries <- c(entries, entry)
} else {
entries <- entry
}
}

if (format == "bibtex") {

entries <- utils::toBibtex(entries)
} else if ( format == "biblatex") {
entries <- RefManageR::toBiblatex ( entries )
}
entries
}
39 changes: 39 additions & 0 deletions man/get_bibentry.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,26 @@ test_that("Handle numbers in filter name",{
ignore.order = TRUE)
})


context("bibliography")

test_that("Bibentry gives correct results",{
skip_on_cran()
expect_equal(
class (get_bibentry ( code = c("sts_inpr_a", "nama_10_gdp"),
keywords = list ( c("production", "industry"),
c("GDP")
),
format = "Biblatex")),
"Biblatex"
)
expect_error(
get_bibentry( code = 123456))
expect_warning(
get_bibentry( code = c("sts_inpr_a", "nama_10_gdp"),
keywords = list ( c("production", "industry"),
c("GDP")
),
format = "character"))
})

0 comments on commit 496c461

Please sign in to comment.