36

How can one wrap commands within a lengthy line of code in the text editor in RStudio such that RStudio recognizes it as the continuation of a single piece of code? Currently, my code is 1 incredibly long line that is difficult to manage. With my current RStudio configuration, "Enter" seems to establish a new and separate line of code (without an indent) that RStudio doesn't process correctly because it begins with a "+".

Soft wrap (auto wrap based upon width of the text editor window) is not what I'm looking for.

Example what what I'm trying to achieve:

ggplot(C, aes(x=Tenure, y = Count, color=Gender, shape=Gender))
     + geom_point(size=1)
     + geom_smooth(aes(fill=Gender))
     + labs(x="Tenure", y="Closeness")
     + ggtitle("Title")

Shift + Enter and Control + Enter are not doing the trick.

3
  • 7
    Leave the + at the end of the line Commented Oct 6, 2016 at 16:20
  • 2
    It's not RStudio that is behaving that way, but rather R itself. Commented Oct 6, 2016 at 16:30
  • Thanks R. Schifini! That did it exactly. I noted that once executed, the code takes on the form of my example in the console window. Commented Oct 6, 2016 at 16:39

1 Answer 1

71

"4. Enable code wrapping If, like me, you like to work with multiple windows up at once, you will undoubtedly run into the issue of not being able to see your code without scrolling sideways. While the best solution is to simply get in the habit of chopping your code up into shorter, more legible segment, a fair alternative is to have R Studio “soft wrap” it for you. All this means is that any part of your R script that would usually fall off the side of your screen is, instead, continued on the following line. Give it a try:"

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

is there any shortcut to toggle it?
@ismailsunni In Options > Code > Editing you can not only check the box for "Soft-wrap R source files" but also set a keybinding for that. Click on "Modify Keyboard Shortcuts" next to "Keybindings" and search for "Toggle Soft Wrap Mode". (At least in Version 1.4.1094)
In RStudio 2025.05.0 when a project is open, the soft-wrapping option is set under the "Code" menu: that is select "Code" and then "Soft Wrap Long Lines"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.