nicholas
nicholas

Reputation: 983

Convert multiple lines to single line using RStudio text editor

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.

Upvotes: 10

Views: 1031

Answers (5)

AndrewGB
AndrewGB

Reputation: 16856

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.

Upvotes: 4

rdelrossi
rdelrossi

Reputation: 1154

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.

Upvotes: 5

jdobres
jdobres

Reputation: 11957

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.

Upvotes: 15

Allan Cameron
Allan Cameron

Reputation: 173813

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.

Upvotes: 3

Gregor Thomas
Gregor Thomas

Reputation: 145775

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

Upvotes: 3

Related Questions