My solution to this problem is as follows:
- Define a function that creates the desired type of widget. Give it one parameter, an integer, and append that integer to the name of the widget using `paste0'.
- Define a reactive object that applies the function to a vector of integers and returns the result as a list using
lapply
.
- Define an output object using
renderUI
that returns the reactive object inside tagList
.
Below is the code for a working version.
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("WidgetVector"),
sidebarLayout(
sidebarPanel(uiOutput("OnButton"),uiOutput("NumberOfWidgets")),
mainPanel(uiOutput("WidgetVectorDisplay")
)
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
output$OnButton=renderUI({radioButtons("TurnOn",label="On/Off",choices=c("On"=1,"Off"=2),selected=2)})
output$NumberOfWidgets=renderUI({numericInput("WidgetCount",label="How many widgets?",value=1)})
makeRadioButton=function(n=1){radioButtons(paste0("Radio",n),label="Pick",choices=c("a"=1,"b"=2,"c"= 3),selected=1)}
WidgetVector=reactive({lapply(X = 1:input$WidgetCount, FUN = makeRadioButton)})
output$WidgetVectorDisplay <- renderUI({
if(is.null(input$TurnOn)){return()
} else if(input$TurnOn == 2){return()
} else tagList(WidgetVector())})
})