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.
I wanted to answer a question regarding plotmath but I failed to get my desired substitute output.
My desired output:paste("Hi", paste(italic(yes),"why not?"))
and what I get: paste("Hi", "paste(italic(yes),\"why not?\")")
text<-'paste(italic(yes),"why not?")'
text
[1] "paste(italic(yes),\"why not?\")"
noqoute_text<-noquote(text)
noqoute_text
[1] paste(italic(yes),"why not?")
sub<-substitute(paste("Hi",noqoute_text),
env=list(noqoute_text=noqoute_text))
sub
paste("Hi", "paste(italic(yes),\"why not?\")")
You're using the wrong function, use parse instead of noquote :
text<-'paste(italic(yes),"why not?")'
noquote_text <- parse(text=text)[[1]]
sub<- substitute(paste("Hi",noquote_text),env=list(noquote_text= noquote_text))
# paste("Hi", paste(italic(yes), "why not?"))
noquote just applies a class to an object of type character, with a specific print method not to show the quotes.
str(noquote("a"))
Class 'noquote' chr "a"
unclass(noquote("a"))
[1] "a"
Would you please elaborate on your answer?
In R you ought to be careful about the difference between what's in an object, and what is printed.
What noquote does is :
add "noquote" to the class attribute of the object
That's it
The code is :
function (obj)
{
if (!inherits(obj, "noquote"))
class(obj) <- c(attr(obj, "class"), "noquote")
obj
}
Then when you print it, the methods print.noquote :
Removes the class "noquote" from the object if it's there
calls print with the argument quote = FALSE
that's it
You can actually call print.noquote on a string too :
print.noquote("a")
[1] a
It does print in a similar fashion as quote(a) or substitute(a) would but it's a totally different beast.
In the code you tried, you've been substituting a string instead of a call.
For solving the question I think Moody_Mudskipperss answer works fine, but as you asked for some elaboration...
You need to be careful about different ways similar-looking things are actually stored in R, which means they behave differently.
Especially with the way plotmath handles labels, as they try to emulate the way character-strings are normally handled, but then applies its own rules. The 3 things you are mixing I think:
character() is the most familiar: just a string. Printing can be confusing when quotes etc. are escaped. The function noquote basically tells R to mark it's argument, so that quotes are not escaped.
calls are "unevaluated function-calls": it's an instruction as to what R should do, but it's not yet executed. Any errors in this call don't come up yet, and you can inspect it.
Note that a call does not have its own evironment given with it, which means a call can give different results if evaluated e.g. from within a function.
Expressions are like calls, but applied more generally, i.e. not always a function that needs to be executed. An expression can be a variable-name, but also a simple value such as "why not?". Also, expressions can consist of multiple units, like you would have with {
Different functions can convert between these classes, but sometimes functions (such as paste!) also convert unexpectedly:
noquote does not do that much useful, as Moody_Mudskipper already pointed out: it only changes the printing. But the object basically remains a character
substitute not only substitutes variables, but also converts its first argument into (most often) a call. Here, the print bites you, for when printing a call, there is no provision for special classes of its members. Try it: sub[[3]] from the question gives[1] paste(italic(yes),"why not?")
without any backslashes! Only when printing the full call the noquote-part is lost.
parse is used to transform a character to an expression. Nothing is evaluated yet, but some structure is introduced, so that you could manipulate the expression.
paste is often behaving annoyingly (although as documented), as it can only paste together character-strings. Therefore, if you feed it anything but a character, it firs calls as.character. So if you give it a call, you just get a text-line again. So in your question, even if you'd use parse, as soon as you start pasting thing together, you get the quotes again.
Finally, your problem is harder because it's using plotmaths internal logic.
That means that as soon as you try to evaluate your text, you'll probably get an error "could not find function italic" (or a more confusing error if there is a function italic defined elsewhere). When providing it in plotmath, it works because the call is only evaluated by plotmath, which will give it a nice environment, where italic works as expected.
This all means you need to treat it all as an expression or call. As long as evaluation cannot be done (as long as it's you that handles the expression, instead of plotmath) it all needs to remain an expression or call. Giving substitute a call works, but you can also emulate more closely what happens in R, with
call('paste', 'Hi', parse(text=text)[[1]])
I hope to get some help on the use of quotation marks within a string for get().
Say, I want to retrieve an element from a list
some_list <- list(element1=11,element2=22,element3=33)
naturally, I can simply reference this element through
some_list[['element1']]
However, once I use this as a string within get(), R throws this error message
get("some_list[['element1']]")
> Error in get("some_list[['element1']]") :
object 'some_list[['element1']]' not found
I cannot figure out why this is the case. get() works fine when used with strings that do not have quotation marks within them, e.g.
get("some_list")
I also tried escaping the quotation marks within the string (although I don't this I would need to since they are single quotation marks) but it does not work either.
some_list[["\'"element1"\'"]]
What am I missing.
get won't do that.
some_list[['element1']] isn't the name of an object in an R environment (in a technical sense). When you type some_list[['element1']] at the console, R parses the expression, looks up the symbol some_list and then calls the function [[. get is intended just for the symbol lookup piece of that.
(Technically, my sequence of events there probably isn't right, but I listed them that way to help make the issue clear. Really, R is just parsing the expression, and then calling [[ with arguments some_list and 'element1', and those symbols are subsequently looked up.)
The quotes have nothing to do with it. Run:
get("some_list")[['element1']]
I am trying to use regex generators to create an expression, but I can't seem to get it right.
What I need to do is find the following type of string in a string:
community_n
For example, within the string which may be
community community_1 community_new_1 community_1_new
from that, I just want to extract community_1
I have tried /(community_\\d+)/, but that is clearly not right.
Try adding word boundries, so
/(\\bcommunity_\\d+\\b)/
Try using the regex (community_\d+).
Though I could be incorrect since I don't know which language you are using.
(For some reason I cannot add comments, I can only answer questions).
In R, I have a function which takes the name of another function as a parameter. I've constructed an if-statement within the parent function to check if the input function name is the same as the name of an already existing function of the name strategy_function.
function_parent <- function(function_name){
if(function_name == strategy_function){...}
}
However, R does not appreciate this notation. Is using the name of a function in this way possible, and even if it is, is there a better way? This seems slightly sloppy.
Try quotes around strategy_function:
function_parent <- function(function_name){
if(function_name == "strategy_function"){...}
}
Using deparse(substitute(strategy_function)) in the comparison did the trick.