如何使用' unique'为Shiny输入创建标签?

时间:2017-03-11 15:34:32

标签: 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( 
       titlePanel("Rad Data"),
       sidebarLayout(
         sidebarPanel(
           selectInput(inputId = 'modality', label = "Modality", choices = list(unique(m), selected = 'CT', selectize = FALSE))   
         ),
         mainPanel(outputPlot(outputId = 'distPlot'))
       ))

感谢您的任何建议。 有效值

1 个答案:

答案 0 :(得分:1)

据我所知,您希望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) {
  }
)

更新:如果您想根据data.frame中的列生成选项,可以使用最初由A5C1D2H2I1M1N2O1R2T1建议的uiOutput和renderUI:

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)   
    })
  }
)