Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 对嵌套列表应用递归函数,同时保留子列表的类_R_List_Recursion_Data Manipulation - Fatal编程技术网

R 对嵌套列表应用递归函数,同时保留子列表的类

R 对嵌套列表应用递归函数,同时保留子列表的类,r,list,recursion,data-manipulation,R,List,Recursion,Data Manipulation,我有一个名为输入的嵌套列表: library(htmltools) library(shiny) inputs = tagList( selectInput('first', 'FIRST', letters), checkboxInput('second', 'SECOND') ) str(inputs, max.level = 1) List of 2 $ :List of 3 ..- attr(*, "class")= chr "shiny.tag" ..- att

我有一个名为
输入的嵌套列表

library(htmltools)
library(shiny)

inputs = tagList(
  selectInput('first', 'FIRST', letters), 
  checkboxInput('second', 'SECOND')
)

str(inputs, max.level = 1)
List of 2
 $ :List of 3
  ..- attr(*, "class")= chr "shiny.tag"
  ..- attr(*, "html_dependencies")=List of 1
 $ :List of 3
  ..- attr(*, "class")= chr "shiny.tag"
 - attr(*, "class")= chr [1:2] "shiny.tag.list" "list"
我想修改所有具有class
shinny.tag
且其
name
元素等于
label
的子列表(请参见
inputs[[1]][[“children”]][[1]]
,以了解此类子列表的示例),但在执行此操作时保留列表的原始结构

为此,我定义了一个递归函数
hideLabel

hideLabel <- function(tag.list) {

  lapply(tag.list, function(x) {

    if(inherits(x, 'shiny.tag')) {

      if(x$name == 'label') {

        tagAppendAttributes(x, style = 'display:none;')

      } else {

        hideLabel(x$children)

      }

    } else {

      return(x)

    }
  })
} 
如上所示,hideLabel不会返回与原始列表输入结构相同的列表(将第一个代码块中的str输出与上面第三个代码块中的str输出进行比较)。我想知道是否有人可以帮助我理解为什么函数会这样做,以及如何修改它?我试过几次重写,但都没有用

更新:

在考虑了每个阶段函数返回的内容后,我让它开始工作。以下是更新后的函数:

hideLabel <- function(x) {

  children = x$children

  x$children = lapply(children, function(y) {

    if(inherits(y, 'shiny.tag')) {

      if(y$name == 'label') tagAppendAttributes(y, style = 'display:none;') else hil(y)

    } else y

  })

  return(x)

}

注意:整个列表的类从
shinny.tag.list
更改为仅
list
。有人知道如何防止这种情况发生吗?我知道我可以使用
do.call(tagList,inputs\u new)
手动添加
shinny.tag.list
类,但这似乎有点黑客味。

您需要共享
tagAppendAttributes
的代码,以使其成为一个可复制的示例它是
htmltools
包中的一个函数,我在代码的标题中包含了一个
调用,因此结果应该是可复制的。
我想修改所有具有class shinny.tag且其name元素等于label的子列表
修改为什么?另外,为什么您认为
tagAppendAttributes
会更改函数中
x
的类?不,对不起,我想修改那些具有class
shinny.tag
且其第一个元素(
name
)的子列表通过将
tagAppendAttributes
函数应用于标签,等于标签,
tagAppendAttributes(x,style='display:none;')
tagAppendAttributes
不会更改标记的类别。是
hideLabel
函数在递归过程中更改不满足
x$name==“label”
条件的标记类。这说明了吗?基本上,我需要将
tagAppendAttributes(x,style='display:none;')
应用到
inputs
列表中的所有标签标签上(标签标签标签都是class
闪亮的子列表。tag
和name='label'),同时保留
inputs
列表的原始结构(该结构显示在我文章第一块的
str
输出中)。您需要共享
tagAppendAttributes
的代码以使其成为可复制的示例。这是
htmltools
包中的一个函数,我在代码的标题中包含了一个
library
调用,因此结果应该是可复制的。
我想修改所有具有类shiny.tag且名称为elem的子列表ent等于label
修改为什么?还有,为什么您认为
tagAppendAttributes
会更改函数中
x
的类?不,对不起,我想修改那些具有class
Shining.tag
以及其第一个元素(
name
)的子列表通过将
tagAppendAttributes
函数应用于标签,使其等于:
tagAppendAttributes(x,style='display:none;')
tagAppendAttributes
不会更改标记的类。是
hideLabel
函数在递归过程中更改不满足
x$name='label'
条件的标记类。这说明了这一点吗?基本上,我需要应用
tagAppendAttributes(x,style='display:none;'))
添加到
输入
列表中的所有标签标签(标签标签标签都是class
shinny.tag
和name='label'),同时保留
输入
列表的原始结构(结构显示在我帖子第一个区块的
str
输出中)。
hideLabel <- function(x) {

  children = x$children

  x$children = lapply(children, function(y) {

    if(inherits(y, 'shiny.tag')) {

      if(y$name == 'label') tagAppendAttributes(y, style = 'display:none;') else hil(y)

    } else y

  })

  return(x)

}
inputs_new = lapply(inputs, hideLabel)

str(inputs, max.level = 1)
List of 2
 $ :List of 3
  ..- attr(*, "class")= chr "shiny.tag"
  ..- attr(*, "html_dependencies")=List of 1
 $ :List of 3
  ..- attr(*, "class")= chr "shiny.tag"
 - attr(*, "class")= chr [1:2] "shiny.tag.list" "list"