【发布时间】:2020-11-20 18:24:33
【问题描述】:
我正在尝试制作一个 RShiny 应用程序,您可以从列表中选择一个基因,它会使用该基因的转录本显示不同的图表。然而,每个基因都有不同数量的转录本,因此每次选择不同的基因时都必须显示不同数量的图表。我现在的设置是,当一个人选择一个基因时,会创建一个新表,其中包含转录编号(要绘制的数据)以及所有转录名称的新列表(此列表的长度是我需要的地块)。这些是反应值。
下面,在服务器中,我创建了一个函数来创建我想要的图形,然后我通过索引到名称的反应列表来迭代函数的创建,因此它为每个名称创建一个图形(如每个名字都是不同的成绩单)。现在,代码正确地遍历所有名称,但只显示最后一个图。有没有办法显示每个情节?我尝试了很多不同的东西,从 renderUI 到使用本地调用,但无法弄清楚。
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("var", label = "Choose a gene to display", names),
mainPanel(
plotOutput("tdot"))
))
server <- function(input, output) {
genename <- reactive({
input$var
})
transTable2 <- reactive ({
cbind(biofluids, select(transTable, starts_with(input$var)))
})
names <- reactive ({
tableBF <- cbind(biofluids, select(transTable, starts_with(input$var)))
n <- colnames(tableBF)
final <- n[-1]
})
createUI <- function(name, table) {
ggplot(table, aes_string(x = "biofluids", y = name))+geom_boxplot(aes(color = biofluids))+
geom_boxplot(aes(fill = biofluids)) + scale_y_log10()+ylab( 'log10 normalized counts')+
ggtitle(name)}
output$tdot <- renderPlot({
lapply(1:length(names()), function(i)
createUI(names()[i], transTable2()))
})
}
# Run the application
shinyApp(ui = ui, server = server)
以下是 iris 数据集的可重现示例,该示例将让用户选择一个类别(“Sepal”或“Petal”),然后为数据集中以该词开头的每一列创建一个图:
cats <- c("Sepal", "Petal")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("var", label = "Choose a category to display", cats),
mainPanel(
plotOutput("tdot"))
))
server <- function(input, output) {
category <- reactive({
input$var
})
iris2 <- reactive ({
select(iris, starts_with(input$var))
})
names <- reactive ({
table2 <- select(transTable, starts_with(input$var))
n <- colnames(table2)
})
createUI <- function(name, table) {
ggplot(table, aes_string(x = "species", y = name))+geom_boxplot(aes(color = species))+
geom_boxplot(aes(fill = species)) + scale_y_log10()+ylab( 'log10 normalized counts')+
ggtitle(name)}
output$tdot <- renderPlot({
lapply(1:length(names()), function(i)
createUI(names()[i], iris2()))
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】:
-
嗨,欢迎来到 Stack Overflow。你能否举一个可重现的例子,也许使用像 iris 这样的数据集?
-
我刚做了。让我知道这是否有帮助!
-
您好,如果以下解决方案适合您,请将其标记为答案,谢谢。