I am rendering numericInput
widgets in a Shiny app using the lapply
function in a renderUI
environment. The amount of numericInput
widgets depends on the amount of elements in a sublist. Said list contains the parametres of a distribution, i.e.:
normal -> mean, sd
betagen -> shape1, shape2, min, max.
So, for the 'normal' element I would need two widgets rendered and for 'betagen' four.
The problem however is that the corresponding labels don't show up for each widget.
Furthermore, if I try and assign a unique inputId
to each numericInput
I get an error.
Here is a MWE of the code I'm using. I have tried using names()
to get the list element names as the label
but nothing shows up. Any help would be appreciated.
MWE
library(shiny)
# Lists containing the distribution parametres
lNormal <-list(mean=10, sd=20)# Easily identifiable values for debugging
lBetagen <-list(shape1=10 , shape2=20, min=30, max=40)# Easily identifiable values for debugging
# List of lists
lDists <- list(normal=lNormal, betagen=lBetagen)
# UI
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("ui1")
),
mainPanel()
)
)
# Server
server <- function(input, output) {
output$ui1 <- renderUI({
# index pointing to a distribution in lDists
i <- 2
# Render numeric inputs for each element of IDs
lapply(lDists[[i]], function(x){numericInput(inputId = "id",# names(x) -> results in an error
label = names(x),# the names don't show up as labels
value = x,
step = 0.1
)}
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
CodePudding user response:
You should use mapply
instead of lapply
:
mapply(
function(x, y){
numericInput(
inputId = y,
label = y,
value = x,
step = 0.1
)
},
x = lDists[[i]],
y = names(lDists[[i]]),
SIMPLIFY = FALSE
)
or you could use purrr::imap
:
purrr::imap(lDists[[i]], ~numericInput(inputId = .y, label = .y, value = .x, step = 0.1))