14

While reading http://ggvis.rstudio.com/interactivity.html, I noticed the code has := sprinkled in it. I assume that is a new way of providing arguments to a function? What is it exactly?

mtcars %>%
  ggvis(~wt, ~mpg, size := input_slider(10, 1000)) %>%
  layer_points(fill := "red") %>%
  layer_points(stroke := "black", fill := NA)
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Chris
  • 1,219
  • 2
  • 11
  • 21

1 Answers1

12

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."

maj
  • 2,479
  • 1
  • 19
  • 25
  • 12
    Package data.table uses the `:=` syntax for assignment of values within a `j` statement. In the `data.table` context, this is to avoid creating copies of the entire object (as would happen with `<-` or `=`), which can be very slow for large objects. – Sycorax Aug 18 '15 at 15:47
  • I thought introduced operators had to be surrounded by %% – Chris Aug 18 '15 at 15:48
  • Is it possible to create my own := operator? – Chris Sep 24 '15 at 20:54
  • 3
    @Chris: yep, you just use `\`:=\` = function(x,y){...}` where `...` probably uses [Non-Standard Evaluation tricks](http://adv-r.had.co.nz/Computing-on-the-language.html) like `substitute()` and friends. (better a little late than a little never) – Jthorpe Nov 03 '17 at 22:50