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
471 views
in Technique[技术] by (71.8m points)

r - Get name of x when defining `(<-` operator

I want to define (<- and access the name of the left hand side argument :

*<- functions use internally an intermediate '*tmp*' variable. Is it still possible to get the name of x ?

`(<-` <- function(x,value){
  print(deparse(substitute(value)))
  print(deparse(substitute(x)))
  print(match.call())
  value
}

foo <- 0
(foo) <- 3
# [1] "3"
# [1] "*tmp*"
# `(<-`(x = `*tmp*`, value = 3)# [1] "3"

I want to get "foo" from inside the function.

I tried to hack it by using tracemem, i.e. calling sapply(ls(envir = parent.frame()),tracemem) and tracemem(x) inside of the functions but the address of foo, *temp* and x are all different.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I hacked it, though I didn't understand everything that I did.

I noticed pryr::address was giving a different kind of results than tracemem and tried it (I had to dig into the code to use pryr:::address2 because pryr::address doesn't have an environment argument).

Then I noticed that mixing the results from tracemem on x and pryr:::address2 on the rest of the objects there was a match (after basic reformatting) :

`(<-` <- function(x,value){
  pf <- parent.frame()
  all_addresses       <- sapply(ls(pf), pryr:::address2, pf)
  all_addresses       <- all_addresses[names(all_addresses) != "*tmp*"]
  all_addresses_short <- gsub("(^|<)[0x]*(.*?)(>|$)","\2",all_addresses)

  x_address       <- tracemem(x)
  x_address_short <- tolower(gsub("(^|<)[0x]*(.*?)(>|$)","\2",x_address))

  ind    <- match(x_address_short, all_addresses_short)
  x_name <- names(all_addresses)[ind]

  message("all_addresses, using pryr::address2")
  print(all_addresses)
  print(all_addresses_short)

  message("x_address, using tracemem")
  print(x_address)
  print(x_address_short)

  message("x_name, matching substrings")
  print(x_name)

  value
}

The regex used in gsub calls tries to account for the address formats we get with different systems, I'm not 100% sure that it's general.

output:

foo <- 1
bar <- 2
(foo) <- foo

# all_addresses, using pryr::address2
# (<-          bar          foo 
# "0x1433df50" "0x14937678" "0x14937708" 
# (<-        bar        foo 
# "1433df50" "14937678" "14937708" 
# x_address, using tracemem
# [1] "<0000000014937708>"
# [1] "14937708"
# x_name, matching substrings
# [1] "foo"

It breaks if x is not a variable name, for example:

foo <- iris
(foo$species) <- 3

We could assume that if the address isn't found x is a list item, and then lookup its address among the addresses of the items of all the lists we have in the parent.frame (recursively), but I think that's enough ugly hacks for today.


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

...