Colons Equals Operator in R? New Syntax

colons equals operator in R? new syntax?

In this case, := is simply ggvis' syntax for assigning fixed values; in contrast, = would here be used to assign a variable value. As you might have noticed in your code example, on the right hand side, there are only such values as "red" or NA, therefore := is the right operator to use in this context. If you would like "size" to depend on the "mpg" column, for example, you could write size = mpg, using the usual equals sign.

I admit that I am not familiar enough with := to say whether there are other packages which have adopted this operator as well.

From http://ggvis.rstudio.com/properties-scales.html (see for further examples and information):

"The props() function uses the = operator for mapping (scaled), and the
:= operator for setting (unscaled). It also uses the ~ operator to
indicate that an expression should be evaluated in the data (and in
ggvis, the data can change); without the ~ operator, the expression is
evaluated immediately in the current environment. Generally speaking,
you’ll want to use ~ for variables in the data, and not use it for
constant values."

R: What do you call the :: and ::: operators and how do they differ?

It turns out there is a unique way to access help info for operators such as these colons: add quotations marks around the operator. [E.g., ?'::' or help(":::")].

  • Also, instead of quotation marks, back-ticks (i.e, ` ) also work.

Double Colon Operator and Triple Colon Operator

The answer to the question can be found on the help page for "Double Colon and Triple Colon Operators" (see here).

For a package pkg, pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name. The package namespace will be loaded if it was not loaded before the call, but the package will not be attached to the search path.

The difference can be seen by examining the code of each:

> `::`
function (pkg, name)
{
pkg <- as.character(substitute(pkg))
name <- as.character(substitute(name))
getExportedValue(pkg, name)
}
<bytecode: 0x00000000136e2ae8>
<environment: namespace:base>

> `:::`
function (pkg, name)
{
pkg <- as.character(substitute(pkg))
name <- as.character(substitute(name))
get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x0000000013482f50>
<environment: namespace:base>

:: calls getExportedValue(pkg, name), returning the value of the exported variable name in the package's namespace.

::: calls get(name, envir = asNamespace(pkg), inherits = FALSE), searching for the object name in the Namespace environment of the package, and returning the value of the internal variable name.


So, what exactly is a namespace?

This site does a good job of explaining the concept of namespaces in R. Importantly:

As the name suggests, namespaces provide “spaces” for “names”. They provide a context for looking up the value of an object associated with a name.

What is the difference between := and = in Go?

= is assignment. more about assignment in Go: Assignments

The subtle difference between = and := is when = used in variable declarations.

General form of variable declaration in Go is:

var name type = expression

the above declaration creates a variable of a particular type, attaches a name to it, and sets its initial value. Either the type or the = expression can be omitted, but not both.

For example:

var x int = 1
var a int
var b, c, d = 3.14, "stackoverflow", true

:= is called short variable declaration which takes form

name := expression

and the type of name is determined by the type of expression

Note that: := is a declaration, whereas = is an assignment

So, a short variable declaration must declare at least one new variable. which means a short variable declaration doesn't necessarily declare all the variables on its left-hand side, when some of them were already declared in the same lexical block, then := acts like an assignment to those variables

For example:

 r := foo()   // ok, declare a new variable r
r, m := bar() // ok, declare a new variable m and assign r a new value
r, m := bar2() //compile error: no new variables

Besides, := may appear only inside functions. In some contexts such as the initializers for "if", "for", or "switch" statements, they can be used to declare local temporary variables.

More info:

variable declarations

short variable declarations

What's the difference in using a semicolon or explicit new line in R code

At the console, the script is evaluated as soon as the a line ends with a complete statement. Hence, this:

temp_a 
temp_b <- 1
temp_c <- 2

is equivelent to calling this:

source(textConnection('temp_a'))
source(textConnection('temp_b <- 1'))
source(textConnection('temp_c <- 2'))

in which each line is evaluated as soon as it is encountered, and failures
in previous lines don't prevent the subsequent lines from being evaluated.
On the other hand. this:

temp_a ; temp_b <- 1 ; temp_c <- 2

is equivalent to calling this:

source(textConnection('temp_a ; temp_b <- 1 ; temp_c <- 2'))

which is equivalent to this

source(textConnection('
temp_a
temp_b <- 1
temp_c <- 2'))

because when first line fails, the remainder of the code is not run.

Incidentally, if you want to mimic this behavior at the console, you can
take advantage of the fact that the lines are not evaluated until they make
a complete statement, by surrounding the three lines with braces to make a
single code block that is evaluated as a whole, like so:

{
temp_a
temp_b <- 1
temp_c <- 2
}

TypeScript : Colon vs Equal To ( Angular Tutorial )

Look again at the AppComponent:

export class AppComponent { 
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}

The first field definition:

title = 'Tour of Heroes';

is assigning a string value. Because that value is a string, TS can infer the type. The line is equivalent to:

title: string = 'Tour of Heroes';

The second field definition

hero: Hero = {
id: 1,
name: 'Windstorm'
};

is assigning an object, which could be any type. So here, to get the most out of TS, we should be specific about the kind of thing it will be. It could also be written:

hero: { id: number, name: string } = {...};

In the Hero class, you're only setting types, no default values. The pattern is actually the same in both:

name[: type][ = value];

It's probably worth having a look in the TS handbook for more information about types and class definitions.



Related Topics



Leave a reply



Submit