Skip to content

Commit

Permalink
Dollar sign within quotations fix (#151)
Browse files Browse the repository at this point in the history
* fix to rm dollar sign from lines that have quotes
fixes #150

* update docs

* account for nested list scenario

---------

Co-authored-by: Kathryn Doering <[email protected]>
  • Loading branch information
e-perl-NOAA and k-doering-NOAA authored Dec 19, 2024
1 parent 499d1d5 commit 4562601
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/doc-and-style-r.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- name: setup env using GITHUB_TOKEN, if no pat
if: env.GITHUB_PAT == ''
run: echo "GITHUB_PAT=${{ secrets.GITHUB_TOKEN}}" >> "$GITHUB_ENV"

- name: checkout, make changes and submit as pr on new branch
if: inputs.commit-directly == false
uses: actions/checkout@v4
Expand Down
66 changes: 43 additions & 23 deletions R/rm_dollar_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
#' which we don't often want. This function takes a file and changes all dollar
#' signs to double brackets with names in quotations instead. Note that this
#' function will incorrectly convert text enclosed in backticks that includes a dollar sign.
#' Note also that if a dollar sign is within a text
#' string enclosed with quotation marks, it will also not convert correctly and
#' so will exit on error.
#' (for example, "See test$name" would become
#' "See test\[\["name"\]\]", which is not parsable R code due to 2 sets of quotation
#' marks.)
#' @param file Filename either with full path or relative to working directory.
#' @param out_file The name or path of a new file to write to. This is by
#' default the same as the original file. Set to NULL to avoid writing a new
Expand Down Expand Up @@ -71,6 +65,27 @@ rm_dollar_sign <- function(file,
lines
)
# all others not in back ticks
# address situations in quotation marks
pattern_no_backtick_in_quotes <-
'(".*)([[:alnum:]]|\\.|\\_|\\]|\\(|\\))\\$([[:alnum:]]+)(([[:alnum:]]|\\.|\\_)*)(.*")'
replace_no_backtick_in_quotes <- "\\1\\2\\[\\[\'\\3\\4\'\\]\\]\\6"
mod_lines <- gsub(
pattern = pattern_no_backtick_in_quotes,
replacement = replace_no_backtick_in_quotes,
mod_lines
)
if (allow_recursive) {
mod_lines <- recursive_replace(pattern = pattern_no_backtick_in_quotes,
replace = replace_no_backtick_in_quotes, lines = mod_lines, max_loops = max_loops)
} else {
if (length(grep(pattern_no_backtick_in_quotes, x = mod_lines)) > 0) {
warning(
"There are lists in lists in quotes, but allow_recursive = FALSE, so not all",
"dollar sign operators were converted."
)
}
}

pattern_no_backtick <-
"([[:alnum:]]|\\.|\\_|\\]|\\(|\\))\\$([[:alnum:]]+)(([[:alnum:]]|\\.|\\_)*)(\\s|[[:punct:]]|$)"
replace_no_backtick <- "\\1\\[\\[\"\\2\\3\"\\]\\]\\5"
Expand All @@ -80,23 +95,8 @@ rm_dollar_sign <- function(file,
mod_lines
)
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
)
}
if (length(grep(pattern_no_backtick, x = mod_lines)) > 0) {
warning(
"max_loops was set too low to replace all instances of dollar ",
"sign references."
)
}
mod_lines <- recursive_replace(pattern = pattern_no_backtick,
replace = replace_no_backtick, lines = mod_lines, max_loops = max_loops)
} else {
if (length(grep(pattern_no_backtick, x = mod_lines)) > 0) {
warning(
Expand All @@ -110,3 +110,23 @@ rm_dollar_sign <- function(file,
}
mod_lines
}

recursive_replace <- function(pattern, replace, lines, max_loops) {
# get rid of $ when there are lists in lists.
ind <- 1
while (length(grep(pattern, x = lines)) > 0 && ind <= max_loops) {
ind <- ind + 1
lines <- gsub(
pattern = pattern,
replacement = replace,
lines
)
}
if (length(grep(pattern, x = lines)) > 0) {
warning(
"max_loops was set too low to replace all instances of dollar ",
"sign references in quotes."
)
}
lines
}
6 changes: 0 additions & 6 deletions man/rm_dollar_sign.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions tests/testthat/test-rm_dollar_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ test_that("rm dollar sign works", {
"x$`nameinbacktick`",
"x$mylist$my_col$YetAnotherCol",
"x$mylist$my_col$`1_somename`",
"x()$my_name <- y$test"
"x()$my_name <- y$test",
"\"test object$item with additional quoted text\"",
"\"test object$item1$item2 with additional quoted text and nested list\""
)
expect_output <- c(
"x[[\"my_name\"]] <- y[[\"test\"]]",
Expand All @@ -31,7 +33,9 @@ test_that("rm dollar sign works", {
"x[[\"nameinbacktick\"]]",
"x[[\"mylist\"]][[\"my_col\"]][[\"YetAnotherCol\"]]",
"x[[\"mylist\"]][[\"my_col\"]][[\"1_somename\"]]",
"x()[[\"my_name\"]] <- y[[\"test\"]]"
"x()[[\"my_name\"]] <- y[[\"test\"]]",
"\"test object[['item']] with additional quoted text\"",
"\"test object[['item1']][['item2']] with additional quoted text and nested list\""
)
writeLines(test_text, "test_rm_dollar_sign.txt")
new_text <- rm_dollar_sign(
Expand Down

0 comments on commit 4562601

Please sign in to comment.