Partial matching of function argument

余生长醉 提交于 2019-11-26 23:03:09

Partial matching exists to save you typing long argument names. The danger with it is that functions may gain additional arguments later on which conflict with your partial match. This means that it is only suitable for interactive use – if you are writing code that will stick around for a long time (to go in a package, for example) then you should always write the full argument name. The other problem is that by abbreviating an argument name, you can make your code less readable.

Two common good uses are:

  1. len instead of length.out with the seq (or seq.int) function.

  2. all instead of all.names with the ls function.

Compare:

seq.int(0, 1, len = 11) 
seq.int(0, 1, length.out = 11)

ls(all = TRUE)
ls(all.names = TRUE)

In both of these cases, the code is just about as easy to read with the shortened argument names, and the functions are old and stable enough that another argument with a conflicting name is unlikely to be added.

A better solution for saving on typing is, rather than using abbreviated names, to use auto-completion of variable and argument names. R GUI and RStudio support this using the TAB key, and Architect supports this using CTRL+Space.

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