Convert multiple lines to single line using RStudio text editor - r

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.

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:
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.
You can read about some of the other functions and shortcuts (e.g., paste as vertical vector) at Typical Datapasta Usage with Rstudio.

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.

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.

Related

Atom Editor - multiple selections to rename variables quickly doesn't recognize scope

Sublime text has a feature that allows you to highlight multiple identical selections to rename variables quickly. It is initiated with Ctrl+D on your keyboard. Most importantly it is context aware. If I want to rename the variable is to xy, it won't, for instance, rename this to thxy.
How do I get Atom to recognize scope? If I Ctrl+D the same as in Sublime it does not differentiate between is and this.
Highlight the first two letters in this code chunk below (ie 'is') and press Ctrl+D. It's jumps to the 'is' in this.
is
this is really cool
code
this is really cool
code
My answer is valid for JavaScript, for which I had the same question as you did.
I installed two more packages in atom:
https://atom.io/packages/refactor
https://atom.io/packages/js-refactor
The first one is the core package i think and the second one is javascript-specific. It worked after installing and restarting atom one time.
For you this package alone should do: https://atom.io/packages/cpp-refactor

How to highlight same words with shift-8(*) in Atom under Vim-mode?

This is the effect I am looking for:
Namely I want to highlight all instances of the selected word.
I don't actually use Vim Mode in Atom, however I get the same effect of highlighting all instances of the selected word with the highlight-selection package:
I strongly suspect it will work just as well with Vim Mode as it does in normal Atom Mode.

What's an easier way to do tab completion of many superscripts?

I know I can type x\^n and tab to get xⁿ.
But how can I input xᵃ⁺¹ easily, without doing x\^a tab \^+ tab \^1 tab.
I tried using brackets, that doesn't work.
At the moment, there's no shortcut for expanding extended latex superscripts or subscripts via tab expansion. Julia is not actually interpreting the latex you write at tab time – all the symbols are hard-coded in the Julia source as a mapping from simple latex to unicode (you can find them all here). A better approach might be to define some keyboard shortcuts to write unicode sub/superscripts directly.

R & Inkscape: text labels in SVG graphics exported from R did not recognized as a text in Inkscape

I constructed dendrogram in R with the code:
data(iris)
aver<-sapply(iris[,-5],function(x) by(x,iris$Species,mean))
matrix<-dist(aver)
clust<-hclust((matrix),"ave")
clust$labels<-row.names(aver)
plot(as.dendrogram(clust))
I wanted to save the dendrogram as svg file using the code:
install.packages("Cairo")
library(Cairo)
svg("plot.svg")
plot(as.dendrogram(clust))
dev.off()
Here the problem started:
When I imported the "plot.svg" into Inkscape (ver: 0.48.4) and selected any label (e.g. "setosa") it was not recognized as a text, but rather as some "user defined" object. Specifically, when I selected any "letter" in the label and inspect it with the XML Editor (ctrl+shift+X) in Inkscape I obtained this information:
**id**: use117
**x**: 142.527344
**xlink:href**: #glyph0-8
**y**: 442.589844
On the other hand, when I manually wrote "setosa" using "create and edit text objects" tool, and inspected in XML Editor, it returned:
**id**: text4274
**sodipodi:linespacing**: 125%
**style**: font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Palatino Linotype;-inkscape-font-specification:Palatino Linotype
**transform**: scale(0.8,0.8)
**x**: 176.02016
**xml:space**: preserve
**y**: 596.96674
It is likely that Inkscape did not recognize the labels as a text according to the attribute "id" from XML Editor. Hence, I am not able to change neither font, size as well as use other functions related to text objects in Inkscape.
Here is the svg file, that I made with the previous code and imported into Inkscape
I checked previous steps using other versions of Inkscape as well as R, but it would be the same.
Here is the question:
Do you have any suggestion how I can gather labels as a text attribute instead of a "user defined" (or whatever it is object...) when importing svg files from R into Inkscape?
UPDATE
#baptiste linked to the SO thread where #Oscar Perpiñán suggested three packages (gridSVG, SVGAnnotation and RSVGTipsDevice) that manipulate SVG. Unfortunately, neither of packages suggested could solve the problem with the text issue.
So far I found SO thread where #Mo Sander suggested RSvgDevice package since it can preserve text object rather than glyphs. Being stuck with the RSvgDevice installation procedure, I found that it RSvgDevice is only available for 32-bit installations and R < 2.15.0. Otherwise, R returned warning message:
Warning message:
package ‘RSvgDevice’ is not available (for R version 3.0.1)
Beside the requirements for older R versions, currently only RSvgDevice can preserve a text object in SVG.
I'm a bit late to the party, but I've been dealing with this myself. I found a trick to make it work. First, I export the plot as PDF instead of SVG because PDF fonts are recognized by inkscape.
This, however brings a new problem as the text often ends up being defined letter by letter meaning that you can change the font, but the spacing is still defined and it becomes immensely annoying. I found that it was due to the x coordinate being defined at each letter.
I wrote a perl script and put it in this gist to remove all the trailing coordinates. After that I'm able to manipulate all the fonts I wished. Note, that this will only work for horizontal text.
Hope that helps this problem you had over a year ago :)
This is a failing in Cairo. Major, from my point of view.
The cairo SVG surface (i.e. the back-end in Cairo used to "draw" on SVG) simply does not support the "text" tag. It does not understand about strings at all. Instead, it places each character (glyph) individually. So any SVG generated with Cairo is not useful if you want to post-process contained text with a vector editor. :(
The only mention I found on the cairo list was this one:
http://lists.cairographics.org/archives/cairo/2011-February/021777.html
The svglite package exports text on Linux as desired.
[EDIT] According to this thread, there is also a way to remove the squeezing of the edited text into the fixed box width. Just remove the textLength field from the object in the XML editor.
Cheers
Can't directly comment on mgrewe answer because of my low reputation but thank you for the solution.
Implemented the textLength edit into R:
svgitem<-readLines('file.svg')
svgitem<-gsub('textLength=','tL=',svgitem)
writeLines(svgitem,'without_textLength.svg')
Text-box seems to be no longer affected after edition in Inkscape using the without_textLength.svg file and keeps a trace of old textLength renamed 'tL'.
Thanks again mgrewe, I've lost so many hours reformating text in Inkscape before seeing your answer.
R is clearly not using the standard SVG text objects for producing its labels. I have no idea why. I am not an R user.
Perhaps by default it uses it's own custom font that it manually inserts glyph-by-glyph into the output. Are you using the same font in both cases? In Inkscape you are using Palatino. Is that what you are using for the labels in R?

Hide and show comments

I'm writing all my scripts on .R file using R for mac. It is convenient to me because there are colors to highlight the type of commands.
I have a many comments following the # symbol that are useful when I forget about the meaning of my script but they tend to blur my script so that it gets harder to find a given command line.
Is there a way to hide and show these comments ? (Using the programm I'm currently using or another one). What would you suggest as the best program to write R script ?
Thanks a lot !
RStudio supports code folding. You can standardize your comment blocks so that they are recognized as code blocks.
For example, enter this into your RStudio editor
#=======================================================
# this is a comment block
# more comments here
# comments upon comments
and then press Alt+L to fold, and Alt+Shift+L to unfold.
Try RStudio for mac. One of the greatest code writing environment for R there is.
You can also try Emacs, which is more like old-fashioned command line editor. You can find a good guide here.

Resources