闪亮的R中的动态网格图

时间:2015-11-23 09:15:57

标签: r graph shiny

我正在尝试从绘图对象列表中生成闪亮的图形网格。目前,所有图形都显示了绘图对象列表中的最后一个绘图。

我想以网格格式显示绘图列表中的绘图。

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

       })
    })
  }
})

2 个答案:

答案 0 :(得分:4)

正如@vongo所说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) 是一个非常棒的包,我鼓励你使用。但是如果你想在网格中组织你的html,引导网格系统对此非常有用。你可以这样做:

$name = 'utils';
$$name = new $name();

答案 1 :(得分:1)

您可以考虑使用ggplot2而不是基本情节,然后(R食谱上的Cf。this post)使用多重函数:

# 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)
  })
})

但也许我并没有理解你所有的限制。