0

I am having trouble using different datasets as input of a shiny app. I would like that the user could select one of 2 datasets ("x" or "y") through checkboxGroupInput. Depending on the dataset selected, the options in selectInput will be different. How can I handle this ? Here is a minimal version of my application (not working):

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)

If I want this code to work I need to preload one of the 2 datasets, and in the choices of selectInput I write "x" or "y" (hard written, no option possible). Any help would be welcome !

EDIT

With the help of this post R shiny passing reactive to selectInput choices, I could arrange to make it run (but maybe not the optimal solution).

A working version:

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)
Natha
  • 364
  • 1
  • 3
  • 20
  • You can't use `input$data` in the UI. You can either initialize the input with a fixed set of choices (or an empty string) and [update the input](https://shiny.rstudio.com/reference/shiny/1.2.0/updateSelectInput.html). Another option would be building the UI for the input on the [server side](https://shiny.rstudio.com/reference/shiny/0.14/renderUI.html). See [this related question](https://stackoverflow.com/questions/21465411/r-shiny-passing-reactive-to-selectinput-choices/21467399#21467399) where the accepted answer explains both options. – MalditoBarbudo Nov 23 '19 at 12:52
  • Thanks for your help, I succeeded in making it running. However maybe there are better options to write it. I'll edit my post – Natha Nov 23 '19 at 14:04
  • To avoid repeating data loading steps, try to wrap your data loading in a [reactive expression](https://shiny.rstudio.com/tutorial/written-tutorial/lesson6/) (`get_data <- reactive({get(input$data})`) and call this reactive expression on both `render*` calls (`mydata <- get_data()`) – MalditoBarbudo Nov 23 '19 at 19:27

0 Answers0