Skip to content

Commit 496c461

Browse files
authored
Merge pull request #136 from antaldaniel/master
See enhancement issue #128
2 parents 78a8a4f + 193e005 commit 496c461

File tree

5 files changed

+176
-7
lines changed

5 files changed

+176
-7
lines changed

DESCRIPTION

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ Depends:
2424
Imports:
2525
broom,
2626
classInt,
27-
dplyr,
28-
httr,
27+
dplyr,
28+
httr,
2929
jsonlite,
3030
RColorBrewer,
31-
readr,
32-
sf,
33-
sp,
31+
readr,
32+
sf,
33+
sp,
3434
stringi,
3535
stringr,
3636
tibble,
3737
tidyr,
38-
countrycode
38+
countrycode,
39+
RefManageR
3940
Suggests:
4041
Cairo,
4142
ggplot2,
@@ -56,4 +57,4 @@ BugReports: https://github.com/ropengov/eurostat/issues
5657
VignetteBuilder: knitr
5758
NeedsCompilation: no
5859
Repository: CRAN
59-
RoxygenNote: 6.1.0
60+
RoxygenNote: 6.1.1

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export(cut_to_classes)
55
export(dic_order)
66
export(eurotime2date)
77
export(eurotime2num)
8+
export(get_bibentry)
89
export(get_eurostat)
910
export(get_eurostat_dic)
1011
export(get_eurostat_geospatial)
@@ -18,6 +19,8 @@ export(label_eurostat_vars)
1819
export(search_eurostat)
1920
import(methods)
2021
importFrom(RColorBrewer,brewer.pal)
22+
importFrom(RefManageR,BibEntry)
23+
importFrom(RefManageR,toBiblatex)
2124
importFrom(broom,tidy)
2225
importFrom(classInt,classIntervals)
2326
importFrom(dplyr,"%>%")
@@ -47,3 +50,4 @@ importFrom(tidyr,gather_)
4750
importFrom(tidyr,separate)
4851
importFrom(utils,data)
4952
importFrom(utils,download.file)
53+
importFrom(utils,toBibtex)

R/get_bibentry.R

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#' @title Create A Data Bibliography
2+
#' @description Creates a bibliography from selected Eurostat data files,
3+
#' including last Eurostat update, URL access data, and optional keywords
4+
#' set by the user.
5+
#' @param code A Eurostat data code or a vector of Eurostat data codes as
6+
#' character or factor.
7+
#' @param keywords A list of keywords to be added to the entries. Defaults
8+
#' to \code{NULL}.
9+
#' @format Default is \code{'Biblatex'}, alternatives are \code{'bibentry'}
10+
#' or \code{'Bibtex'} (not case sensitive.)
11+
#' @importFrom RefManageR BibEntry toBiblatex
12+
#' @importFrom utils toBibtex
13+
#' @export
14+
#' @author Daniel Antal, Przemyslaw Biecek
15+
#' @return a bibentry, Bibtex or Biblatex object.
16+
#' @examples
17+
#' \dontrun{
18+
#' my_biblography <- get_bibentry (
19+
#' code = c("tran_hv_frtra", "t2020_rk310","tec00001") ,
20+
#' keywords = list ( c("railways", "freight", "transport"),
21+
#' c("railways", "passengers", "modal split") ),
22+
#' format = "Biblatex" )
23+
#'
24+
#' readLines ( my_bibliograhy, "eurostat_data.bib")
25+
#' }
26+
27+
28+
get_bibentry <- function(
29+
code,
30+
keywords = NULL,
31+
format = "Biblatex" ) {
32+
33+
if ( ! any( class (code) %in% c("character", "factor")) ) {
34+
stop("The code(s) must be added as character vector")
35+
}
36+
if ( !is.null(keywords) & ! class (keywords) == "list" ) {
37+
stop("If keyword(s) are added, they must be added as a list.")
38+
}
39+
40+
code <- as.character(code)
41+
format <- tolower(as.character(format))
42+
43+
if ( ! format %in% c("bibentry", "bibtex", "biblatex")) {
44+
warning ( "The ", format, " is not recognized, will return Biblatex as
45+
default.")
46+
format <- 'biblatex'
47+
}
48+
49+
code = c("tran_hv_frtra", "t2020_rk310", "tec00001")
50+
toc <- get_eurostat_toc()
51+
toc <- toc[toc$code %in% code, ]
52+
toc <- toc[! duplicated(toc), ]
53+
54+
urldate <- as.character(Sys.Date())
55+
56+
if (nrow(toc) == 0) {
57+
warning(paste0("Code ",code, "not found"))
58+
return()
59+
}
60+
61+
eurostat_id <- paste0( toc$code, "_",
62+
gsub("\\.", "-", toc$`last update of data`))
63+
64+
for ( i in 1:nrow(toc) ) {
65+
66+
if ( !is.null(keywords) ) { #if user entered keywords
67+
if ( length(keywords)<i ) { #last keyword not entered
68+
keyword_entry <- NULL } else if ( nchar(keywords)[i] > 0 ) { #not empty keyword entry
69+
keyword_entry <- paste( keywords[[i]], collapse = ', ' )
70+
}
71+
} else {
72+
keyword_entry <- NULL
73+
}
74+
75+
entry <- RefManageR::BibEntry(
76+
bibtype = "misc",
77+
key = eurostat_id[i],
78+
title = paste0(toc$title[i]," [",code[i],"]"),
79+
url = paste0("https://ec.europa.eu/eurostat/web/products-datasets/-/",code[i]),
80+
language = "en",
81+
year = paste0(toc$`last update of data`[1]),
82+
publisher = "Eurostat",
83+
author = "Eurostat",
84+
keywords = keyword_entry,
85+
urldate = urldate
86+
)
87+
88+
if ( i > 1 ) {
89+
entries <- c(entries, entry)
90+
} else {
91+
entries <- entry
92+
}
93+
}
94+
95+
if (format == "bibtex") {
96+
97+
entries <- utils::toBibtex(entries)
98+
} else if ( format == "biblatex") {
99+
entries <- RefManageR::toBiblatex ( entries )
100+
}
101+
entries
102+
}

man/get_bibentry.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-all.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,26 @@ test_that("Handle numbers in filter name",{
183183
ignore.order = TRUE)
184184
})
185185

186+
187+
context("bibliography")
188+
189+
test_that("Bibentry gives correct results",{
190+
skip_on_cran()
191+
expect_equal(
192+
class (get_bibentry ( code = c("sts_inpr_a", "nama_10_gdp"),
193+
keywords = list ( c("production", "industry"),
194+
c("GDP")
195+
),
196+
format = "Biblatex")),
197+
"Biblatex"
198+
)
199+
expect_error(
200+
get_bibentry( code = 123456))
201+
expect_warning(
202+
get_bibentry( code = c("sts_inpr_a", "nama_10_gdp"),
203+
keywords = list ( c("production", "industry"),
204+
c("GDP")
205+
),
206+
format = "character"))
207+
})
208+

0 commit comments

Comments
 (0)