Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.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
Html R:在列表中有条件地包含命名元素_Html_R_List_If Statement - Fatal编程技术网

Html R:在列表中有条件地包含命名元素

Html R:在列表中有条件地包含命名元素,html,r,list,if-statement,Html,R,List,If Statement,因为Shiny中包含的checkboxGroup不完全符合我的需要,所以我正在重建checkboxGroup函数。我正在寻找一种方法,根据一个布尔变量,在传递给tags$input(…)的参数中包含一个名为checked的元素 我希望下面的代码能按预期工作,但我理解为什么它不能也不应该。有没有类似的简明语法可以用来达到预期的效果 f <- function(selected = TRUE) { tags$input( type = 'checkbox', if(sele

因为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>

f有条件地向函数调用添加参数并不是一件非常容易的事情,但是您可以这样做

f <- function(selected = TRUE) {
  tag <- tags$input(
    type = 'checkbox',
    "Checkbox Content"
  )
  if (selected) tag <- tagAppendAttributes(tag, selected=TRUE)
  tag
}

f(TRUE)
# <input type="checkbox" selected="TRUE">Checkbox Content</input>
f(FALSE)
# <input type="checkbox">Checkbox Content</input>

f我真的不明白,但你不能使用
renderUI
?否则,无论如何,你应该做
标记$input(type='checkbox',checked=if(selected)“checked”,“checkbox Content”)
两难的是,如果checked作为一个命名参数包含进来,它将导致复选框被选中。再见,谢谢。我就是这么想的。如果没有人能提供更“内联”或更简单/简洁的答案,我会接受这个答案。@ctesta01我能想到的另一种方法是将所有参数构建到一个列表中,并使用
do.call
调用函数。Flick先生的解决方案更简洁。