-4

I have a long command with a long line of dplyr/tidyr commands:

Object %>% mutate() %>% select() %>% separate() %>% separate() %>% separate %>% separate() %>% separate() %>% select()

and I want to separate it into multiple lines for readability sake, is there a way to do this?

Sky Scraper
  • 148
  • 1
  • 10
  • 4
    Type enter after the pipe. Or are you asking something more complicated? – camille Jan 31 '22 at 21:11
  • Yes. Generally in R you can put a line break anywhere that is **not** a valid end-of-line. Most people put line breaks after every `%>%`. As long as you don't put line breaks right before `%>%` it should be fine. – Gregor Thomas Jan 31 '22 at 21:11
  • @GregorThomas I get a warning when sourcing the code `Error in fBody[[i]] : subscript out of bounds` when I try that.. let me try again tho. – Sky Scraper Jan 31 '22 at 21:13
  • @camille I get a warning when sourcing the code `Error in fBody[[i]] : subscript out of bounds` when I try that.. let me try again tho. – Sky Scraper Jan 31 '22 at 21:14
  • 5
    That seems like a different problem that we won't be able to see without a [mcve] – camille Jan 31 '22 at 21:20
  • I will get back to this later. It only happens when I separate out by hitting return after the `%>%` does it matter that it is within a function? I will come up with an example and update @camille – Sky Scraper Jan 31 '22 at 21:23
  • ok, I had that error because separating the command was disrupting a breakpoint I set. @GregorThomas had the correct answer. thank you, sorry – Sky Scraper Jan 31 '22 at 21:29

2 Answers2

3

R generally continues reading more lines until it has read a complete expression. You can therefore break an expression across multiple lines by ensuring that, at the end of a given line, there’s no complete expression.

Consider

x = 1
+ 2

This code has a complete expression on its first line (x = 1 is a valid, complete expression) so R does not continue to the next line — it evaluates the expression x = 1. Afterwards, x has the value 1.

x = 1 +
2

By contrast, this piece of code does not have a complete expression on its first line (x = 1 + would be invalid) so R continues to the next line and only then evaluates the expression x = 1 + 2. Afterwards, x has the value 3.

The same is true for the %>% operator. So you have multiple choices. For instance, and this is the generally recommended way, you can end lines in %>%:

Object %>%
    mutate() %>%
    select() %>%
    …

Conversely, some people prefer having %>% at the beginning of a line. But to make this work we need to do something to make the expression not-complete. Easy: wrap it in parentheses:

(Object
    %>% mutate()
    %>% select()
    %>% …
)

This second style is decidedly less common (and some people actively dislike it because of the redundant parentheses, which add visual clutter), but it does have its proponents.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

You can put each command on a separate line as long as it ends with the pipe, %>%.

Object %>% 
  mutate() %>% 
  select() %>% 
  separate() %>% 
  separate() %>% 
  separate() %>% 
  separate() %>% 
  separate() %>% 
  select()
norie
  • 9,609
  • 2
  • 11
  • 18