colons equals operator in R? new syntax?

巧了我就是萌 提交于 2019-11-29 12:43:34

问题


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)

回答1:


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



来源:https://stackoverflow.com/questions/32077483/colons-equals-operator-in-r-new-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!