R: List of operators available for assignment - r

I am interested it writing a few operators. Many characters are reserved and cannot be used a or b for example while others are currently used and I would not like them overwritten +,-,>, and < for example. There are others which are unavailable for less clear reasons such as $ or #.
I would like a list of characters that can be used as user written operators.
Thanks for your help,
Francis

Both "?" and "!" can be overloaded. I'm not going to reproduce the source code here, but take a look at the sos package and at the cgwtools::splatnd function for info on how to write your own unary (single-argument) operators.
I believe there's a tutorial on how to write %foo% binary operators but I forget where I saw it :-(

Related

How to set up custom automatic character replacement in emacs ess?

One of the useful features of ess-mode (Emacs speaks statistics) is to automatically replace the underscore _ with the assignment operator <-. Lately, I have been using a lot of pipes (written as %>%) and it would be great to not have to type three characters for each pipe.
Is it possible to define a custom key binding for the pipe, similar to the one converting _ into ->?
The simplest solution is to just bind a key to insert a string:
(define-key ess-mode-map (kbd "|") "%>%")
You can still insert | with C-q |. I'm not sure about the map's name; you'll almost certainly want to limit the key binding to ess-mode.
Check out yasnippet. You can use it to define something like "if this sequence of characters is followed by this key (which you can define to whatever you like), then replace them with this other sequence of characters and leave the cursor in this place". There's more to yasnippet than this, but there's plenty of documentation online and even already made recipes similar to the example I gave above that you can try, like yasnippet-ess-mode, for example.
Alternatively, you can also try abbrev-mode and see if that works for you.
I, for one, like yasnippet better, since you can also specify where to leave the cursor after the expansion, but abbrev-mode seems to be easier to set up. As always in Emacs world, try multiple solutions, don't settle for the first one you put your hands on. What works best for others might not work for you, and vice-versa.

Does "!!" have a special meaning in GTM scripts?

I came across the following syntax in a GTM script and am not quite sure how it's supposed to work:
if(!!{{var1}} && !!{{var2}}){
// . . . other code here
}
Now, I can tell that the {{}} syntax is used to capture variable values, but I can't find any reference to the !! operator (if there is such a thing).
Does it represent something in GTM, or is it just sloppy programming and these !! can be removed? I should mention that there are many more if statements in this script and they all look sane. This is the only one that has these !!. Any ideas?
This is not GTM script specific, it's a standard JavaScript practice to coerce objects to boolean. In JavaScript there are falsey values like null or undefined, etc. (list here), these will be false after using !! (bang bang), otherwise, true.
Note that this is not a special operand, just ! (NOT) used twice to force the type to be boolean.
Here is a good article that explains it very well: https://medium.com/#edplatomail/js-double-bang-or-the-not-operator-part-40e55d089bf0
To answer your question, it is not sloppy programming, and should not be removed.

How to process latex commands in R?

I work with knitr() and I wish to transform inline Latex commands like "\label" and "\ref", depending on the output target (Latex or HTML).
In order to do that, I need to (programmatically) generate valid R strings that correctly represent the backslash: for example "\label" should become "\\label". The goal would be to replace all backslashes in a text fragment with double-backslashes.
but it seems that I cannot even read these strings, let alone process them: if I define:
okstr <- function(str) "do something"
then when I call
okstr("\label")
I directly get an error "unrecognized escape sequence"
(of course, as \l is faultly)
So my question is : does anybody know a way to read strings (in R), without using the escaping mechanism ?
Yes, I know I could do it manually, but that's the point: I need to do it programmatically.
There are many questions that are close to this one, and I have spent some time browsing, but I have found none that yields a workable solution for this.
Best regards.
Inside R code, you need to adhere to R’s syntactic conventions. And since \ in strings is used as an escape character, it needs to form a valid escape sequence (and \l isn’t a valid escape sequence in R).
There is simply no way around this.
But if you are reading the string from elsewhere, e.g. using readLines, scan or any of the other file reading functions, you are already getting the correct string, and no handling is necessary.
Alternatively, if you absolutely want to write LaTeX-like commands in literal strings inside R, just use a different character for \; for instance, +. Just make sure that your function correctly handles it everywhere, and that you keep a way of getting a literal + back. Here’s a suggestion:
okstr("+label{1 ++ 2}")
The implementation of okstr then needs to replace single + by \, and double ++ by + (making the above result in \label{1 + 2}). But consider in which order this needs to happen, and how you’d like to treat more complex cases; for instance, what should the following yield: okstr("1 +++label")?

