Convert multiple lines to single line using RStudio text editor

Viewed 329

Does the RStudio text editor have a simple automated way to convert this:

c(
"097",
"085",
"041",
"055"
)

to this: c("097", "085", "041", "055")?

There are answers for other text editors (e.g., here), but I'm looking for a solution specific to RStudio.

5 Answers

Not an automated solution per se, but many people don't know about RStudio's support for multiline cursors. Just hold Alt (Option on Mac) and drag across multiple lines, then press backspace a few times:

enter image description here

Also very handy for adding commas or closing parentheses to multiple lines at once.

The multi-line cursor answer from @jdobres is how I'd do it, but you should also be aware of the excellent datapasta package, which can do what you're asking and a whole lot more. A very helpful add-in for RStudio, imo.

As mentioned by @rdelrossi, datapasta is a helpful package for editing and styling text/code. On Mac, once you install the package (also installs as an add-in), then you have access to several keyboard shortcuts. For example, to convert a vector from vertical to horizontal, you could use vector_paste() or use the keyboard shortcut, which is ctrl+alt+shift+v.

enter image description here

You can read about some of the other functions and shortcuts (e.g., paste as vertical vector) at Typical Datapasta Usage with Rstudio.

You can use dput:

dput(c(
"097",
"085",
"041",
"055"
))
#> c("097", "085", "041", "055")

It is also possible to set-up a code formatting add-in that you can bind to keyboard shortcuts by installing the styler package.

RStudio has a find/replace feature. You can replace new line characters \n with spaces for this effect. Make sure you have the "regex" box checked for it to work.

enter image description here

Related