The important part is to put the $ at the end of the line and not in the next line, as R then continues to parse the following line and perceive it as one expression,
(Like with pipes, but also works for other stuff like +, &, etc...).
In other words: as long as it's not a complete expression at the end of the line, the parser continues adding the following line.
I think I recently read that somewhere but I forgot where