Skip to content

Instantly share code, notes, and snippets.

@thomasp85
Created February 26, 2019 19:59
Show Gist options
  • Save thomasp85/b956740c89043959c190e96991f84871 to your computer and use it in GitHub Desktop.
Save thomasp85/b956740c89043959c190e96991f84871 to your computer and use it in GitHub Desktop.
Single-quote styler
transformers <- styler::tidyverse_style()
transformers$token$fix_quotes <- function (pd_flat) {
str_const <- pd_flat$token == 'STR_CONST'
str_const_change <- grepl('^"(.*)"$', pd_flat$text[str_const])
raw_str <- sub('^"(.*)"$', '\\1', (pd_flat$text[str_const][str_const_change]))
raw_str <- gsub('(\\\\)?\'', '\\\\\'', raw_str)
raw_str <- gsub('\\\\"', '"', raw_str)
pd_flat$text[str_const][str_const_change] <- paste0('\'', raw_str, '\'')
pd_flat
}
styler::style_pkg(transformers = transformers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment