使用...修改功能中的嵌套列表 [英] Use ... to modify a nested list within a functional

查看:77
本文介绍了使用...修改功能中的嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我试图创建一种将...中给出的函数参数转换为闭包函数中预定列表中的值的方法.

In R, I'm trying to create a way to transform function parameters given in ... to values in a pre-determined list within a closure function.

我希望能够做这样的事情:

I would like to be able to do something like this:

function_generator <- function(args_list = list(a = "a", 
                                                b = "b", 
                                                c = list(d = "d", 
                                                         e = "e")){

    g <- function(...){
         ## ... will have same names as args list
         ## e.g. a = "changed_a", d = "changed_d"
         ## if absent, then args_list stays the same e.g. b="b", e="e"
         arguments <- list(...)
         modified_args_list <- amazing_function(arguments, args_list)
         return(modified_args_list)
         } 

    }

args_list每次都会有所不同-它是要在httr请求中发送的主体对象.

args_listwill be different each time - its a body object to be sent in a httr request.

如果列表没有嵌套列表,我有一个功能可以工作:

I've got a function that works if the list does not have nested lists:

substitute.list <- function(template, replace_me){

  template[names(replace_me)] <- 
    replace_me[intersect(names(template),names(replace_me))]

  return(template)

}

t <- list(a = "a", b="b", c="c")
s <- list(a = "changed_a", c = "changed_c")

substitute.list(t, s)
> $a
>[1] "changed_a"

>$b
>[1] "b"

>$c
>[1] "changed_c"

但是我不知道如何修改它,使其与嵌套列表一起使用:

But I can't work out how to modify it so that it works with nested lists:

## desired output
t <- list(a = "a", b = "b", c = list(d = "d", e = "e"))
s <- list(a = "changed_a", d = "changed_d")

str(t)
List of 3
 $ a: chr "a1"
 $ b: chr "b1"
 $ c:List of 2
  ..$ d: chr "d1"
  ..$ e: chr "e1"

amaze <- amazing_function(t, s)

str(amaze)
List of 3
 $ a: chr "changed_a"
 $ b: chr "b1"
 $ c:List of 2
  ..$ d: chr "changed_d"
  ..$ e: chr "e1"

amazing_function可能是什么?我猜想使用substitute.list进行某种递归可以行得通,但是却找不到任何东西,因此,我转向互联网寻求帮助或参考,以使其正常运行. 非常有义务.

What could amazing_functionbe? I guess some kind of recursion using substitute.list could work, but haven't been able to find anything, hence I turn to you, internet, for help or references to make it work. Much obliged.

推荐答案

嵌套列表的后深度深度第一步

Post-order depth first walk of nested list

postwalk<-function(x,f) {
  if(is.list(x)) f(lapply(x,postwalk,f))  else f(x)
}

替换函数,返回修改后的列表,而不是在原处进行变异

Replacement function that returns modified list rather than mutating in place

replace.kv<-function(x,m) {
   if(!is.list(x)) return(x)
   i<-match(names(x),names(m));
   w<-which(!is.na(i));
   replace(x,w,m[i[w]])
}

示例

t<-list(a="a1", b="b1", c=list(d="d1", e="e1"))
s<-list(a="a2", d="d2")

str(postwalk(t,function(x) replace.kv(x,s)))


List of 3
 $ a: chr "a2"
 $ b: chr "b1"
 $ c:List of 2
  ..$ d: chr "d2"
  ..$ e: chr "e1"

这篇关于使用...修改功能中的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