Autocompletion does not work in R terminal inside square brackets - r

I am using R from the command line and noticed that autocompletion of list names does not work when inside square brackets. Consider this example:
myList <- list(firstElement=sample(1:10), secondElement=sample(1:10))
Typing out myList$f and then pressing tab gives myList$firstElement
But does not work here (at myList$s inside the brackets):
myList$firstElement[myList$secondElement > 5]
My question is - why it does not work and is there something one can do to make it work?

Putting the term inside round brackets seems to work
myList$firstElement[(myList$
No idea why it doesn't autocomplete without this though.

Related

When does pressing the Enter (Return) key matter when creating a regex matching expression?

I want to search for multiple codes appearing in a cell. There are so many codes that I'd like to write parts of the code in succeeding lines. For example, let's say I am looking for "^a11","^b12", "^c67$" or "^d13[[:blank:]]". I am using:
^a11|^b12|^c67$|^d13[[:blank:]]
This seems to work. Now, I tried:
^a11|^b12|
^c67$|^d13[[:blank:]]
That also seemed to work. However when I tried:
^a11|^b12|^c67$|
^d13[[:blank:]]
It did not count the last one.
Note that my code is wrapped into a function. So the above is an argument that I feed the function. I'm thinking that's the problem, but I still don't know why one truncation works while the other does not.
I realized the answer today. The problem is that since I am feeding the regex argument, it will count the next line in the succeeding code.
Thus, the code below was only "working" because ^c67$ is empty.
^a11|^b12|
^c67$|^d13[[:blank:]]
And the code below was not working because ^d13 is not empty but also this setup looks for (next line)^d13[[:blank:]] instead of just ^d13[[:blank:]]
^a11|^b12|^c67$|
^d13[[:blank:]]
So an inelegant fix is:
^a11|^b12|^c67$|
^nothinghere|^d13[[:blank:]]
This inserts a burner code that is empty which is affected by the line break.

Making the body of a function span multiple lines in the R Studio console?

I know that functions are supposed to look like this:
my_function <- function(x) {
Body
}
My issue is that I can't get the symbol "{" and the symbol "}" on different lines. When I type start typing "my_function <- function(x) {" in the console, the closing brace immediately appears, and if I press enter at any point, I just get a new line.
To answer this specific question, if you delete the closing curly brace it adds before typing enter you can write multi-line functions like that. Also, if you copy/paste from another editor that will also work.
Also, you can disable the auto-brace adding feature by going to Tools->Global Options->Code->Editing and unchecking "Insert matching parens/quotes"

utltsnips - surround with space

I have the following utilsnips script that I use for Vim:
snippet - "assignment"
<-
endsnippet
I use it for R to expand a dash to the assignment operator. I would like to make it so that a space is put both before and after the <- on expansion. However, when I put a space before it in the snippet like <-, it won't expand on hitting Tab. How should I modify the script to have spaces around the operator? Desired result: <-.
You could use r option to include head and trailing spaces around snippets. r will treat snippet as a python regular expression and you should define your snippet within quotes when using this flag.
snippet " -" "assignment" r
<-
endsnippet
Note that there is a space before and after <- in snippet definition.
As a bonus, It's more interesting to define the snippet like the following:
snippet " - " "assignment" rA
<-
endsnippet
A is autoexpansion. so now you dont need to hit tab anymore! just type - and as soon as you type space after - it will expand to <- Automatically.

Parsing strings with grep/str_extract

As part of my feature engineering, I need to parse text strings from different languages and keep text enclosed within parentheses. Everything was going well until I encountered a very strange phenomenon. For some languages, the parentheses I need to find look slightly different, and various regexp options fail.
I'm pasting screen-shots because strangely, copying and pasting the strange parentheses changes it to a 'normal' one, so I can't set up a different regex to find those separately.
Notice that the parentheses in the first entry look normal, but for the second entry, it appears sort of 'sharp'
If I use stringr's str_extract, the first instance works fine, but the second fails.
But, the encodings are the same. Anyone know what's going on?
[Edit: here are the results of dput on these same examples. dput apparently sees the parentheses as equivalent, even though grep does not]
c("Obnaružena poterâ šaga na (Motor šprica pipettora R1).", "(STAT tàn zhen Z zhóu ma dá) tàn cè dào diu bù<U+3002>")
Finally, I am actually copy and pasting the two parentheses from R into the code window below; they do appear different this way. First is normal, second is the strange one.
( (

How to indent multiple lines of code in Rstudio?

I have tried using "reindent lines" after highlighting the lines but that doesn't work (in fact it removes indentation). For example I want a quick way to
take a script like this
#parameters
b=2
c=1
d=4
q=3
and turn it into this
#parameters
b=2
c=1
d=4
q=3
in Rstudio
Highlighting the code of interest and hitting tab should add another level of indentation. Shift-tab will remove a level of indentation.
You could use a shortcut. First, highlight the lines you want to indent, and then press: Ctrl + } (at least, that is the combination on my keyboard).
If you want to move in the other direction, then press: Ctrl + {.
RStudio will let you use Regex in Find and Replace. You can search for \n and replace with \n\t, which will do what (I think) you want.
The automatic indent is designed to format your code so that there is indenting where appropriate (inside a pair of brackets, for example). 'Reindent' is for when there has been automatic indenting applied which is no longer appropriate (eg brackets have been removed).
re format code and reindent works for me. But, for your example, I think it is more based on the space a tab creates. Also, the below link is a good way to write good codes
https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml
http://r-pkgs.had.co.nz/r.html

Resources