简体   繁体   中英

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 FeaturePlot generates those plots.
FeaturePlot is a function from Seurat which "Colors single cells on a dimensional reduction plot according to a 'feature' (ie 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM