簡體   English   中英

閃亮的R中的動態網格圖

[英]Dynamic grid plots in shiny R

我正在嘗試從繪圖對象列表中生成閃亮的圖形網格。 當前,所有圖形都顯示繪圖對象列表中的最后一個繪圖。

我想以網格格式顯示圖列表中的圖。

shinyServer(function(input, output) {

  # Insert the right number of plot output objects into the web page
  output$plots <- renderUI({
    plot_output_list <<- lapply(1:length(plots), function(i) {
      plotname <- paste("plot", i , sep="")
      plotOutput(plotname, height = 280, width = 250)
    })

    # Convert the list to a tagList - this is necessary for the list of items
    # to display properly.
    do.call(tagList, plot_output_list)
  })

  # Call renderPlot for each one. Plots are only actually generated when they
  # are visible on the web page.
  max_plots = length(plots)
  for (i in 1:length(plots)) {
    # Need local so that each item gets its own number. Without it, the value
    # of i in the renderPlot() will be the same across all instances, because
    # of when the expression is evaluated.
    local({

      plotname <- paste("plot", i , sep="")
      plotnames[i] <<- plotname
      output[[plotname]] <- renderPlot({
        plots[[i]] # plots - list of plot objects which I am trying to display as grids

       })
    })
  }
})

正如@vongo所說的, ggplot2是一個非常棒的軟件包,我鼓勵您使用。 但是,如果您想在網格中組織html,引導網格系統確實非常適合此功能。 您可以執行以下操作:

require(shiny)

ui <- shinyUI(fluidPage(
  uiOutput('plots')
))

server <- shinyServer(function(input, output) {

  plots <- lapply(1:10, function(i){
    plot(runif(50),main=sprintf('Plot nr #%d',i)) 
    p <- recordPlot()
    plot.new()
    p
  })
  n.col <- 3

  output$plots <- renderUI({
    col.width <- round(12/n.col) # Calculate bootstrap column width
    n.row <- ceiling(length(plots)/n.col) # calculate number of rows
    cnter <<- 0 # Counter variable

    # Create row with columns
    rows  <- lapply(1:n.row,function(row.num){
        cols  <- lapply(1:n.col, function(i) {
          cnter    <<- cnter + 1
          plotname <- paste("plot", cnter, sep="")
          column(col.width, plotOutput(plotname, height = 280, width = 250))
        }) 
        fluidRow( do.call(tagList, cols) )
    })

    do.call(tagList, rows)
  })

  for (i in 1:length(plots)) {
    local({
      n <- i # Make local variable
      plotname <- paste("plot", n , sep="")
      output[[plotname]] <- renderPlot({
        plots[[n]]
      })
    })
  }
})

shinyApp(ui=ui,server=server)

您可以考慮使用ggplot2而不是基本繪圖,然后(參見R cookbook上的該帖子 )使用multiplot函數:

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

這樣一來,您就可以在Shiny服務器端將一組地塊視為一個地塊,例如:

shinyServer(function(input, output) {
  # Insert the right number of plot output objects into the web page
  output$plots <- renderPlot({
    plot_output_list <- sapply(1:length(plots), function(i) {
      plotname <- paste("plot", i , sep="")
      plotOutput(plotname, height = 280, width = 250)
    })
    multiplot(plot_output_list)
  })
})

但是也許我不了解您的所有限制。

暫無
暫無

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

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