mastering-shiny icon indicating copy to clipboard operation
mastering-shiny copied to clipboard

Clarity in 18.2.1 - Other Applications

Open labouz opened this issue 3 years ago • 2 comments

This example was a little difficult to interpret:

iconRadioButtons <- function(inputId, label, choices, selected = NULL) {
  names <- lapply(choices, icon)
  values <- if (is.null(names(choices))) names(choices) else choices
  radioButtons(inputId,
    label = label,
    choiceNames = names, choiceValues = values, selected = selected
  )
}

Upon first reading, I had no clue what icon was until spending several minutes to figure out that icon() is a Shiny function. I also had help from other members of the r4ds/MS bookclub who reminded me that there is a use case of icon() in conjunction with radioButtons() in Ch.2.2.5.

I think some clarity here would really help the readers understand this particular use case better - maybe linking out the icon() function and including what choices looks like so we get a better sense of what values could return.

Side question: wouldn't if (is.null(names(choices))) names(choices) return NULL if the condition evaluates to TRUE? If so, I don't really understand why we would pass null choice values to the radio buttons. names would be NULL and values would be NULL, so is it so that no radio buttons are rendered (even if an id and label are passed to the func?)? Sorry, that whole conditional is a bit confusing to me.

labouz avatar Jun 29 '21 17:06 labouz

Would you mind providing a link to the section where that code lives?

Looks like I got the conditional the wrong way around (which I do quite often). It should be if (!is.null(names(choices))) names(choices) else choices

hadley avatar Jun 29 '21 18:06 hadley

yes! book: https://mastering-shiny.org/scaling-functions.html#other-applications gh: https://github.com/hadley/mastering-shiny/blob/master/scaling-functions.Rmd - line 106

labouz avatar Jun 29 '21 18:06 labouz