我正在使用環境中的功能在numericInputShiny 應用程式中渲染小部件。小部件的數量取決于子串列中元素的數量。所述串列包含分布的引數,即:lapplyrenderUInumericInput
正常 -> 平均值,標準差
betagen -> shape1,shape2,最小值,最大值。
因此,對于“普通”元素,我需要渲染兩個小部件,而對于“betagen”,我需要四個。
然而,問題是每個小部件都沒有顯示相應的標簽。此外,如果我嘗試inputId為每個分配一個唯一的,numericInput我會得到一個錯誤。
這是我正在使用的代碼的 MWE。我曾嘗試使用names()來獲取串列元素名稱,label但沒有顯示。任何幫助,將不勝感激。
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)
uj5u.com熱心網友回復:
您應該使用mapply而不是lapply:
mapply(
function(x, y){
numericInput(
inputId = y,
label = y,
value = x,
step = 0.1
)
},
x = lDists[[i]],
y = names(lDists[[i]]),
SIMPLIFY = FALSE
)
或者你可以使用purrr::imap:
purrr::imap(lDists[[i]], ~numericInput(inputId = .y, label = .y, value = .x, step = 0.1))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521656.html
標籤:r闪亮的
上一篇:切片資料框中的行子集
下一篇:在R中子集串列子元素的最快方法
