【发布时间】:2018-09-11 19:15:55
【问题描述】:
因为 Shiny 中包含的 checkboxGroup 并不完全符合我的需要,所以我正在重建 checkboxGroup 函数。我正在寻找一种方法,根据布尔变量在传递给 tags$input(...) 的参数中包含一个名为 checked 的元素。
我希望以下代码能按预期工作,但我明白为什么它不能也不应该。有没有类似的简洁语法可以用来达到预期的效果?
f <- function(selected = TRUE) {
tags$input(
type = 'checkbox',
if(selected) checked = "checked",
"Checkbox Content"
)
}
f()
# actual result:
# <input type="checkbox">
# checked
# Checkbox Content
# </input>
# desired result:
# <input type="checkbox" checked = "checked">
# Checkbox Content
# </input>
【问题讨论】:
-
我不太明白,但你不能用
renderUI吗? -
否则,无论如何,你应该这样做
tags$input(type = 'checkbox', checked = if(selected) "checked", "Checkbox Content") -
困境是如果checked被包含为一个命名参数,它会导致复选框被选中。见stackoverflow.com/questions/12700626/…
标签: html r list if-statement