[英]Dynamic number of Plots with reactive data in R Shiny
I am trying to make an RShiny app that you can pick a gene from a list, and it will display different graphs using that gene's transcripts.我正在尝试制作一个 RShiny 应用程序,您可以从列表中选择一个基因,它会使用该基因的转录本显示不同的图表。 However, each gene has a different number of transcripts, so a different number of graphs must be displayed every time a different gene is chosen.然而,每个基因都有不同数量的转录本,因此每次选择不同的基因时必须显示不同数量的图表。 How I have it set right now is that when a person chooses a gene, a new table is created with the transcript numbers (data to be plotted) along with a new list of all the transcript names (length of this list is the amount of plots that I need).我现在的设置是,当一个人选择一个基因时,会创建一个新表,其中包含转录编号(要绘制的数据)以及所有转录名称的新列表(此列表的长度是我需要的地块)。 These are reactive values.这些是反应值。
Below, in the server, I made a function that creates the graph that I want, and then I iterate through the creation of the function by indexing into the reactive list of names, so it creates a graph for each name (as each name is a different transcript).下面,在服务器中,我创建了一个 function 来创建我想要的图形,然后我通过索引到名称的反应列表来迭代 function 的创建,因此它为每个名称创建一个图形(因为每个名称都是不同的成绩单)。 Right now, the code iterates through all the names correctly but only displays the last plot.现在,代码正确地遍历了所有名称,但只显示最后一个 plot。 Is there a way to have every plot displayed?有没有办法让每个 plot 显示? I have tried a lot of different things, from renderUI to using local calls, but cannot figure it out.我尝试了很多不同的东西,从 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)
A reproducible example is as follows with the iris dataset, which would have the user select a category (either "Sepal" or "Petal"), and then create a plot for every column in the dataset that starts with that word: iris 数据集的可重现示例如下,它将为用户 select 提供一个类别(“Sepal”或“Petal”),然后为数据集中以该单词开头的每一列创建一个 plot:
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)
The following code generates dynamic number of outputs with iris data.以下代码使用 iris 数据生成动态数量的输出。 You should be able to adapt this to your data.您应该能够使其适应您的数据。
library(shiny)
library(tidyverse)
# Load data
data("iris")
# Add row id
iris2 <- iris %>% mutate(ID = 1:n())
# ui
ui <- fluidPage(
sidebarPanel(
selectInput(inputId = "sel", label = "Select one or more parameters",
choices = names(iris2), multiple = TRUE)
),
mainPanel(
uiOutput("plots")
)
)
# server
server <- function(input, output, session){
# Dynamically generate the plots based on the selected parameters
observe({
req(input$sel)
lapply(input$sel, function(par){
p <- ggplot(iris2, aes_string(x = "ID", y = par)) +
geom_boxplot(aes(fill = Species, group=Species, color=Species)) +
ggtitle(paste("Plot: ", par))
output[[paste("plot", par, sep = "_")]] <- renderPlot({
p
},
width = 380,
height = 350)
})
})
# Create plot tag list
output$plots <- renderUI({
req(input$sel)
plot_output_list <- lapply(input$sel, function(par) {
plotname <- paste("plot", par, sep = "_")
plotOutput(plotname, height = '250px', inline=TRUE)
})
do.call(tagList, plot_output_list)
})
}
shinyApp(ui, server)
It gives the following output:它给出了以下 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.