Escaping characters for text mode in AUCTeX [duplicate]

When editing an Sweave document in LaTeX (using the Noweb mode), Emacs knows to "ignore" code that is in <<>>= blocks. However, for interstitial \Sexpr{} blocks, this isn't the case. Given that R references by columns via '$' and LaTeX uses $ to set-off equations, these \Sexpr{} blocks often break the syntax highlighting, like so:
I have a very rudimentary understanding the elisp & Emacs syntax highlighting, but my hope is that it might be possible to add something to .emacs that will disable any parsing/$ detection within \Sexpr{}'s.
I thought emacs with ESS has correct syntax highlighting for Sweave?
Anyway, the easiest "fix" is to just not use the $ operator but [[ instead. For example:
foo$p.value
foo[['p.value']]
Should give the same result. I think foo$p.value is just short for foo[["p.value",exact=FALSE]]
I don't have a fix either, but I'll pass along my workaround, which is to never (well, rarely) do any processing in \Sexpr chunks but instead to store things I want to use in \Sexpr in variables, and to do so in the same chunk I do the main calculations in.
<<echo=FALSE, results=hide>>=
t1 <- chisq.test(someVar)
p1 <- formatC(t1$p.value, format="%f", digits=2)
#
\dots with a $p$-value of \Sexpr{p1}.
While there are some downsides to this, I find it helps me to better keep track of what I want to present, and how I want to present it.
As an aside, consider using formatC instead of round as it can keep significant zeros (ie, 0.10 instead of 0.1).
I have no good answer for you as I am not an Emacs hacker, so I usually do one of two things:
Either add a simple % $ comment at the of the line to "close" the math expression from $ to $,
Or rewrite the expression to not use $-based subsetting:
round(as.numeric(chisq.test(someVar)["p.value"]), 2).

List of all the R keywords

I want to enable the auto-complete functionality in emacs for editing my R files. For this, I need to have listed all the keywords in the R language. Does someone know if this is available somewhere? I know I would have to include all the functions names in the external packages I am using, but for now the list of what is in r-cran-base should be fine for me.
Thanks a lot!
apropos with an empty string argument will list all objects on the search path. It is what is used for the tab complete in the default GUI.
apropos("")
[1] "-"
[2] "-.Date"
[3] "-.POSIXt"
[4] "!"
[5] "!.hexmode"
[6] "!.octmode"
...
The R Language Definition lists all of R's keywords. Note that those are also reserved.
The following identifiers have a special meaning and cannot be used for object names
if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_
NA_complex_ NA_character_ ... ..1 ..2 etc.
See ?Reserved, ?Control and maybe ?Syntax and ?Ops.
Just a little heads up that what's being discussed here has nothing to do with actual R keywords, which is its own special thing. I suspect this is what #HongOoi is alluding to.
R keywords exists ostensibly to help group functions by theme, but, except for the special case of internal, isn't widely used.
If you want to see the list of valid keywords you can get it like this
readLines(file.path(R.home("doc"), "KEYWORDS.db"))
try this
ls('package:base')
list all objects in a package
You might go to an R buffer and look at the following variable (given that you have Emacs Speaks Statistics):
ess-R-font-lock-keywords
by using C-h v ess-R-font-lock-keywords.
From there on, you can look in ess-custom.el and find everything you need on the implementation.
Just making sure you really want to do this since ?rcompgen describes the built-in functions by Deepayan Sarkar in the utils-package that already provide "tab-completion".

Resources