Skip to content

Commit

Permalink
initial commit of okay fix - it ignores ()$ occurrences though
Browse files Browse the repository at this point in the history
  • Loading branch information
e-perl-NOAA committed Dec 13, 2024
1 parent 499d1d5 commit 9079b6c
Showing 1 changed file with 74 additions and 26 deletions.
100 changes: 74 additions & 26 deletions R/rm_dollar_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
#' @export
#' @author Kathryn Doering
#' @examples
#' test_text <- c(
#' "x$my_name <- y$test",
#' "x[['my_name']]",
#' "no_replace_here <- 44",
#' "x$names<-new_assign;x$`22`",
#' "x$names <- new_assign; x$`22`",
#' "x$`$$weirdcharacters`<-222",
#' "x$`nameinbacktick`",
#' "x$mylist$my_col$YetAnotherCol",
#' "x$mylist$my_col$`1_somename`"
# ' test_text <- c(
# ' "x$my_name <- y$test",
# ' "x[['my_name']]",
# ' "no_replace_here <- 44",
# ' "x$names<-new_assign;x$`22`",
# ' "x$names <- new_assign; x$`22`",
# ' "x$`$$weirdcharacters`<-222",
# ' "x$`nameinbacktick`",
# ' "x$mylist$my_col$YetAnotherCol",
# ' "x$mylist$my_col$`1_somename`"
# ' ""example text""
#' )
#' writeLines(test_text, "test_rm_dollar_sign.txt")
#' new_text <- rm_dollar_sign(
Expand All @@ -44,6 +45,11 @@
#' )
#' new_text
#' file.remove("test_rm_dollar_sign.txt")

# Test for r4ss
# file <- "C:\\Users\\elizabeth.gugliotti\\Documents\\github_repos\\r4ss\\R\\get_ss3_exe.R"
# out_file <- "C:\\Users\\elizabeth.gugliotti\\Documents\\github_repos\\r4ss\\R\\get_ss3_exe2.R"

rm_dollar_sign <- function(file,
out_file = file,
allow_recursive = TRUE,
Expand All @@ -64,49 +70,91 @@ rm_dollar_sign <- function(file,
paste0(paste0("Line ", difficult_lines, " ", difficult_txt), collapse = "\n")
)
}
# get rid of names in back ticks first:
mod_lines <- gsub(
pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\$`([[:print:]]+)`",
replacement = "\\1\\[\\[\"\\2\"\\]\\]",
lines

# issue with regex
# mod_lines_better_fix <- gsub(
# pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\(.*\\)\\$([[:print:]])\\([[:space:]]) ",
# replacement = "\\1\\(\\)\\[\\[\"\\2\"\\]\\]",
# lines)

mod_lines <- lapply(lines, function(x){
check_parenth <- grepl(pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\(.*\\)\\$([[:print:]]+)", x)
if(check_parenth == FALSE){
gsub(pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\$`([[:print:]]+)`",
replacement = "\\1\\[\\[\"\\2\"\\]\\]",
x)
} else {
x
}
}
)

# # get rid of names in back ticks first:
# mod_lines <- gsub(
# pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\$`([[:print:]]+)`",
# replacement = "\\1\\[\\[\"\\2\"\\]\\]",
# lines
# )

# all others not in back ticks
pattern_no_backtick <-
"([[:alnum:]]|\\.|\\_|\\]|\\(|\\))\\$([[:alnum:]]+)(([[:alnum:]]|\\.|\\_)*)(\\s|[[:punct:]]|$)"
replace_no_backtick <- "\\1\\[\\[\"\\2\\3\"\\]\\]\\5"
mod_lines <- gsub(
pattern = pattern_no_backtick,
replacement = replace_no_backtick,
mod_lines
# mod_lines <- gsub(
# pattern = pattern_no_backtick,
# replacement = replace_no_backtick,
# mod_lines
# )

mod_lines <- lapply(mod_lines, function(x){
check_parenth <- as.logical(grepl(pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\(.*\\)\\$([[:print:]]+)", x))
if(check_parenth == FALSE){
gsub(pattern = pattern_no_backtick, replacement = replace_no_backtick, x)
} else {
x
}
}
)

if (allow_recursive) {
# get rid of $ when there are lists in lists.
ind <- 1
while (length(grep(pattern_no_backtick, x = mod_lines)) > 0 &
ind <= max_loops) {
ind <- ind + 1
mod_lines <- gsub(
pattern = pattern_no_backtick,
replacement = replace_no_backtick,
mod_lines
mod_lines <- lapply(mod_lines, function(x){
check_parenth <- grepl(pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\(.*\\)\\$([[:print:]]+)", x)
if(isTRUE(check_parenth) && check_parenth == FALSE){
gsub(pattern = pattern_no_backtick, replacement = replace_no_backtick, x)
} else {
x
}
}
)
}
if (length(grep(pattern_no_backtick, x = mod_lines)) > 0) {
# mod_lines <- gsub(
# pattern = pattern_no_backtick,
# replacement = replace_no_backtick,
# mod_lines
# )

check_parenth <- grep(pattern = "([[:alnum:]]|\\.|\\_|\\(|\\))\\(.*\\)\\$([[:print:]]+)", mod_lines)
if (length(grep(pattern_no_backtick, x = mod_lines)) > length(check_parenth)) {
warning(
"max_loops was set too low to replace all instances of dollar ",
"sign references."
)
}
} else {
if (length(grep(pattern_no_backtick, x = mod_lines)) > 0) {
if (length(grep(pattern_no_backtick, x = mod_lines)) > length(check_parenth)) {
warning(
"There are lists in lists, but allow_recursive = FALSE, so not all",
"dollar sign operators were converted."
)
}
}
if (!is.null(out_file)) {
writeLines(mod_lines, out_file)
writeLines(unlist(mod_lines), out_file)
}
mod_lines
}

0 comments on commit 9079b6c

Please sign in to comment.