【问题标题】:Adding multiple reactive plots and tables to Shiny app向 Shiny 应用程序添加多个反应图和表格
【发布时间】:2017-08-22 01:54:09
【问题描述】:

我正在开发一个 Shiny 应用程序,我一直在以随意的方式添加图形和表格。我希望有一个更好的框架,以便随着它的进一步发展,我可以灵活地将反应式图形和表格添加到输出中。

目前我一直在使用 tabPanel 和 Fluidrow 添加额外的汇总表和第二个图。但是,我在适应这一点时遇到了麻烦。例如,我目前生成 3 个图,但一次只能绘制 2 个。谁能告诉我一种修改代码以在同一页面上显示所有三个图(distPlot1、distPlot2、distPlot3)和汇总表的方法?理想情况下,将来可以很容易地添加其他表格和绘图。

提前谢谢你。

我当前的代码如下。

ui.R

library(reshape2)
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
fluidPage(

  # Application title
  titlePanel("Mutation Probability"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("x", "Probability of mutation (per bp):", 
                  min=1/1000000000000, max=1/1000, value=1/10000000),

      sliderInput("y", "Size of region (bp):", 
                  min = 10, max = 10000, value = 1000, step= 100),

      sliderInput("z", "Number of samples:", 
                  min = 1, max = 100000, value = 1000, step= 10)

    ),

    # Show a plot of the generated distribution
    mainPanel(
      tabsetPanel(
        tabPanel("Plot",
          fluidRow(
            splitLayout(cellWidths = c("50%", "50%"), plotOutput("distPlot1"), plotOutput("distPlot3"), plotOutput("distPlot3)"))
          )),
        tabPanel("Summary",  verbatimTextOutput("summary"))
      )
    )
  )
)

服务器.R

server <- function(input, output) {

  mydata <- reactive({
    x <- input$x
    y <- input$y
    z <- input$z
    Muts <- as.data.frame(rpois(100,(x*y*z)))
    Muts
  })


  output$distPlot1 <- renderPlot({
    Muts <- mydata()
    ggplot(Muts, aes(Muts)) + geom_density() +xlab("Observed variants")
  })

  output$distPlot2 <-renderPlot({
    Muts <- mydata()
    ggplot(Muts, aes(Muts)) + geom_histogram() + xlab("Observed variants")
  })
  #get a boxplot working
  output$distPlot3 <-renderPlot({
    Muts <- mydata()
    ggplot(data= melt(Muts), aes(variable, value)) + geom_boxplot() + xlab("Observed variants")
  })

  output$summary <- renderPrint({
    Muts <- mydata()
    summary(Muts)
  })


}

【问题讨论】:

  • 你不应该改变分割宽度以适应三个地块吗?不是 50%,你不是需要 33% 才能连续获得三个吗?
  • 我试过了,但它似乎没有用,只是让前 2 个地块变小了。但可能我做得不对。

标签: r plot ggplot2 shiny data-visualization


【解决方案1】:

我喜欢使用 grid.arrange 等工具在服务器中布置图形,这些工具来自 gridExtra 包或 cowplot 包 - 它们提供了很多布局灵活性。例如:

library(reshape2)
library(shiny)
library(ggplot2)
library(gridExtra)
# Define UI for application that draws a histogram

u <- fluidPage(

  # Application title
  titlePanel("Mutation Probability"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("x", "Probability of mutation (per bp):", 
                  min=1/1000000000000, max=1/1000, value=1/10000000),

      sliderInput("y", "Size of region (bp):", 
                  min = 10, max = 10000, value = 1000, step= 100),

      sliderInput("z", "Number of samples:", 
                  min = 1, max = 100000, value = 1000, step= 10)

    ),

    # Show a plot of the generated distribution
    mainPanel(
      tabsetPanel(
        tabPanel("Plot",
                 fluidRow(
                   plotOutput("distPlot4"),
                   verbatimTextOutput("summary"))
                 )),
        tabPanel("Summary",  verbatimTextOutput("summary1"))
      )
    )
  )
)
s <- function(input, output) {

  mydata <- reactive({
    x <- input$x
    y <- input$y
    z <- input$z
    Muts <- as.data.frame(rpois(100,(x*y*z)))
    Muts
  })
  output$distPlot4 <- renderPlot({
    Muts <- mydata()
    p1 <- ggplot(Muts, aes(Muts)) + geom_density() +xlab("Observed variants")
    p2 <- ggplot(Muts, aes(Muts)) + geom_histogram() + xlab("Observed variants")
    p3 <- ggplot(data= melt(Muts), aes(variable, value)) + geom_boxplot() + xlab("Observed variants")
    grid.arrange(p1,p2,p3, ncol=3,widths = c(2,1,1))
  })
  output$summary <- renderPrint({
    Muts <- mydata()
    summary(Muts)
  })
}
shinyApp(u,s)

产生:

对于汇总表,我只是将它们一个接一个地添加到底部,我认为您可以在那里做的不多。

【讨论】:

  • 非常感谢。正是我一直在寻找的那种灵活性。
  • 在定义每个地块的大小、位置和功能时可以这样做吗?
  • 当然可以,但具体如何做到最好取决于细节。
  • 在不创建新帖子的情况下向您展示我的 UI 的最佳方式是什么?
  • 我会在早上看看,目前正在休假,所以我的编辑和测试能力受到严重限制:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 2021-06-02
  • 2020-04-01
  • 2020-10-27
  • 2010-09-07
相关资源
最近更新 更多