簡體   English   中英

R Shiny 中具有反應數據的繪圖的動態數量

[英]Dynamic number of Plots with reactive data in R Shiny

我正在嘗試制作一個 RShiny 應用程序,您可以從列表中選擇一個基因,它會使用該基因的轉錄本顯示不同的圖表。 然而,每個基因都有不同數量的轉錄本,因此每次選擇不同的基因時必須顯示不同數量的圖表。 我現在的設置是,當一個人選擇一個基因時,會創建一個新表,其中包含轉錄編號(要繪制的數據)以及所有轉錄名稱的新列表(此列表的長度是我需要的地塊)。 這些是反應值。

下面,在服務器中,我創建了一個 function 來創建我想要的圖形,然后我通過索引到名稱的反應列表來迭代 function 的創建,因此它為每個名稱創建一個圖形(因為每個名稱都是不同的成績單)。 現在,代碼正確地遍歷了所有名稱,但只顯示最后一個 plot。 有沒有辦法讓每個 plot 顯示? 我嘗試了很多不同的東西,從 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 數據集的可重現示例如下,它將為用戶 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)

以下代碼使用 iris 數據生成動態數量的輸出。 您應該能夠使其適應您的數據。

  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)

它給出了以下 output:

輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM