R 如何使用';独特的';要为闪亮的输入创建标签?
我想定义“selectInput”的输入标签,但不指定每个标签,而是使用“unique”给出:R 如何使用';独特的';要为闪亮的输入创建标签?,r,shiny,R,Shiny,我想定义“selectInput”的输入标签,但不指定每个标签,而是使用“unique”给出: Error in (function (choice, name) : All sub-lists in "choices" must be named. 示例代码: m <- sample(c('CT', 'MRI', 'US', 'XRAY'), size = 100, replace = TRUE) ui <- fluidPage( t
Error in (function (choice, name) :
All sub-lists in "choices" must be named.
示例代码:
m <- sample(c('CT', 'MRI', 'US', 'XRAY'), size = 100, replace = TRUE)
ui <- fluidPage(
titlePanel("Rad Data"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = 'modality', label = "Modality", choices = list(unique(m), selected = 'CT', selectize = FALSE))
),
mainPanel(outputPlot(outputId = 'distPlot'))
))
m据我所知,您希望selectInput中的项目具有唯一性。在您的示例中,列表太多(m
是一个列表,您将其封装在choices=list(…
中的另一个列表中)
试试这个:
m <- sample(c('CT', 'MRI', 'US', 'XRAY'), size = 100, replace = TRUE)
shinyApp(
ui = fluidPage(
titlePanel("Rad Data"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = 'modality', label = "Modality", choices = unique(m), selected = 'CT', selectize = FALSE)
),
mainPanel()
)),
server = function(input, output) {
}
)
m是“m”预定义的还是要从数据中动态生成?是的,m是一个数据帧变量。唯一的因素通常是我从中采样的4个因素,但有时会添加或删除一个不同的因素,如果我不需要编辑selectInput选项,这将是一个优雅的选择。然后,我认为反应式UI就是你想要的,由K覆盖下面是里斯托弗的回答。
df <- data.frame(m = sample(c('CT', 'MRI', 'US', 'XRAY'), size = 100, replace = TRUE))
shinyApp(
ui = fluidPage(
titlePanel("Rad Data"),
sidebarLayout(
sidebarPanel(
uiOutput("unique_modalities")
),
mainPanel()
)),
server = function(input, output) {
output$unique_modalities <- renderUI({
selectInput(inputId = 'modality', label = "Modality", choices = unique(df$m), selected = 'CT', selectize = FALSE)
})
}
)