I have a tagList
of two Shiny inputs, inputs
. I would like to extract the label
tag for each input. I was hoping that htmltools
had a getter function to achieve this but in the absence of one I've defined a function, getLabel
, that recurses through the input list and extracts the sublists whose name element equals the value label
. Here is my code:
library(htmltools)
library(shiny)
inputs = tagList(
selectInput('first', 'FIRST', letters),
checkboxInput(inputId = 'second', label = 'SECOND')
)
getLabel2 <- function(children) {
lapply(children, function(x) {
if(inherits(x, 'shiny.tag')) {
if(x$name == 'label') {
return(x)
} else {
chldn = x$children
if(is.list(chldn)) getLabel2(chldn)
}
}
})
}
getLabel <- function(inputs) {
lapply(inputs, function(x) {
if(grepl('shiny-input-container', tagGetAttribute(x, 'class'))) {
getLabel2(x$children)
} else {
return(x)
}
})
}
labels = getLabel(inputs)
The problem is that the resulting list includes zero-length sublists. My desired output is a list of two elements (the label for each input) of class 'shiny-tag'. How can I modify my function to achieve this? Also, can this be done in htmltools
? I can't find any relevant getters in the package manual.