R: Conditionally including a named element in a list
10:23 02 Apr 2018

Because the checkboxGroup included in Shiny doesn't exactly fit my needs, I am rebuilding the checkboxGroup function. I am looking for a way to include an element named checked in the arguments passed to tags$input(...) depending on a boolean variable.

I wish that the following code worked as desired, but I understand why it doesn't and shouldn't. Is there any similarly concise syntax I can use to achieve the desired result?

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

f()
# actual result:
# 
#   checked
#   Checkbox Content
# 

# desired result:
# 
#   Checkbox Content
# 
r list