1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 07:26:11 +01:00
AMR/data-raw/reproduction_of_poorman.R

111 lines
6.4 KiB
R
Raw Normal View History

2020-05-16 13:05:47 +02:00
# get complete filenames of all R files in the GitHub repository of nathaneastwood/poorman
2023-02-08 13:48:06 +01:00
library(magrittr)
2023-02-08 16:51:41 +01:00
`%like%` <- function(x, y) grepl(y, x, ignore.case = TRUE, perl = TRUE)
`%unlike%` <- function(x, y) !grepl(y, x, ignore.case = TRUE, perl = TRUE)
2023-02-08 13:48:06 +01:00
commit <- "3cc0a9920b1eb559dd166f548561244189586b3a"
2020-05-16 13:05:47 +02:00
files <- xml2::read_html(paste0("https://github.com/nathaneastwood/poorman/tree/", commit, "/R")) %>%
rvest::html_nodes("a") %>%
rvest::html_attr("href")
2023-02-08 13:48:06 +01:00
files <- files[files %like% "/blob/.*R$"]
2020-05-16 13:05:47 +02:00
# get full URLs of all raw R files
2020-09-19 11:54:01 +02:00
files <- sort(paste0("https://raw.githubusercontent.com", gsub("blob/", "", files[files %like% "/R/.*.R$"])))
2020-05-16 13:05:47 +02:00
# remove files with only pkg specific code
files <- files[files %unlike% "(zzz|init)[.]R$"]
2020-09-19 11:54:01 +02:00
# also, there's a lot of functions we don't use
2023-02-08 16:51:41 +01:00
files <- files[files %unlike% "/(between|coalesce|cumulative|fill|glimpse|group_cols|na_if|near|nest_by|check_filter|poorman-package|print|recode|reconstruct|replace_na|replace_with|rownames|slice|union_all|unite|window_rank|with_groups)[.]R$"]
2020-05-16 13:05:47 +02:00
# add our prepend file, containing info about the source of the data
2023-02-12 17:10:48 +01:00
intro <- readLines("data-raw/poorman_prepend.R") %>%
2023-02-08 13:48:06 +01:00
# add commit to intro part
gsub("{commit}", commit, ., fixed = TRUE) %>%
# add date to intro part
gsub("{date}", trimws(format(Sys.Date(), "%e %B %Y")), ., fixed = TRUE)
# copyright info:
2023-02-08 13:48:06 +01:00
copyright <- paste0("# ", readLines(paste0("https://raw.githubusercontent.com/nathaneastwood/poorman/", commit, "/LICENSE")))
2020-05-16 13:05:47 +02:00
# read all contents to a character vector
contents <- character(0)
sapply(files, function(file) {
message("reading ", basename(file))
2020-05-16 13:05:47 +02:00
contents <<- c(contents, readLines(file))
invisible()
})
# remove lines starting with "#'" and NULL and write to file
contents <- contents[!grepl("^(#'|NULL|\"_PACKAGE)", contents)]
2023-02-08 13:48:06 +01:00
contents.bak <- contents
2020-05-16 13:05:47 +02:00
2023-02-08 13:48:06 +01:00
# grouped attributes same as dplyr
contents <- gsub("grouped_data", "grouped_df", contents, fixed = TRUE)
2020-05-16 13:05:47 +02:00
# now make it independent on UseMethod, since we will not export these functions
2023-02-08 13:48:06 +01:00
has_usemethods <- gsub("^([a-z_]+).*", "\\1", contents[which(contents %like% "usemethod") - 1])
for (use in has_usemethods) {
relevant_row <- which(contents %like% paste0("^", use, " <- function")) + 1
function_call <- trimws(gsub(".*function(.*)\\{.*", "\\1", contents[relevant_row - 1]))
function_call1 <- trimws(gsub("[()]", "", strsplit(function_call, ",")[[1]][1]))
if (any(contents %like% paste0(use, ".grouped_df"))) {
# this function will have methods for data.frame and grouped_df
contents[relevant_row] <- paste0(" if (\"grouped_df\" %in% class(", function_call1, ")) ", use, ".grouped_df", function_call, " else ", use, ".data.frame", function_call)
} else {
# this function will only have data.frame as method
contents[relevant_row] <- paste0(" ", use, ".data.frame", function_call)
}
# add pm_ prefix
contents[relevant_row - 1] <- paste0("pm_", contents[relevant_row - 1])
}
# correct for NextMethod
contents <- gsub("NextMethod\\(\"(.*)\"\\)", "\\1.data.frame(...)", contents)
# correct for 'default' method
contents <- gsub(".default <-", ".data.frame <-", contents, fixed = TRUE)
contents <- gsub("pm_group_by_drop.data.frame", "pm_group_by_drop", contents, fixed = TRUE)
2023-02-08 16:51:41 +01:00
contents <- gsub("(stats::)?setNames", "stats::setNames", contents)
# now get all those pm_* functions to replace all untransformed function name calls as well
new_pm_names <- sort(gsub("pm_(.*?) <-.*", "\\1", contents[grepl("^pm_", contents)]))
for (i in seq_len(length(new_pm_names))) {
contents <- gsub(paste0("([^a-z._])", new_pm_names[i], "([^a-z._])"), paste0("\\1pm_", new_pm_names[i], "\\2"), contents)
# starting with a space or a straight bracket or an opening parenthesis, ending with nothing or a non-character or a closing parenthesis
contents <- gsub(paste0("( |\\[|\\()", new_pm_names[i], "($|[^a-z]|\\))"), paste0("\\1pm_", new_pm_names[i], "\\2"), contents)
}
2022-08-28 10:31:50 +02:00
# replace %>% with %pm>%
2023-02-08 13:48:06 +01:00
contents[which(contents %like% "^\\|\\|") - 1] <- paste0(contents[which(contents %like% "^\\|\\|") - 1], " ||")
contents[which(contents %like% "^\\|\\|")] <- gsub("^\\|\\|", "", contents[which(contents %like% "^\\|\\|")])
contents <- gsub("%>%", "%pm>%", contents, fixed = TRUE)
# fix for new lines, since n() also existed
contents <- gsub("\\pm_n", "\\n", contents, fixed = TRUE)
# prefix other functions also with "pm_"
contents <- gsub("^([a-z_]+)(\\$|)", "pm_\\1\\2", contents)
2023-02-08 13:48:06 +01:00
# prefix environmental objects and functions
2023-02-08 16:51:41 +01:00
contents <- gsub("(add_group_columns|add_tally|apply_grouped_function|as_function|as_symbols|build_data_frame|calculate_groups|check_filter|check_if_types|check_name|check_context|collapse_to_sentence|context|deparse_|dotdotdot|drop_dup_list|eval_call|eval_env|eval_expr|eval_select_pos|find_used|flatten|get_group_details|gluestick|group_|groups|groups_set|has_groups|have_name|insert_dot|is.grouped_df|is_df_or_vector|is_empty_list|is_formula|is_named|is_negated_colon|is_nested|is_string|is_wholenumber|join_message|join_worker|names_are_invalid|nth|peek_vars|reconstruct_attrs|replace_na|replace_with|select_|select_context|select_env|select_positions|setup_|split_into_groups|squash|tally|tally_n|validate_case_when_length)", "pm_\\1", contents)
# now a lot of items are overprefixed
contents <- gsub("(pm_)+", "pm_", contents)
2023-02-08 16:51:41 +01:00
contents <- gsub("_pm_", "_", contents)
2023-02-08 13:48:06 +01:00
contents <- gsub("pm_if (\"grouped_df", "if (\"grouped_df", contents, fixed = TRUE)
# remove comments and empty lines
contents <- gsub("#.*", "", contents)
contents <- contents[trimws(contents) != ""]
# fix for their relocate()
contents <- gsub("if (!missing(.before))", "if (!missing(.before) && !is.null(.before))", contents, fixed = TRUE)
contents <- gsub("if (!missing(.after))", "if (!missing(.after) && !is.null(.after))", contents, fixed = TRUE)
contents[which(contents %like% "reshape\\($") + 1] <- gsub("data", "as.data.frame(data, stringsAsFactors = FALSE)", contents[which(contents %like% "reshape\\($") + 1])
2023-02-12 17:10:48 +01:00
contents <- gsub("pm_relocate(.data = long, values_to, .after = -1)", 'pm_relocate(.data = long, "value", .after = -1)', contents, fixed = TRUE)
# who needs US spelling?
2023-02-08 13:48:06 +01:00
contents <- contents[contents %unlike% "summarize"]
# add intro
contents <- c(
intro,
copyright,
"",
contents
)
2020-05-16 13:05:47 +02:00
writeLines(contents, "R/aa_helper_pm_functions.R")
2020-12-17 16:22:25 +01:00
2023-02-08 13:48:06 +01:00
# note: pm_left_join() will be overwritten by aaa_helper_functions.R, which contains a faster implementation
2023-02-24 09:43:10 +01:00
# replace `res <- as.data.frame(res)` with `res <- as.data.frame(res, stringsAsFactors = FALSE)`