Skip to content

Commit 3a6dbbb

Browse files
authored
Merge pull request #34 from antaldaniel/master
roxygen and tidyselect changes
2 parents 94a6eb7 + bb7b652 commit 3a6dbbb

24 files changed

+285
-191
lines changed

DESCRIPTION

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
Type: Package
22
Package: retroharmonize
33
Title: Ex Post Survey Data Harmonization
4-
Version: 0.2.5.002
5-
Date: 2022-09-24
4+
Version: 0.2.5.003
5+
Date: 2023-12-01
66
Authors@R: c(
7-
person("Daniel", "Antal", , "[email protected]", role = c("aut", "cre"),
7+
person(given = "Daniel",
8+
family = "Antal",
9+
email = "[email protected]",
10+
role = c("aut", "cre"),
811
comment = c(ORCID = "0000-0001-7513-6760")),
912
person(given = "Marta",
10-
family = "Kolczynska",
11-
role = c("ctb"),
12-
email = "[email protected]",
13-
comment = c(ORCID = "0000-0003-4981-0437")),
14-
person(given = "Pyry",
15-
family = "Kantanen",
16-
role = "ctb",
17-
comment = c(ORCID = "0000-0003-2853-2765")),
18-
person(given = "Leo",
19-
family = " Lahti",
20-
role = "ctb",
21-
comment = c(ORCID = "0000-0001-5537-637X")
22-
)
13+
family = "Kolczynska",
14+
role = c("ctb"),
15+
email = "[email protected]",
16+
comment = c(ORCID = "0000-0003-4981-0437"))
2317
)
2418
Maintainer: Daniel Antal <[email protected]>
2519
Description: Assist in reproducible retrospective (ex-post) harmonization
@@ -69,6 +63,6 @@ Config/testthat/edition: 3
6963
Encoding: UTF-8
7064
Language: en-US
7165
LazyData: true
72-
RoxygenNote: 7.2.1
66+
RoxygenNote: 7.2.3
7367
X-schema.org-isPartOf: http://ropengov.org/
7468
X-schema.org-keywords: ropengov

R/collect_val_labels.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Collect labels from metadata file
1+
#' @title Collect labels from metadata file
22
#'
33
#' @param metadata A metadata data frame created by
44
#' \code{\link{metadata_create}}.

