首页 > 解决方案 > 一个下拉菜单的 R 闪亮 updateSelectInput 选项以及另一个下拉菜单的选择(即一个是另一个的子类别)

问题描述

我有一个MegaP2器官类型的数据表,分为LungSkin,然后是各种细胞类型,所有这些都来自肺或皮肤。我试图让Cell Lines下拉框中的可用选项仅反映来自第一个下拉框中所选器官的选项。

如果我选择皮肤,它会完美地提供相关的细胞系,但是如果我尝试选择其他器官类型,则会进一步将细胞系限制为仅在两个器官中的细胞系,而不是为新器官选择提供所有细胞系. 它还阻止我单击单元格行下拉菜单在此处进行更改。

我想我需要一些方法来让器官类型在做出新的选择时刷新,但任何帮助都将不胜感激。

我已经创建了选择列表,如下所示:

Cell_type = c("All", as.character(levels(MegaP2$Cell_line)))
Organ_type = as.character(levels(MegaP2$Organ))

Lung_cells = filter(MegaP2, Organ == "Lung")
#Then to remove the levels that have been filtered out
Lung_cells = droplevels(Lung_cells)
Lung_lines = c("All", as.character(levels(Lung_cells$Cell_line)))
Skin_cells = filter(MegaP2, Organ == "Skin")
Skin_cells = droplevels(Skin_cells)
Skin_lines = c("All", as.character(levels(Skin_cells$Cell_line)))

我的(相关)ui 代码如下所示:

ui = fluidPage(
  titlePanel(title=div(img(src="cell_image.png", height = 140, width = 400), "The Senescent Cell")),
  sidebarLayout(
    sidebarPanel(
      selectInput("OrganT",
                  label = "Organ",
                  choices = Organ_type,
                  multiple = T,
                  selected = "All"),
      selectInput("Cell",
                  label = "Cell Line",
                  choices = Cell_type,
                  multiple = T,
                  selected = "All")
      
    ),
  mainPanel(
    tableOutput("MegaData")
  )
  )
)

我的服务器代码如下:如果导致问题,我将保留在 Select All 会话更新中,因为理想情况下我希望它也可以与这些一起工作。

server = function(input, output, session) {
  selectedData <- reactive({
    req(input$OrganT)
    req(input$Cell)
    MegaP2 %>% 
      dplyr::filter(Cell_line %in% input$Cell & Organ %in% input$OrganT)
  })
  output$MegaData = renderTable({
    data = selectedData()
  })
  observe({    
    if("Lung" %in% input$OrganT & !"Skin" %in% input$OrganT)
      choices2 = Cell_type[which(Cell_type %in% Lung_lines)]
    else if("Skin" %in% input$OrganT & !"Lung" %in% input$OrganT)
      choices2 = Cell_type[which(Cell_type %in% Skin_lines)]
    else
      choices2 = Cell_type
    updateSelectInput(session, "Cell", choices = choices2, selected = choices2)
                                    
    if("All" %in% input$Cell)
      selected_choices6 = choices2[-1]
    else
      selected_choices6 = input$Cell
    updateSelectInput(session, "Cell", selected = selected_choices6)
  })
}

标签: rshinyshiny-server

解决方案


我认为您应该直接使用数据表来选择选项。也许你可以试试这个

ui = fluidPage(
  titlePanel(title=div(img(src="cell_image.png", height = 140, width = 400), "The Senescent Cell")),
  sidebarLayout(
    sidebarPanel( 
      uiOutput("organt"),
      uiOutput("cellt")
    ),
    mainPanel(
      tableOutput("MegaData")
    )
  )
)


server = function(input, output, session) {
  
  df1 <- veteran
  MegaP <- df1 %>% mutate(Organ=ifelse(trt==1,"Lung","Skin"))
  
  output$organt <- renderUI({
    selectInput("OrganT",
                label = "Organ",
                choices = unique(MegaP$Organ),
                multiple = T,
                selected = "All")
  })
  
  MegaP1 <- reactive({
    data <- subset(MegaP, Organ %in% req(input$OrganT))
  })
  
  output$cellt <- renderUI({
    selectInput("Cell",
                label = "Cell Line",
                choices = unique(MegaP1()$celltype),
                multiple = T,
                selected = "All")
  })
  
  selectedData <- reactive({
    req(MegaP1(),input$Cell)
    data <- subset(MegaP1(), celltype %in% input$Cell)
  })

  output$MegaData = renderTable({
    selectedData()
  })

}

shinyApp(ui = ui, server = server)

推荐阅读