0

I am attempting to add spaces to a messy R file to clean it up. We have:

a <- b
g<-h
q <- my_df[,c(1:5)]

j<- k%>%
  mutate(mm = 'new col')%>%
  mutate(nn = 'new col') %>%
  mutate(oo = 'new col')

desired output is:

a <- b
g <- h
q <- [, c(1:5)]

j <- k %>%
  mutate(mm = 'new col') %>%
  mutate(nn = 'new col') %>%
  mutate(oo = 'new col')

To summarise:

  • space before / after <-, but of course don't add these spaces if they already exist
  • space before %>%
  • space after ,

It seems like the best option is to search and replace using RStudio's built in tool, and to use the Regex feature specifically. However, I'm not quite sure how to write the right Regex pattern for these subs without messing anything else up.

Canovice
  • 9,012
  • 22
  • 93
  • 211
  • `b`, `h` could be a string or a number, `[]` is meant to be indexing a dataframe but i forgot the dataframe variable itself, will update - okay see updates – Canovice Apr 01 '21 at 19:38
  • 2
    RStudio has a code format feature. Highlight the code, then select Code / Reformat Code. It will make these changes and do more. – Gregor Thomas Apr 01 '21 at 19:41
  • 2
    Suggested duplicate - [autoformat code in RStudio](https://stackoverflow.com/q/15703553/903061). – Gregor Thomas Apr 01 '21 at 19:43
  • I don't want it to do more, I just want this specific change. RStudios code format feature re-structures my entire script, which I don't want. – Canovice Apr 01 '21 at 19:52
  • Not really a duplicate as the question is asking about specific regex to make certain replacements to improve code style, not about using RStudios code formatting tool (which does line breaks and other styles that we don't want) – Canovice Apr 01 '21 at 19:53
  • 1
    Yes, with your clarification it's not a duplicate as you only want to improve your code style a little bit ;) – Gregor Thomas Apr 01 '21 at 19:58
  • the default RStudio code styles involve far too many line breaks than I prefer is all – Canovice Apr 01 '21 at 20:02
  • 1
    Not related to your question but in your example code, you don't need to use multiple `mutate()` calls. It can all be done in one call: `k %>% mutate(mm = 'new col', nn = 'new col', oo = 'new col')` – Phil Apr 02 '21 at 02:46

1 Answers1

1

Try these patterns (the quotes indicate the start and end of the pattern, you won't need them in the RStudio find/replace boxes). Basically whenever you want exactly 1 space, we use the * quantifier to replace any number of spaces (including 0 spaces) with 1 space.

Do be careful - this will format all your code, including any quoted strings.

Find       Replace
" *<- *"   " <- "
" *%>%"    " %>%"
", *"      ", "
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294