R/create_codebook.R

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,32 @@ create_codebook <- function ( metadata = NULL,
8383
if ( n_labelled_numeric > 0 ) {
8484
# These area cases when the labels are of class numeric
8585
valid_labelled_numeric <- metadata_labelled_numeric %>%
86-
filter ( grepl( "labelled", .data$class_orig )) %>%
86+
filter ( grepl( "labelled", class_orig )) %>%
8787
select ( all_of(c("entry", "id", "filename", "var_name_orig", "var_label_orig", "valid_labels"))) %>%
88-
unnest_longer( .data$valid_labels) %>%
88+
unnest_longer( valid_labels) %>%
8989
rlang::set_names ( c("entry", "id", "filename", "var_name_orig","var_label_orig", "val_code_orig", "val_label_orig")) %>%
9090
mutate (
9191
# This is the valid observation range
9292
label_range = "valid",
93-
val_code_orig = as.character(.data$val_code_orig))
93+
val_code_orig = as.character(val_code_orig))
9494

9595
na_labelled_numeric <- metadata[num_labels ,] %>%
96-
filter ( grepl( "labelled", .data$class_orig )) %>%
96+
filter ( grepl( "labelled", class_orig )) %>%
9797
select ( all_of(c("entry", "id", "filename", "var_name_orig", "var_label_orig", "na_labels"))) %>%
98-
unnest_longer( .data$na_labels) %>%
98+
unnest_longer( na_labels) %>%
9999
purrr::set_names ( c("entry", "id", "filename", "var_name_orig", "var_label_orig", "val_code_orig", "val_label_orig")) %>%
100100
mutate (
101101
# This is the missing observation range
102102
label_range = "missing") %>%
103-
filter ( !is.na(.data$val_code_orig) ) %>%
104-
mutate ( val_code_orig = as.character(.data$val_code_orig) )
103+
filter ( !is.na(val_code_orig) ) %>%
104+
mutate ( val_code_orig = as.character(val_code_orig) )
105105

106106

107107
num_labels <- valid_labelled_numeric %>%
108108
dplyr::bind_rows (
109109
na_labelled_numeric
110110
) %>%
111-
dplyr::arrange( .data$entry, .data$val_code_orig ) %>%
111+
dplyr::arrange( entry, val_code_orig ) %>%
112112
left_join ( metadata %>% select ( any_of(c("entry", "id", "filename", "na_range",
113113
"n_labels", "n_valid_labels", "n_na_labels",
114114
user_vars))),
@@ -126,34 +126,34 @@ create_codebook <- function ( metadata = NULL,
126126
if ( n_labelled_character > 0) {
127127
# These area cases when the na_labels are of class character
128128
valid_labelled_character <- metadata_labelled_character %>%
129-
filter ( grepl( "labelled", .data$class_orig )) %>%
129+
filter ( grepl( "labelled", class_orig )) %>%
130130
select ( all_of(c("entry", "id", "filename", "var_name_orig", "var_label_orig", "valid_labels"))) %>%
131-
unnest_longer( .data$valid_labels) %>%
131+
unnest_longer( valid_labels) %>%
132132
rlang::set_names ( c("entry", "id", "filename", "var_name_orig","var_label_orig", "val_code_orig", "val_label_orig")) %>%
133133
mutate (
134134
# This is the valid observation range
135135
label_range = "valid") %>%
136-
mutate ( val_code_orig = as.character(.data$val_code_orig) )
136+
mutate ( val_code_orig = as.character(val_code_orig) )
137137

138138

139139
na_labelled_character <- metadata[char_labels ,] %>%
140-
filter ( grepl( "labelled", .data$class_orig )) %>%
140+
filter ( grepl( "labelled", class_orig )) %>%
141141
select ( all_of(c("entry", "id", "filename", "var_name_orig", "var_label_orig", "na_labels"))) %>%
142-
unnest_longer( .data$na_labels) %>%
142+
unnest_longer( na_labels) %>%
143143
purrr::set_names ( c("entry", "id", "filename", "var_name_orig", "var_label_orig",
144144
"val_code_orig", "val_label_orig")) %>%
145145
mutate (
146146
# This is the missing observation range
147147
label_range = "missing") %>%
148-
filter ( !is.na(.data$val_code_orig)) %>%
149-
mutate ( val_code_orig = as.character(.data$val_code_orig) )
148+
filter ( !is.na(val_code_orig)) %>%
149+
mutate ( val_code_orig = as.character(val_code_orig) )
150150

151151

152152
char_labels <- valid_labelled_character %>%
153153
dplyr::bind_rows (
154154
na_labelled_character
155155
) %>%
156-
dplyr::arrange( .data$entry, .data$val_code_orig ) %>%
156+
dplyr::arrange( entry, val_code_orig ) %>%
157157
left_join ( metadata %>% select ( any_of(c("entry", "id", "filename", "na_range",
158158
"n_labels", "n_valid_labels", "n_na_labels",
159159
user_vars))),
@@ -178,14 +178,14 @@ create_codebook <- function ( metadata = NULL,
178178
left_join ( user_data[0,], by = "entry" )
179179
} else if ( n_labelled_character == 0 ) {
180180
num_labels %>%
181-
dplyr::arrange (.data$entry)
181+
dplyr::arrange (entry)
182182
} else if ( n_labelled_numeric == 0 ) {
183183
char_labels %>%
184-
dplyr::arrange (.data$entry)
184+
dplyr::arrange (entry)
185185
} else {
186186
num_labels %>%
187187
bind_rows ( char_labels) %>%
188-
dplyr::arrange (.data$entry)
188+
dplyr::arrange (entry)
189189
}
190190
}
191191

R/crosswalk.R

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#' data frames, where the variable names, and optionally the variable labels, and the missing
2626
#' value range is harmonized (the same names, labels, codes are used.)
2727
#' @importFrom dplyr filter select mutate distinct_all relocate across everything
28-
#' @importFrom rlang .data
2928
#' @importFrom assertthat assert_that
3029
#' @family harmonization functions
3130
#' @examples
@@ -90,7 +89,7 @@ crosswalk_surveys <- function(crosswalk_table,
9089
msg = "selection must have rows")
9190

9291
select_to_harmonize <- selection %>%
93-
filter ( !is.na(.data$val_label_orig) )
92+
filter ( !is.na(val_label_orig) )
9493

9594
vars_to_harmonize <- unique(select_to_harmonize$var_name_target)
9695

@@ -102,7 +101,7 @@ crosswalk_surveys <- function(crosswalk_table,
102101
for ( this_var in vars_to_harmonize ) {
103102

104103
correspondence_table <- select_to_harmonize %>%
105-
filter ( .data$var_name_target == this_var )
104+
filter ( var_name_target == this_var )
106105

107106
assert_that(is.numeric(correspondence_table$val_numeric_target),
108107
msg = "Error in relabel_survey: 'val_numeric_target' must be a numeric vector")
@@ -138,15 +137,15 @@ crosswalk_surveys <- function(crosswalk_table,
138137
subset_survey <- function(this_survey) {
139138

140139
survey_id <- attr(this_survey, "id")
141-
assertthat::assert_that(length(survey_id)>0,
142-
msg = "Error in subset_survey(): survey_id has 0 length.")
140+
assert_that(length(survey_id)>0,
141+
msg = "Error in subset_survey(): survey_id has 0 length.")
143142

144143
tmp <- this_survey %>%
145144
mutate ( id = survey_id ) %>%
146-
relocate ( .data$id, .before = everything())
145+
relocate ( id, .before = everything())
147146

148147
selection <- crosswalk_table %>%
149-
filter ( .data$id == survey_id ) %>%
148+
filter ( id == survey_id ) %>%
150149
distinct_all()
151150

152151

@@ -322,8 +321,8 @@ crosswalk_table_create <- function(metadata) {
322321
if (nrow(metadata)==1) {
323322
fn_labels(x=metadata[1,])
324323
} else {
325-
ctable_list <- lapply ( 1:nrow(metadata), function(x) fn_labels(metadata[x,]) )
326-
ctable <- suppressMessages(purrr::reduce ( ctable_list, full_join ))
324+
ctable_list <- lapply (1:nrow(metadata), function(x) fn_labels(metadata[x,]))
325+
ctable <- suppressMessages(purrr::reduce(ctable_list, full_join))
327326
ctable
328327
}
329328
}
@@ -332,7 +331,6 @@ crosswalk_table_create <- function(metadata) {
332331
#' @rdname crosswalk_table_create
333332
#' @param ctable A table to validate if it is a crosswalk table.
334333
#' @importFrom dplyr tally group_by across filter
335-
#' @importFrom rlang .data
336334
#' @family metadata functions
337335
#' @export
338336

@@ -351,8 +349,8 @@ is.crosswalk_table <- function(ctable) {
351349
distinct_all() %>%
352350
group_by ( across(c("var_name_target", "id"))) %>%
353351
tally() %>%
354-
filter ( .data$n>1) %>%
355-
select (.data$var_name_target ) %>%
352+
filter ( n>1) %>%
353+
select (var_name_target ) %>%
356354
unlist()
357355

358356
error_msg <- paste(unique(duplicates), collapse = ', ')

R/document_survey_item.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ document_survey_item <- function(x) {
7272
tbl_length <- nrow(coding)
7373

7474
list (
75-
code_table = dplyr::bind_cols(coding, labelling) %>%
76-
mutate ( missing = ifelse (.data$values %in% attr(x, "na_values"),
75+
code_table = bind_cols(coding, labelling) %>%
76+
mutate ( missing = ifelse (values %in% attr(x, "na_values"),
7777
TRUE, FALSE)),
7878
history_var_name = c(
7979
c("name" = original_x_name ),

R/harmonize_survey_variables.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ harmonize_survey_variables <- function( crosswalk_table,
6565
new_names <- tibble( var_name_orig = names(this_survey)) %>%
6666
left_join (
6767
crosswalk_table %>%
68-
filter (.data$id == survey_id) %>%
69-
select ( .data$var_name_orig, .data$var_name_target ) %>%
68+
filter (id == survey_id) %>%
69+
select ( var_name_orig, var_name_target ) %>%
7070
distinct_all(),
7171
by = "var_name_orig",
7272
) %>%
73-
mutate ( var_name_target = ifelse (.data$var_name_orig == "rowid",
73+
mutate ( var_name_target = ifelse (var_name_orig == "rowid",
7474
yes = "rowid",
75-
no = .data$var_name_target)) %>%
76-
select ( .data$var_name_target ) %>% unlist() %>% as.character()
75+
no = var_name_target)) %>%
76+
select ( var_name_target ) %>% unlist() %>% as.character()
7777

7878
rlang::set_names(this_survey, nm = new_names )
7979

@@ -96,15 +96,15 @@ harmonize_survey_variables <- function( crosswalk_table,
9696
new_names <- tibble( var_name_orig = names(this_survey)) %>%
9797
left_join (
9898
crosswalk_table %>%
99-
filter (.data$id == survey_id) %>%
100-
select ( .data$var_name_orig, .data$var_name_target ) %>%
99+
filter (id == survey_id) %>%
100+
select ( var_name_orig, var_name_target ) %>%
101101
distinct_all(),
102102
by = "var_name_orig",
103103
) %>%
104-
mutate ( var_name_target = ifelse (.data$var_name_orig == "rowid",
104+
mutate ( var_name_target = ifelse (var_name_orig == "rowid",
105105
yes = "rowid",
106-
no = .data$var_name_target)) %>%
107-
select ( .data$var_name_target ) %>% unlist() %>% as.character()
106+
no = var_name_target)) %>%
107+
select ( var_name_target ) %>% unlist() %>% as.character()
108108

109109
this_survey <- rlang::set_names(this_survey, nm = new_names )
110110
saveRDS(this_survey, file = file.path(export_path, x), version = 2 )

R/metadata_create.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ metadata_waves_create <- function(survey_list) {
9090
#' @importFrom labelled na_values na_range val_labels var_label
9191
#' @importFrom purrr map
9292
#' @importFrom assertthat assert_that
93-
#' @importFrom rlang .data
9493
#' @family metadata functions
9594
#' @return A nested data frame with metadata and the range of
9695
#' labels, na_values and the na_range itself.
@@ -219,14 +218,14 @@ metadata_survey_create <- function(survey) {
219218

220219
return_df <- metadata %>%
221220
left_join ( range_df %>%
222-
group_by ( .data$var_name_orig ) %>%
221+
group_by ( var_name_orig ) %>%
223222
tidyr::nest(),
224223
by = "var_name_orig") %>%
225224
tidyr::unnest ( cols = "data" ) %>%
226225
ungroup() %>%
227-
mutate ( n_na_labels = as.numeric(.data$n_na_labels),
228-
n_valid_labels = as.numeric(.data$n_valid_labels),
229-
n_labels = as.numeric(.data$n_labels)) %>%
226+
mutate ( n_na_labels = as.numeric(n_na_labels),
227+
n_valid_labels = as.numeric(n_valid_labels),
228+
n_labels = as.numeric(n_labels)) %>%
230229
as.data.frame()
231230

232231
change_label_to_empty <- function() {
@@ -247,7 +246,7 @@ metadata_survey_create <- function(survey) {
247246
no = return_df$na_labels )
248247

249248
return_df %>%
250-
select ( -.data$label_type )
249+
select ( -label_type )
251250
}
252251

253252

R/pull_survey.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#' Pull a survey from a survey list
1+
#' @title Pull a survey from a survey list
22
#'
3-
#' Pull a survey by survey code or id.
3+
#' @description Pull a survey by survey code or id.
44
#'
55
#' @param survey_list A list of surveys
66
#' @param id The id of the requested survey. If \code{NULL} use

R/read_spss.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#' \code{tibble::\link[tibble:as_tibble]{as_tibble}} for details.
1919
#' @inheritParams read_rds
2020
#' @importFrom haven read_spss read_sav write_sav is.labelled
21+
#' @importFrom assertthat assert_that
2122
#' @importFrom tibble rowid_to_column as_tibble
2223
#' @importFrom fs path_ext_remove path_file is_file
2324
#' @importFrom labelled var_label
@@ -58,7 +59,7 @@ read_spss <- function(file,
5859

5960
source_file_info <- valid_file_info(file)
6061

61-
safely_read_haven_spss <- purrr::safely(.f = haven::read_spss)
62+
safely_read_haven_spss <- safely(.f = haven::read_spss)
6263

6364
tmp <- safely_read_haven_spss (file = file,
6465
user_na = user_na,
@@ -78,13 +79,13 @@ read_spss <- function(file,
7879

7980
all_vars <- names(tmp)
8081

81-
assertthat::assert_that(length(all_vars)>0,
82-
msg = "The SPSS file has no names.")
82+
assert_that(length(all_vars)>0,
83+
msg = "The SPSS file has no names.")
8384

84-
filename <- fs::path_file(file)
85+
filename <- path_file(file)
8586

8687
if ( is.null(id) ) {
87-
id <- fs::path_ext_remove ( filename )
88+
id <- path_ext_remove(filename)
8889
}
8990

9091
if ( is.null(doi)) {
@@ -161,7 +162,7 @@ read_spss <- function(file,
161162
return_survey <- survey (return_df, id=id, filename=filename, doi=doi)
162163

163164
object_size <- as.numeric(object.size(as_tibble(return_df)))
164-
attr(return_survey, "object_size") <- object_size
165+
attr(return_survey, "object_size") <- object_size
165166
attr(return_survey, "source_file_size") <- source_file_info$size
166167

167168
return_survey

0 commit comments

Comments
 (0)