Skip to content

Commit 1dd9362

Browse files
committed
Improve indentation, integrate catAndRun() msgs
and use kwb.utils::createFunctionExtdataFile()
1 parent bb56428 commit 1dd9362

14 files changed

Lines changed: 247 additions & 169 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ Remotes:
4949
github::kwb-r/kwb.utils
5050
Encoding: UTF-8
5151
LazyData: true
52-
RoxygenNote: 7.1.1
52+
RoxygenNote: 7.1.2
5353
VignetteBuilder: knitr

R/check_for_differences.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#' head(diffs_df)
3939
#'
4040
#' }
41-
check_for_differences <- function(
42-
df_x, df_y, dbg = TRUE) {
41+
check_for_differences <- function(df_x, df_y, dbg = TRUE)
42+
{
4343
name_df_x <- deparse(substitute(df_x))
4444
name_df_y <- deparse(substitute(df_y))
4545

@@ -49,9 +49,9 @@ check_for_differences <- function(
4949
tidy_name <- function(name) paste0(name, "_tidy")
5050

5151
get_text <- function(name_df, name_value) sprintf(
52-
"Tidying data.frame '%s' and rename 'value' to '%s'. Saving to %s",
53-
name_df, name_value, tidy_name(name_df)
54-
)
52+
"Tidying data.frame '%s' and rename 'value' to '%s'. Saving to %s",
53+
name_df, name_value, tidy_name(name_df)
54+
)
5555

5656
df_x_tidy <- kwb.utils::catAndRun(
5757
messageText = get_text(name_df_x, name_value_x), dbg = dbg,
@@ -84,10 +84,12 @@ check_for_differences <- function(
8484

8585
diffs_idx <- kwb.utils::catAndRun(
8686
messageText = messageText, dbg = dbg,
87-
expr = which(!sapply(seq_len(nrow(df_xy_tidy)), function(row) identical(
87+
expr = which(
88+
!sapply(seq_len(nrow(df_xy_tidy)), function(row) identical(
8889
df_xy_tidy[[name_value_x]][row],
8990
df_xy_tidy[[name_value_y]][row]
90-
)))
91+
))
92+
)
9193
)
9294

9395
kwb.utils::catIf(dbg, sprintf(

R/create_endnote_list.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#' str(endnote_list[1]$record)
1111
#' attr(endnote_list, "xml_file_info")
1212
#' attr(endnote_list, "xml_filename_without_extension")
13-
create_endnote_list <- function(endnote_xml = default_xml()) {
13+
create_endnote_list <- function(endnote_xml = default_xml())
14+
{
1415
endnote_list <- xml2::as_list(xml2::read_xml(endnote_xml))$xml$records
1516

16-
1717
add_file_info_attributes(endnote_list, endnote_xml)
1818
}

R/create_list_by_pubtype_from_df.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#' refs_list_by_pubtype <- create_list_by_pubtype_from_df(refs_df)
1313
#' str(refs_list_by_pubtype, 1)
1414
#' }
15-
create_list_by_pubtype_from_df <- function(refs_df) {
15+
create_list_by_pubtype_from_df <- function(refs_df)
16+
{
1617
refs_df <- refs_df %>%
1718
dplyr::arrange(dplyr::desc(.data$rec_number))
1819

R/create_list_with_unique_entries.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#' unique_entries_list <- create_list_with_unique_entries(refs_df)
1313
#' str(unique_entries_list, 1)
1414
#' }
15-
create_list_with_unique_entries <- function(refs_df) {
15+
create_list_with_unique_entries <- function(refs_df)
16+
{
1617
select_columns <- function(pattern) {
1718
columns <- unique(stringr::str_extract(names(refs_df), pattern))
1819
columns[!is.na(columns)]

R/create_references_df_from_list.R

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
# colbind: Wrapper around dplyr::bind_cols(). Makes sure that no rows get lost
22
# if the data frame to add has no rows.
3-
colbind <- function(df, df_2) {
3+
colbind <- function(df, df_2)
4+
{
45
if (nrow(df_2) == 0L) {
6+
57
# Return the original data frame if the data frame to add has no columns
68
if (ncol(df_2) == 0L) {
79
return(df)
810
}
11+
912
# Give the data frame to add one empty row (full of NA)
1013
df_2 <- df_2[1L, ]
1114
}
15+
1216
dplyr::bind_cols(df, df_2)
1317
}
1418

1519
#' @noRd
1620
#' @keywords internal
17-
null_to_na <- function(x, na_fill = NA_character_) {
21+
null_to_na <- function(x, na_fill = NA_character_)
22+
{
1823
if (is.null(x[[1]])) na_fill else x[[1]]
1924
}
2025

@@ -26,14 +31,15 @@ null_to_na <- function(x, na_fill = NA_character_) {
2631
#' @return one row abstract data frame
2732
#' @export
2833
#' @importFrom dplyr bind_cols
29-
get_abstract <- function(record_list, collapse = FALSE) {
34+
get_abstract <- function(record_list, collapse = FALSE)
35+
{
3036
abstract <- record_list$record$abstract
3137

3238
if (is.null(abstract)) {
3339
return(NA_character_)
3440
}
3541

36-
collapse_fields(abstract, collapse, collapse_val = " ", element = 1)
42+
collapse_fields(abstract, collapse, collapse_val = " ", element = 1L)
3743
}
3844

3945
#' Helper function: get keywords from list for a reference
@@ -47,8 +53,12 @@ get_abstract <- function(record_list, collapse = FALSE) {
4753
#' @export
4854
#' @importFrom dplyr bind_cols
4955
get_keywords <- function(
50-
record_list, col_name = "keyword", extract_value = "keywords",
51-
collapse = FALSE) {
56+
record_list,
57+
col_name = "keyword",
58+
extract_value = "keywords",
59+
collapse = FALSE
60+
)
61+
{
5262
get_multi_entry(
5363
entries = record_list$record[[extract_value]],
5464
col_name = col_name,
@@ -68,7 +78,12 @@ get_keywords <- function(
6878
#' @export
6979
#' @importFrom dplyr bind_cols
7080
get_authors <- function(
71-
record_list, col_name = "author", extract_value = "authors", collapse = FALSE) {
81+
record_list,
82+
col_name = "author",
83+
extract_value = "authors",
84+
collapse = FALSE
85+
)
86+
{
7287
get_multi_entry(
7388
entries = record_list$record$contributors[[extract_value]],
7489
col_name = col_name,
@@ -78,8 +93,8 @@ get_authors <- function(
7893
}
7994

8095
# get_multi_entry --------------------------------------------------------------
81-
get_multi_entry <- function(
82-
entries, col_name, element = NULL, collapse = FALSE) {
96+
get_multi_entry <- function(entries, col_name, element = NULL, collapse = FALSE)
97+
{
8398
if (is.null(entries)) {
8499
return(stats::setNames(nm = colname_i(col_name, 1), tibble::tibble(
85100
value = NA_character_
@@ -100,15 +115,21 @@ get_multi_entry <- function(
100115
}
101116

102117
# collapse_fields --------------------------------------------------------------
103-
collapse_fields <- function(entries,
104-
collapse = TRUE,
105-
collapse_val = "",
106-
element) {
118+
collapse_fields <- function(
119+
entries,
120+
collapse = TRUE,
121+
collapse_val = "",
122+
element
123+
)
124+
{
107125
if (is.list(entries) && collapse) {
126+
108127
paste(collapse = collapse_val, lapply(seq_along(entries), function(i) {
109128
null_to_na(entries[[i]][[element]])
110129
}))
130+
111131
} else {
132+
112133
null_to_na(entries[[element]])
113134
}
114135
}
@@ -119,8 +140,11 @@ collapse_fields <- function(entries,
119140
#' @return one row authors data frame
120141
#' @export
121142
#' @inheritParams get_authors
122-
get_secondary_authors <- function(record_list, collapse = FALSE) {
123-
get_authors(record_list, "author_secondary", "secondary-authors", collapse)
143+
get_secondary_authors <- function(record_list, collapse = FALSE)
144+
{
145+
get_authors(
146+
record_list, "author_secondary", "secondary-authors", collapse
147+
)
124148
}
125149

126150
#' Helper function: get tertiary authors from list for a reference
@@ -129,8 +153,11 @@ get_secondary_authors <- function(record_list, collapse = FALSE) {
129153
#' @return one row authors data frame
130154
#' @export
131155
#' @inheritParams get_authors
132-
get_tertiary_authors <- function(record_list, collapse = FALSE) {
133-
get_authors(record_list, "author_tertiary", "tertiary-authors", collapse)
156+
get_tertiary_authors <- function(record_list, collapse = FALSE)
157+
{
158+
get_authors(
159+
record_list, "author_tertiary", "tertiary-authors", collapse
160+
)
134161
}
135162

136163
#' Helper function: get pdfurls from list for a reference
@@ -143,7 +170,8 @@ get_tertiary_authors <- function(record_list, collapse = FALSE) {
143170
#' @export
144171
#' @importFrom dplyr bind_cols
145172
#' @importFrom tibble tibble
146-
get_pdfurls <- function(record_list, col_name = "urls_pdf", collapse = FALSE) {
173+
get_pdfurls <- function(record_list, col_name = "urls_pdf", collapse = FALSE)
174+
{
147175
get_multi_entry(
148176
entries = record_list$record$urls$`pdf-urls`,
149177
col_name = col_name,
@@ -160,13 +188,17 @@ get_pdfurls <- function(record_list, col_name = "urls_pdf", collapse = FALSE) {
160188
#' @export
161189
#' @importFrom dplyr bind_cols
162190
#' @importFrom tibble tibble
163-
record_list_to_df <- function(record_list, collapse = FALSE) {
191+
record_list_to_df <- function(record_list, collapse = FALSE)
192+
{
164193
get_record_entry <- function(path) get_list_entry(record_list$record, path)
194+
165195
get_style <- function(path) {
166-
collapse_fields(get_record_entry(path),
167-
collapse = collapse,
168-
collapse_val = "",
169-
element = 1)
196+
collapse_fields(
197+
get_record_entry(path),
198+
collapse = collapse,
199+
collapse_val = "",
200+
element = 1L
201+
)
170202
}
171203

172204
replace_newline_with_semicolon <- function(text) {
@@ -239,7 +271,8 @@ record_list_to_df <- function(record_list, collapse = FALSE) {
239271
#' refs_df <- create_references_df(endnote_list)
240272
#' head(refs_df)
241273
#'
242-
create_references_df <- function(endnote_list, collapse = FALSE) {
274+
create_references_df <- function(endnote_list, collapse = FALSE)
275+
{
243276
extract_values_from_list <- lapply(seq_along(endnote_list), function(rec_id) {
244277
record_list_to_df(record_list = endnote_list[rec_id], collapse)
245278
})

R/give_hints.R

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,37 @@
66
#' @return vector with project_names with hints how to improve data quality (in
77
#' case give_hints = TRUE)
88
#' @export
9-
give_hints_project_names <- function(project_names, dbg = TRUE) {
9+
give_hints_project_names <- function(project_names, dbg = TRUE)
10+
{
1011
kwb.utils::catAndRun(
1112
"Generate hints for 'Project Names'",
12-
expr = {
13-
replace_na_with_value(project_names, "add_project_name")
14-
}
13+
replace_na_with_value(project_names, "add_project_name")
1514
)
1615
}
1716

17+
get_indices_multiple_authors <- function(author_names)
18+
{
19+
n_names <- unlist(lapply(author_names, function(x) {
20+
length(stringr::str_split(x, pattern = ",")[[1L]])
21+
}))
1822

19-
get_indices_multiple_authors <- function(author_names) {
20-
which(unlist(lapply(author_names, function(x)
21-
length(stringr::str_split(x, pattern = ",")[[1]]))) > 2)
23+
which(n_names > 2L)
2224
}
2325

24-
get_indices_firstname_lastname <- function(author_names) {
26+
get_indices_firstname_lastname <- function(author_names)
27+
{
2528
which(
26-
stringr::str_detect(author_names, "^\\s?\\w+\\.?\\s+\\w+") &
27-
!stringr::str_detect(author_names, ",")
28-
)}
29-
29+
stringr::str_detect(author_names, "^\\s?\\w+\\.?\\s+\\w+") &
30+
!stringr::str_detect(author_names, ",")
31+
)
32+
}
3033

31-
get_indices_ends_with_semicolon <- function(author_names) {
32-
which(author_names %>%
33-
stringr::str_trim() %>%
34-
stringr::str_detect(".*,$"))
34+
get_indices_ends_with_semicolon <- function(author_names)
35+
{
36+
author_names %>%
37+
stringr::str_trim() %>%
38+
stringr::str_detect(".*,$") %>%
39+
which()
3540
}
3641

3742
#' Helper Function: Give Hints For Author Names
@@ -43,29 +48,35 @@ get_indices_ends_with_semicolon <- function(author_names) {
4348
#' @return vector with author_names with hints how to improve data quality (in
4449
#' case give_hints = TRUE)
4550
#' @export
46-
give_hints_author_names <- function(author_names, dbg = TRUE) {
51+
give_hints_author_names <- function(author_names, dbg = TRUE)
52+
{
4753
kwb.utils::catAndRun(
4854
"Generate hints for 'Author Names'",
4955
dbg = dbg,
5056
expr = {
5157
multiple_authors_idx <- get_indices_multiple_authors(author_names)
5258
firstname_lastname_idx <- get_indices_firstname_lastname(author_names)
5359
ends_with_semicolon_idx <- get_indices_ends_with_semicolon(author_names)
54-
55-
5660
author_names %>%
57-
replace_indices_with_value(multiple_authors_idx,
58-
"fix_multiple_authors_per_line",
59-
dbg) %>%
60-
replace_indices_with_value(firstname_lastname_idx,
61-
paste0("fix_firstname_lastname_with_",
62-
"lastname_semicolon_firstname"),
63-
dbg) %>%
64-
replace_indices_with_value(ends_with_semicolon_idx,
65-
"fix_author_name_by_deleting_last_semicolon",
66-
dbg) %>%
67-
replace_na_with_value("add_author_lastname_semicolon_firstname",
68-
dbg)
61+
replace_indices_with_value(
62+
multiple_authors_idx,
63+
"fix_multiple_authors_per_line",
64+
dbg
65+
) %>%
66+
replace_indices_with_value(
67+
firstname_lastname_idx,
68+
"fix_firstname_lastname_with_lastname_semicolon_firstname",
69+
dbg
70+
) %>%
71+
replace_indices_with_value(
72+
ends_with_semicolon_idx,
73+
"fix_author_name_by_deleting_last_semicolon",
74+
dbg
75+
) %>%
76+
replace_na_with_value(
77+
"add_author_lastname_semicolon_firstname",
78+
dbg
79+
)
6980
}
7081
)
7182
}
@@ -82,8 +93,6 @@ give_hints_author_names <- function(author_names, dbg = TRUE) {
8293
give_hints_accessiblity <- function(access, dbg = TRUE) {
8394
kwb.utils::catAndRun(
8495
"Generate hints for 'Accessiblity'",
85-
expr = {
86-
replace_na_with_value(access, "add_public_or_confidential")
87-
}
96+
expr = replace_na_with_value(access, "add_public_or_confidential")
8897
)
8998
}

0 commit comments

Comments
 (0)