Search code examples
rggplot2shinybioinformaticsseurat

Render multiple plots in shiny ui


I want to make a shiny app where the user is able to select genes. Then he will see all the plots for those genes.

The selection part works fine (I think)

ui <- fluidPage(
        titlePanel("Test"),

        sidebarPanel(
            selectInput("genes", "Genes:", seurat_genes, multiple = TRUE),
        ),

        mainPanel(
                uiOutput('out1')
        )
)

Now I want to those selected genes to be plotted next to the sidebarPanel:

server <- function(input, output) {

    output$out1 = renderUI({
        p = FeaturePlot(sc, features=input$genes, cols=c("lightgrey", param$col), combine=FALSE)
        names(p) = input$genes
        for(i in names(p)) {
            p[[i]] = plot.mystyle(p[[i]], title=i)
            renderPlot(
                print(p[[i]])
            )
        }
    })
}

seurat_genes is data from the analysis with Seurat, which is a library for single-cell RNA-seq data. So the user specifies which genes he wants to look at and FeaturePlotgenerates those plots.
FeaturePlot is a function from Seurat which "Colors single cells on a dimensional reduction plot according to a 'feature' (i.e. gene expression, PC scores, number of genes detected, etc.)"

I'm fairly new to R and especially Shiny, so feel free to suggest any kind of improvements.


Solution

  • Found a solution that works for me:

    library(shiny)
    library(Seurat)
    
    # This Data is from my Workspace. I have trouble loading it, so its a workaround and is my next Problem.
    seurat_genes = sc.markers[["gene"]]
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
            titlePanel("Einzeldarstellungen von Genen"),
    
            sidebarPanel(
                selectInput("genes", "Gene:", seurat_genes, multiple = TRUE),
            ),
    
            mainPanel(
                splitLayout(cellWidths = c("50%","50%"),uiOutput('out_umap'), uiOutput('out_ridge'))
            )
    )
    
    
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
        output$out_umap = renderUI({
            out = list()
    
            if (length(input$genes)==0){return(NULL)}
            for (i in 1:length(input$genes)){
                out[[i]] <-  plotOutput(outputId = paste0("plot_umap",i))
            }  
            return(out) 
        })
        observe({  
            for (i in 1:length(input$genes)){  
                local({  #because expressions are evaluated at app init
                    ii <- i 
                    output[[paste0('plot_umap',ii)]] <- renderPlot({ 
                            return(FeaturePlot(sc, features=input$genes[[ii]], cols=c("lightgrey", param$col), combine=FALSE))
                    })
                })
            }                                  
        })
    
        output$out_ridge = renderUI({
            out = list()
    
            if (length(input$genes)==0){return(NULL)}
            for (i in 1:length(input$genes)){
                out[[i]] <-  plotOutput(outputId = paste0("plot",i))
            }  
            return(out) 
        })
        observe({  
            for (i in 1:length(input$genes)){  
                local({  #because expressions are evaluated at app init
                    ii <- i 
                    output[[paste0('plot',ii)]] <- renderPlot({ 
                        return(RidgePlot(sc, features=input$genes[[ii]], combine=FALSE))
                    })
                })
            }                                  
        })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)