[Rd] named arguments discouraged in `[.data.frame` and `[<-.data.frame`

Michael Lawrence l@wrence@mich@el @ending from gene@com
Wed Nov 28 20:36:20 CET 2018


Whenever they are calling a primitive, because primitives match
arguments positionally. Of course, you then you need to introduce the
concept of a primitive.

You could also make an argument from the code clarity perspective, as
typically primitives have simple interfaces and/or are used frequently
enough that naming arguments just introduces clutter. That probably
requires experience though.

Michael
On Wed, Nov 28, 2018 at 11:30 AM Henrik Pärn <henrik.parn using ntnu.no> wrote:
>
> tl;dr:
>
> Why are named arguments discouraged in `[.data.frame`, `[<-.data.frame` and `[[.data.frame`?
>
> (because this question is of the kind 'why is R designed like this?', I though R-devel would be more appropriate than R-help)
>
> #############################
>
> Background:
>
> Now and then students presents there fancy functions like this:
>
> myfancyfun(d,12,0.3,0.2,500,1000,FALSE,TRUE,FALSE,TRUE,FALSE)
>
> Incomprehensible. Thus, I encourage them to use spaces and name arguments, _at least_ when trying to communicate their code with others. Something like:
>
> myfancyfun(data = d, n = 12, gamma = 0.3, prob = 0.2,
>                       size = 500, niter = 1000, model = FALSE,
>                      scale = TRUE, drop = FALSE, plot = TRUE, save = FALSE)
>
>
> Then some overzealous students started to use named arguments everywhere. E-v-e-r-y-w-h-e-r-e. Even in the most basic situation when indexing vectors (as a subtle protest?), like:
>
> vec <- 1:9
>
> vec[i = 4]
> `[`(x = vec, i = 4)
>
> vec[[i = 4]]
> `[[`(x = vec, i = 4)
>
> vec[i = 4] <- 10
> `[<-`(x = vec, i = 4, value = 10)
>
> ...or when indexing matrices:
>
> m <- matrix(vec, ncol = 3)
> m[i = 2, j = 2]
> `[`(x = m, i = 2, j = 2)
> # 5
>
> m[i = 2, j = 2] <- 0
> `[<-`(x = m, i = 2, j = 2, value = 0)
>
> ######
>
> This practice indeed feels like overkill, but it didn't seem to hurt either. Until they used it on data frames. Then suddenly warnings appeared that named arguments are discouraged:
>
> d <- data.frame(m)
>
> d[[i = "X2"]]
> # [1] 4 5 6
> # Warning message:
> # In `[[.data.frame`(d, i = "X2") :
> #  named arguments other than 'exact' are discouraged
>
> d[i = 2, j = 2]
> # [1] 0
> # Warning message:
> # In `[.data.frame`(d, i = 2, j = 2) :
> #  named arguments other than 'drop' are discouraged
>
> d[i = 2, j = 2] <- 5
> # Warning message:
> # In `[<-.data.frame`(`*tmp*`, i = 2, j = 2, value = 5) :
> #  named arguments are discouraged
>
>
> ##################################
>
> Of course I could tell them "don't do it, it's overkill and not common practice" or "it's just a warning, don't worry". However, I assume the warnings are there for a good reason.
>
> So how do I explain to the students that named arguments are actively discouraged in `[.data.frame` and `[<-.data.frame`, but not in `[` and `[<-`? When will they get bitten?
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list