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)