create radiobutton list dynamically

2,916 views
Skip to first unread message

Thomas Bristow

unread,
Nov 29, 2012, 5:16:02 PM11/29/12
to shiny-...@googlegroups.com
I am trying to create a list of radiobuttons consisting of the headers in an array selected by a drop down menu. 

As you can see in this sample code - I think I need a reactive function to generate the list based on the selection. I know how  to retrieve the names of the headers, but need help with turning this into a list that can be passed to the radioButtons() on the UI side. For instance, is using reactiveText() correct?
______________________________________________
library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
  
  # Sidebar with controls to select a mineral group, with radiobutton list of minerals in that group generated in response
  sidebarPanel(
    selectInput("mineralgroup", "Choose a mineralgroup:", 
                choices = c("Carbonates", "Feldspars")),
    
    radioButtons("min", "Mineral:",
                      ("radiolist"))
    
  )
))
__________________________________________________________
library(shiny)

carbonates <- read.csv("CARBONATES.csv")
feldspars <- read.csv("FELDSPARS.csv")

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
  
  datasetInput <- reactive(function() {
    switch(input$mineralgroup,
           "Carbonates" = Carbonates,
           "Feldspars" = Feldspars)
  })
  # A reactive function creating list of radiobuttons based on "mineralgroup" choice
  output$radiolist < reactiveText(function() {
    list(names(datasetInput())
  })  
})


Márcio Nicolau

unread,
Nov 29, 2012, 5:53:35 PM11/29/12
to shiny-...@googlegroups.com
Hi Tomas,

Change: radioButtons("min", "Mineral:",
                      ("radiolist"))
By: radioButtons("min", "Mineral:",
                      radiolist)


Márcio Nicolau
@marcionicolau
--
 
 

Joe Cheng

unread,
Nov 29, 2012, 6:03:26 PM11/29/12
to shiny-...@googlegroups.com
I see what you're trying to do, but it won't work exactly in that way. You have two options here that I can think of.

1) Have the page contain two sets of radioButtons: one "minCarbonates" and one "minFieldspars", each one contained in its own conditionalPanel. The conditions of the conditionalPanel would be "input.mineralgroup == 'Carbonates'" and "input.mineralgroup == 'Feldspars'", respectively. If the set of radios is relatively small and well known in advance then this is likely the most robust solution. One downside is that each set of radioButtons needs its own unique inputId and in the server.R you'll need to figure out which one to use, depending on the value for mineralGroup. (So, switch(input$mineralgroup, "Carbonates"=input$minCarbonates ...) )

2) Replace radioButtons("min", "Mineral:", ("radiolist")) with uiOutput("radio"). Then in server.R, remove output$radiolist and replace with:

output$radio <- reactiveUI(function() {
  radioButtons("min", "Mineral:", names(datasetInput()))
})

This is maybe more compact and easier to understand but the reactiveUI feature is more experimental than conditionalPanel and has some edge cases (that shouldn't apply in this case though).




--
 
 

Thomas Bristow

unread,
Nov 29, 2012, 6:21:49 PM11/29/12
to shiny-...@googlegroups.com
Thanks Joe,

I'll give those a shot later.

Tom

Thomas Bristow

unread,
Nov 30, 2012, 12:46:29 PM11/30/12
to shiny-...@googlegroups.com
The second reactiveUI function option works well.

But I am having difficulty extracting the data using the selectInput and radiobutton choice of the user. 

I have a function that returns an array based on selectInput choice:

mineralgroupInput <- reactive(function() {
    switch(input$mineralgroup,
           "Carbonates" = carbonates,
           "Feldspars" = feldspars)

The list of radiobuttons is based on this choice and the headers in the chosen array:

output$radio <- reactiveUI(function() {
    radioButtons("min", "Mineral:", names(mineralgroupInput()))

I figure I need a reactive function like the one below to access the data from one column of a chosen array.

mineralInput <- reactive(function() {
    group <-mineralgroupInput()
switch(input$min,
           "input$min[1]" = groupt$input$min[1],
           "........" =  ..... etc)

But I haven't got a clue how to generate the list used by the switch statement based on the radiobutton list  and choice? I want to avoid hardcoding everything so I can change the mineral group arrays.

Any suggestions?
Thanks.


Reply all
Reply to author
Forward
0 new messages