1

我想制作一个闪亮的应用程序,用户可以在其中选择基因。然后他会看到这些基因的所有图。

选择部分工作正常(我认为)

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

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

        mainPanel(
                uiOutput('out1')
        )
)

现在我想将那些选定的基因绘制在侧边栏面板旁边:

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是来自 Seurat 分析的数据,Seurat 是一个用于单细胞 RNA-seq 数据的库。因此,用户指定他想要查看的基因并FeaturePlot生成这些图。
FeaturePlot是 Seurat 的一个函数,它“根据‘特征’(即基因表达、PC 分数、检测到的基因数量等)为降维图上的单个细胞着色”

我对 R 非常陌生,尤其是 Shiny,所以请随时提出任何改进建议。

4

1 回答 1

1

找到了一个适合我的解决方案:

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)
于 2020-05-14T18:48:32.293 回答