首页 > 解决方案 > 闪亮的应用程序:如何根据 checkboxGroupInput 输出修改 selectInput 选项

问题描述

我在使用不同的数据集作为闪亮应用程序的输入时遇到问题。我希望用户可以通过checkboxGroupInput. 根据所选的数据集,其中的选项selectInput会有所不同。我该如何处理?这是我的应用程序的最小版本(不工作):

library(shiny)
library(Seurat)

#-----------------------------------------------------------
# Select dataset
#-----------------------------------------------------------
x <- readRDS("data/x.rds")
y <- readRDS("data/y.rds")
#-----------------------------------------------------------

#-----------------------------------------------------------
# App code
#-----------------------------------------------------------

ui <- fluidPage(

   titlePanel("Shiny app"),

   sidebarLayout(
      sidebarPanel(

        checkboxGroupInput("data",
                  "Select data to use",
                  choices = c("x", "y"),
                  selected = 'x',
                  inline = TRUE,
                  width = NULL),

         selectInput(inputId = "search",
                        label = "Plot 1 gene",
                        choices = sort(rownames(input$data)), # input$data is not working
                        multiple = FALSE,
                        selectize = TRUE)
      ),

      mainPanel(
         plotOutput(outputId = "searchgene"),
      )
   )
)

server <- function(input, output) {
   output$searchgene <- renderPlot({
     FeaturePlot(input$data,
                 features = input$search)
   })  
}

#-----------------------------------------------------------
# Run the application 
#-----------------------------------------------------------
shinyApp(ui = ui, server = server)

如果我想让这段代码工作,我需要预加载两个数据集之一,并在其中choicesselectInput“x”或“y”(硬写,没有选项可能)。欢迎任何帮助!

编辑

借助这篇文章R shiny 将反应传递给 selectInput 选择,我可以安排让它运行(但可能不是最佳解决方案)。

一个工作版本:

library(shiny)
library(Seurat)

#-----------------------------------------------------------
# Select dataset
#-----------------------------------------------------------
x <- readRDS("data/x.rds")
y <- readRDS("data/y.rds")
#-----------------------------------------------------------

#-----------------------------------------------------------
# App code
#-----------------------------------------------------------

ui <- fluidPage(

   titlePanel("Shiny app"),

   sidebarLayout(
      sidebarPanel(

        checkboxGroupInput("data",
                  "Select data to use",
                  choices = c("x", "y"),
                  selected = 'x',
                  inline = TRUE,
                  width = NULL),

        uiOutput('columns')

      ),

      mainPanel(
         plotOutput(outputId = "searchgene")
      )
   )
)

server <- function(input, output) {

   output$searchgene <- renderPlot({
     mydata = get(input$data) # repeated
     FeaturePlot(mydata,
                 features = input$search)})

   output$columns <- renderUI({
     mydata = get(input$data) # repeated
     selectInput(inputId = "search",
                 label = "Plot 1 gene",
                 choices = sort(rownames(mydata)),
                 multiple = FALSE,
                 selectize = TRUE)
   })  
}


#-----------------------------------------------------------
# Run the application 
#-----------------------------------------------------------
shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


推荐阅读