Using R to add Help Icon beside radioButtons

huangapple go评论62阅读模式
英文:

Using R to add Help Icon beside radioButtons

问题

I'd like to place a helpful icon on the right side of each radio button. When the user hovers over or clicks the helpful icon, I would want text to appear.

The below image is a dropdown menu, but I'm using radio buttons.

NOTE: The radio button options are from a csv file.

example of what I would like

Below is my code:

ui <- fluidPage(
  fluidRow(
    column(5, 
           wellPanel(
             radioButtons('var',
                          h4('Select a Model Object'), 
                          choices = c(""),
                          width = '37%'
             ),
             shinyDirButton('directory', 'Select Directory', 'Please Select a Directory') 
           )
    )
  )
)

server <- function(input, output, session) {
  # Get folder names within the system_models directory
  model_names <- read.csv("/Users/fs8/Desktop/Project/MAAT/src/system_models/model_names.csv")
  
  # Update choices for selectInput0
  updateRadioButtons(session, "var", choices = setNames(model_names$original_name, model_names$display_name))
}
英文:

I'd like to place a helpful icon on the right side of each radio button. When the user hovers over or clicks the helpful icon, I would want text to appear.

The below image is a dropdown menu, but I'm using radio buttons.

NOTE: The radio button options are from a csv file.

example of what I would like

Below is my code:

ui &lt;- fluidPage(
  fluidRow(
    column(5, 
           wellPanel(
             radioButtons(&#39;var&#39;,
                          h4(&#39;Select a Model Object&#39;), 
                          choices = c(&quot;&quot;), 
                          width = &#39;37%&#39;
             ),
             shinyDirButton(&#39;directory&#39;, &#39;Select Directory&#39;, &#39;Please Select a Directory&#39;) 
           )
    )
  )
)

server &lt;- function(input, output, session) {
  # Get folder names within the system_models directory
  model_names &lt;- read.csv(&quot;/Users/fs8/Desktop/Project/MAAT/src/system_models/model_names.csv&quot;)
  
  # Update choices for selectInput0
  updateRadioButtons(session, &quot;var&quot;, choices = setNames(model_names$original_name, model_names$display_name))
}

答案1

得分: 0

以下是您要翻译的内容:

"I haven't found a way of putting the help icon alongside each button in a set of radio buttons because radioButtons doesn't give direct access to each individual button. There will be a way, but it might be complex. I wonder if it's worth the effort...

It's easy to put a help icon against each entry in a list of checkboxes, though. You can easily monitor the checkboxes to make sure only one is checked at a given time.

I've provided help content for only one of the widgets, but you should be able to work out what you need to do for the rest.

By the way, your sample code was not reproducible. You'd forgotten to include your library calls, and, more seriously, we don't have access to your desktop, so we can't populate the radio button list. So I've done something generic.

library(shiny)
library(shinyhelper)
library(magrittr)

ui <- fluidPage(
  fluidRow(
    column(
      5, 
      wellPanel(
        radioButtons(
          'var',
          h4('Make a choice'), 
          choices = LETTERS[1:4]
        ) %>% 
        helper()
      )
    ),
    column(
      5, 
      wellPanel(
          h4('Make another choice'), 
          checkboxInput("checkboxA", "A") %>% 
            helper(
              type = "inline", 
              title="Help for item A", 
              content = "Blah, blah, blah!"
            ),
          checkboxInput("checkboxB", "B") %>% helper(),
          checkboxInput("checkboxC", "C") %>% helper(),
          checkboxInput("checkboxD", "D") %>% helper()
      )
    )
    
  )
)

server <- function(input, output, session) {
  observe_helpers()
}

shinyApp(ui, server)

I've included the magrittr package because I think piping makes the code more readable. If you don't want to use it, then a sample equivalent is

helper(checkboxInput("checkboxB", "B"))

"

英文:

I haven't found a way of putting the help icon along side each button in a set of radio buttons because radioButtons doesn't give direct access to each individual button. There will be a way, but it might be complex. I wonder if it's worth the effort...

It's easy to put a help icon against each entry in a list of checkboxes though. You can easily monitor the checkboxes to make sure only one is checked at a given time.

I've provided help content for only one of the widgets, but you should be able to work out what you need to do for the rest.

By the way, your sample code was not reproducible. You'd forgotten to include your library calls and, more seriously, we don't have access to your desktop, so we can't populate the radio button list. So I've done something generic.

library(shiny)
library(shinyhelper)
library(magrittr)

ui &lt;- fluidPage(
  fluidRow(
    column(
      5, 
      wellPanel(
        radioButtons(
          &#39;var&#39;,
          h4(&#39;Make a choice&#39;), 
          choices = LETTERS[1:4]
        ) %&gt;% 
        helper()
      )
    ),
    column(
      5, 
      wellPanel(
          h4(&#39;Make another choice&#39;), 
          checkboxInput(&quot;checkboxA&quot;, &quot;A&quot;) %&gt;% 
            helper(
              type = &quot;inline&quot;, 
              title=&quot;Help for item A&quot;, 
              content = &quot;Blah, blah, blah!&quot;
            ),
          checkboxInput(&quot;checkboxB&quot;, &quot;B&quot;) %&gt;% helper(),
          checkboxInput(&quot;checkboxC&quot;, &quot;C&quot;) %&gt;% helper(),
          checkboxInput(&quot;checkboxD&quot;, &quot;D&quot;) %&gt;% helper()
      )
    )
    
  )
)

server &lt;- function(input, output, session) {
  observe_helpers()
}

shinyApp(ui, server)

I've included the magrittr package because I think piping makes the code more readable. If you don't want to use it, then a sample equivalent is

helper(checkboxInput(&quot;checkboxB&quot;, &quot;B&quot;))

huangapple
  • 本文由 发表于 2023年5月23日 00:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定