我正在尝试使用
tagQuery
类来 div
sw-input-icon
标签,但我没有得到任何结果。我确实相信这是因为有多个班级。如何选择 div.sw-input-icon
标签?
library(htmltools)
library(shinyWidgets)
# <div class="form-group shiny-input-container">
# <label class="control-label shiny-label-null" for="test" id="test-label"></label>
# <div class="input-group">
# <div class="input-group-addon sw-input-icon input-group-prepend">
# <span class="input-group-text">Select:</span>
# </div>
# <input id="test" type="number" class="form-control numeric-input-icon" value="5"/>
# </div>
# <span class="help-block invalid-feedback hidden d-none"></span>
# </div>
objct <- numericInputIcon("test", NULL, 5, icon = "Select:")
tagQuery(objct)$
find("div.input-group")
# Does not return anything
tagQuery(objct)$
find("div.input-group-addon sw-input-icon input-group-prepend")
# Does not return anything
tagQuery(objct)$
find("div.sw-input-icon")
编辑:
我发现使用
html2r
有点乏味的解决方法。我将 HTML
代码转换为 shiny::tags
:
objct <-
withTags(
div(class = "form-group shiny-input-container",
label(class = "control-label shiny-label-null",
`for` = "test",
id = "test-label"),
div(class = "input-group",
div(class = "input-group-addon sw-input-icon input-group-prepend",
span(class = "input-group-text",
"Select:")),
input(id = "test",
type = "number",
class = "form-control numeric-input-icon",
value = "5")),
span(class = "help-block invalid-feedback hidden d-none"))
)
之后,我可以正确查询
objct
并添加style
tagQuery(objct)$
find("div.sw-input-icon")$
addAttrs(style = "width: 75px")$
allTags()
tagQuery
构造函数创建的,那么
tag
就可以非常整齐地工作,但是 - 正如您所注意到的 - 一旦其中一个子元素是文字 HTML
字符串或 - 如您的case - 标签函数。
你需要做的是确保
shiny.tag.function
被它代表的标签替换,你可以这样做:
objct_rendered <- rapply(objct, as.tags,
classes = "shiny.tag.function", how = "replace")
这告诉
R
递归地用其标签替换所有shiny.tag.functions
。现在孩子们都tags
,你的tagQuery
将会像魅力一样发挥作用:
tagQuery(objct_rendered)$
find("div.sw-input-icon")$
addAttrs(style = "width: 75px")$
allTags()
# <div class="form-group shiny-input-container">
# <label class="control-label shiny-label-null" for="test" id="test-label"></label>
# <div class="input-group">
# <div class="input-group-addon sw-input-icon input-group-prepend" style="width: 75px">
# <span class="input-group-text">Select:</span>
# </div>
# <input id="test" type="number" class="form-control numeric-input-icon" value="5"/>
# </div>
# <span class="help-block invalid-feedback hidden d-none"></span>
# </div>