【问题标题】:Dynamic number of Plots with reactive data in R ShinyR Shiny中具有反应数据的动态图数
【发布时间】: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 这样的数据集?
  • 我刚做了。让我知道这是否有帮助!
  • 您好,如果以下解决方案适合您,请将其标记为答案,谢谢。

标签: r dynamic shiny reactive


【解决方案1】:

以下代码使用虹膜数据生成动态数量的输出。您应该能够根据您的数据进行调整。

  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)

它给出以下输出:

【讨论】:

  • 它运行没有任何错误,但它不会在应用程序中生成图。如果有意义的话,它不会在应用程序中产生情节可以去的地方。
  • 或许您可以发布一些示例数据,以便我进行验证。您可以使用dput,甚至可以创建一个虚拟数据框。
  • 我刚刚添加了一个带有 iris 数据集的样本。让我知道这是否适合你!
  • 非常感谢!!!我整个星期都在为此工作:)
猜你喜欢
  • 1970-01-01
  • 2018-06-23
  • 2021-07-29
  • 2016-10-11
  • 2017-01-17
  • 2020-12-20
  • 2019-12-17
  • 1970-01-01
  • 2016-07-12
相关资源
最近更新 更多