R闪亮的更新选择一个下拉菜单的输入选项与另一个下拉菜单的选项(即一个是另一个的子类别) [英] R shiny updateSelectInput choices for one dropdown menu with choices from another (ie one is a subcategory of the other)

查看:14
本文介绍了R闪亮的更新选择一个下拉菜单的输入选项与另一个下拉菜单的选项(即一个是另一个的子类别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MegaP2器官类型的数据表,分为皮肤,然后各种细胞类型都来自肺或皮肤。我已尝试使"单元格行"下拉框中的可用选项仅反映来自第一个下拉框中选定的器官的选项。

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

我假设我需要某种方法,以便在做出新选择时刷新器官类型,但如果有任何帮助,我们将不胜感激。

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

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 session update"中保留了"Select All Session Updates",以防出现问题,因为理想情况下,我希望它也能与这些更新一起使用。

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

推荐答案

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

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)

这篇关于R闪亮的更新选择一个下拉菜单的输入选项与另一个下拉菜单的选项(即一个是另一个的子类别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