Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

r - list members can be accessed with partial name? Is this a feature?

Consider this R code

> l = list(key = 1)
> l$k
[1] 1
> l$ke
[1] 1
> l[['k']]
NULL
> names(l)
[1] "key"

Does this mean that you can access a list member with $ and its partial name? I couldn't believe my eyes when I discovered this after a frustrating bug-hunting.

Is this a feature of R list? Is there a name for it? Is it possible to turn it off? This causes obvious troubles when you use it like a Python dict.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a feature that is meant to help in interactive mode. You can tighten it locally, see help(options) which has

 ‘warnPartialMatchArgs’: logical.  If true, warns if partial
      matching is used in argument matching.

 ‘warnPartialMatchAttr’: logical.  If true, warns if partial
      matching is used in extracting attributes via ‘attr’.

 ‘warnPartialMatchDollar’: logical.  If true, warns if partial
      matching is used for extraction by ‘$’.

Example:

R> l <- list(key = 1)
R> l$k
[1] 1
R> options("warnPartialMatchDollar"=TRUE)
R> l$k
[1] 1
Warning message:
In l$k : partial match of 'k' to 'key'
R> 

and you can further promote warnings to errors if you so choose (and that option is described on the same page).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...