Skip to content

Commit

Permalink
Merge pull request #135 from NIFU-NO/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
sda030 authored Dec 27, 2024
2 parents dc2f88d + a28fef9 commit 5916266
Show file tree
Hide file tree
Showing 31 changed files with 818 additions and 649 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: saros.base
Title: Base Tools for Semi-Automatic Reporting of Ordinary Surveys
Version: 0.3.2
Version: 0.4.0
Authors@R: c(
person(given = "Stephan",
family = "Daus",
Expand Down
36 changes: 22 additions & 14 deletions R/add_chapter_foldername_to_chapter_structure.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
add_chapter_foldername_to_chapter_structure <-
function(chapter_structure,
max_width_folder_name = 64) {

max_width_folder_name = 64,
filename_prefix = "") {
grouping_vars <- dplyr::group_vars(chapter_structure)

# Handles NA acceptably, as these are converted to string

if(!is.null(chapter_structure$chapter)) {
chapter_structure$.chapter_number <- as.integer(as.factor(chapter_structure$chapter))
if (!is.null(chapter_structure$chapter)) {
chapter_structure$.chapter_number <-
as.integer(factor(chapter_structure$chapter,
levels = unique(chapter_structure$chapter),
exclude = character()
))

digits <- floor(log10(length(unique(chapter_structure$chapter))))+1
chapter_number_text <- sprintf(paste0("%0", digits, "d"),
chapter_structure$.chapter_number)
digits <- floor(log10(length(unique(chapter_structure$chapter)))) + 1
chapter_number_text <- sprintf(
paste0("%0", digits, "d"),
chapter_structure$.chapter_number
)

chapter_foldername_clean <-
filename_sanitizer(as.character(chapter_structure$chapter),
max_chars = max_width_folder_name,
accept_hyphen = FALSE,
make_unique = FALSE)
max_chars = max_width_folder_name,
accept_hyphen = FALSE,
make_unique = FALSE
)

chapter_structure$.chapter_foldername <-
stringi::stri_c(chapter_number_text, "_", chapter_foldername_clean,
ignore_null = TRUE)

stringi::stri_c(filename_prefix,
chapter_number_text, "_", chapter_foldername_clean,
ignore_null = TRUE
)
}
chapter_structure |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(grouping_vars)))
dplyr::grouped_df(grouping_vars)
}
2 changes: 1 addition & 1 deletion R/add_n_cats_to_chapter_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ add_n_cats_to_chapter_structure <-
}
) |>
dplyr::bind_rows() |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(dplyr::group_vars(chapter_structure))))
dplyr::grouped_df(dplyr::group_vars(chapter_structure))
}
18 changes: 8 additions & 10 deletions R/add_n_range_to_chapter_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,30 @@ add_n_range_to_chapter_structure <-
glue_template_1 = "{n}",
glue_template_2 = "[{n[1]}-{n[2]}]",
variable_name = ".n_range") {

chapter_structure |>
dplyr::group_map(.keep = TRUE, .f = ~{
dplyr::group_map(.keep = TRUE, .f = ~ {
deps <- as.character(unique(.x$.variable_name_dep))
deps <- deps[!is.na(deps)]
n <-
lapply(deps, function(v) {
length(data[[v]][!is.na(data[[v]])])
}) |>
unlist()
if(!is.null(n)) {
if (!is.null(n)) {
n <-
range(n, na.rm = TRUE) |>
unique()
if(all(is.na(n))) {
if (all(is.na(n))) {
.x[[variable_name]] <- 0
} else {
template <- if(length(n)==1) glue_template_1 else glue_template_2
tryCatch(.x[[variable_name]] <- glue::glue(template),
error = function(cnd) glue_err(cnd=cnd, arg_name="glue_template_*"))
template <- if (length(n) == 1) glue_template_1 else glue_template_2
tryCatch(.x[[variable_name]] <- glue::glue(template),
error = function(cnd) glue_err(cnd = cnd, arg_name = "glue_template_*")
)
}

}
.x
}) |>
dplyr::bind_rows() |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(dplyr::group_vars(chapter_structure))))
dplyr::grouped_df(dplyr::group_vars(chapter_structure))
}

4 changes: 2 additions & 2 deletions R/add_n_to_chapter_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_n_to_chapter_structure <- function(chapter_structure,
target_grouping_variables <- c(".variable_name_dep", ".variable_name_indep")
out <-
chapter_structure |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(target_grouping_variables))) |>
dplyr::grouped_df(vars = target_grouping_variables) |>
dplyr::group_map(.keep = TRUE, .f = ~ {
target_grouping_variables_tmp <-
lapply(target_grouping_variables, function(chapter_structure_col) {
Expand Down Expand Up @@ -38,7 +38,7 @@ add_n_to_chapter_structure <- function(chapter_structure,
if (length(out) > 0) {
out |>
dplyr::bind_rows() |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(dplyr::group_vars(chapter_structure))))
dplyr::grouped_df(vars = dplyr::group_vars(chapter_structure))
} else {
attr(out, "ptype")
}
Expand Down
15 changes: 7 additions & 8 deletions R/add_n_vars_to_chapter_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ add_n_vars_to_chapter_structure <-
function(chapter_structure,
target_variable = ".variable_name_dep",
variable_name = ".n_dep") {


grouping_structure <- dplyr::group_vars(chapter_structure)

chapter_structure |>
dplyr::group_map(.keep = TRUE,
.f = ~{
dplyr::group_map(
.keep = TRUE,
.f = ~ {
uniques <- as.character(unique(.x[[target_variable]]))
if(!is.na(length(uniques[!is.na(uniques)]))) {
if (!is.na(length(uniques[!is.na(uniques)]))) {
.x[[variable_name]] <-
length(uniques[!is.na(uniques)])
} else {
.x[[variable_name]] <- 0
}
.x
}) |>
}
) |>
dplyr::bind_rows() |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(grouping_structure)))

dplyr::grouped_df(grouping_structure)
}
51 changes: 27 additions & 24 deletions R/add_obj_name_to_chapter_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@ add_obj_name_to_chapter_structure <-
max_width = 128,
valid_obj = TRUE,
make_unique = TRUE,
to_lower = TRUE
) {


to_lower = TRUE) {
grouping_structure_original <- dplyr::group_vars(chapter_structure)
out <- replace_label_groups_with_name_groups(chapter_structure)
grouping_structure_reduced <- dplyr::group_vars(out)
grouping_structure_temporary <- ".variable_group_id"

collapsed <-
out |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(c(grouping_structure_temporary)))) |>
dplyr::group_map(.keep = FALSE,
.f = ~{

.x[[variable_name]] <-
get_common_names_from_data(.x[, grouping_structure_reduced],
sep = sep,
max_width = max_width)
.x
}) |>
dplyr::grouped_df(grouping_structure_temporary) |>
dplyr::group_map(
.keep = FALSE,
.f = ~ {
.x[[variable_name]] <-
get_common_names_from_data(.x[, grouping_structure_reduced],
sep = sep,
max_width = max_width
)
.x
}
) |>
dplyr::bind_rows() |>
dplyr::distinct(dplyr::pick(tidyselect::all_of(c(grouping_structure_original, variable_name))))

collapsed[[variable_name]] <-
filename_sanitizer(x = collapsed[[variable_name]],
max_chars = max_width,
sep = sep,
valid_obj = valid_obj,
to_lower = to_lower,
make_unique = make_unique)
filename_sanitizer(
x = collapsed[[variable_name]],
max_chars = max_width,
sep = sep,
valid_obj = valid_obj,
to_lower = to_lower,
make_unique = make_unique
)

dplyr::left_join(x = out,
y = collapsed,
by = grouping_structure_original) |>
dplyr::group_by(dplyr::pick(tidyselect::all_of(grouping_structure_original)))
dplyr::left_join(
x = out,
y = collapsed,
by = grouping_structure_original
) |>
dplyr::grouped_df(vars = grouping_structure_original)
}
32 changes: 19 additions & 13 deletions R/add_parsed_vars_to_chapter_structure.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
add_parsed_vars_to_chapter_structure <-
function(chapter_structure,
data) {

chapter_structure$.variable_selection <-
stringi::stri_replace_all_fixed(chapter_structure$.variable_selection,
pattern = '\"',
replacement = "'")
pattern = '\"',
replacement = "'"
)
chapter_structure$.variable_selection <-
stringi::stri_replace_all_regex(chapter_structure$.variable_selection,
pattern = '[[:space:],]+',
replacement = ",")
pattern = "[[:space:],]+",
replacement = ","
)

chapter_structure$.cols <-
eval_cols(x = chapter_structure$.variable_selection,
data = data,
call = call)
eval_cols(
x = chapter_structure$.variable_selection,
data = data,
call = call
)

chapter_structure <-
tidyr::unnest_longer(chapter_structure,
col = ".cols",
values_to = ".variable_position",
indices_to = ".variable_name")
col = ".cols",
values_to = ".variable_position",
indices_to = ".variable_name"
)
chapter_structure$.variable_position <- unname(chapter_structure$.variable_position)
chapter_structure$.variable_name <-
ifelse(chapter_structure$.variable_name %in% c("1", ""),
NA_character_,
chapter_structure$.variable_name)
NA_character_,
chapter_structure$.variable_name
)

chapter_structure
}
Loading

0 comments on commit 5916266

Please sign in to comment.