R动态绘图,以生成4个绘图,而不是1个绘图

R动态绘图,以生成4个绘图,而不是1个绘图,r,shiny,R,Shiny,我想修改此代码: 因此,将显示4个绘图,而不是1个绘图 在当前代码中,如果输入$n=1您将看到1个绘图 plot1 或者输入$n=3您将看到3个图: plot1 plot2 plot3 我需要在2x2的布局中显示4个绘图,这样如果input$n=1您可以看到 plot1A plot1B plot1C plot1D 对于输入$n=2,您将看到 plot1A plot1B plot1C plot1D plot2A plot2B plot2C plot2D 对于输入$n=

我想修改此代码:

因此,将显示4个绘图,而不是1个绘图

在当前代码中,如果
输入$n=1
您将看到1个绘图

plot1 
或者
输入$n=3
您将看到3个图:

plot1

plot2

plot3
我需要在2x2的布局中显示4个绘图,这样如果
input$n=1
您可以看到

plot1A  plot1B
plot1C  plot1D
对于
输入$n=2
,您将看到

plot1A  plot1B
plot1C  plot1D

plot2A  plot2B
plot2C  plot2D
对于
输入$n=3
,您将看到:

plot1A  plot1B
plot1C  plot1D

plot2A  plot2B
plot2C  plot2D

plot3A  plot3B
plot3C  plot3D
等等

有人可以修改该代码,以便在上面看到的相同2x2框架中显示4次绘图吗

以下是我的想法:

这是我的ui.R:

shinyUI(pageWithSidebar(
  headerPanel("Dynamic number of plots"),
  sidebarPanel(

sliderInput("n", "Number of plots", value=1, min=1, max=5)
  ),

  mainPanel(
# This is the dynamic UI for the plots
h3("Heading 1"),
uiOutput("plots"),
h3("Heading 2"),
verbatimTextOutput("test")
  )
))
这是我的服务器。r:

library("gridExtra")
library(ggplot2)

shinyServer(function(input, output) {

  # Insert the right number of plot output objects into the web page
  output$plots <- renderUI({
      plot_output_list <- lapply(1:(input$n), 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)
      do.call(grid.arrange,  plot_output_list)

  })##End outputplots

  #PrintPlots<- reactive({
  observe({
    max_plots<-length(getAugmentedTerms(input$Desk, input$Product, input$File1))

    #print(max_plots)
    # Call renderPlot for each one. Plots are only actually generated when they
    # are visible on the web page.
    for (i in 1:max_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({
    my_i <- i
    plotname <- paste("plot", my_i, sep="")

    p1<-qplot(1:10)

    p2<-qplot(1:10)

    p3<- qplot(1:10)

    p4<-qplot(1:10)



        output[[plotname]] <- renderPlot({

         #grid.arrange(  arrangeGrob
          arrangeGrob(  p1, p2,p3,p4, ncol= 2)
       })#End RENDER PLOT
      })#END LOCAL
    }##### End FoR Loop
  })# END OF PRINT PLOTS

})# END OF ENERYTHING

谢谢。

我不太明白你的问题

顺便说一下,如果你使用标准图(如你的主旨),你可以简单地使用PAR选项。
output[[plotname]] <- renderPlot({
  par(mfrow = c(2,2))
  p1 = plot(...)
  p2 = plot(...)
  p3 = plot(...)
  p4 = plot(...)
})

希望我能回答您的问题。

这些不是标准绘图,因为它们是动态渲染的。您知道如何修改代码以显示动态图表吗?请提供一个您想要绘制的示例。正如您在上面的代码中看到的,绘图已经动态呈现:它们取决于输入值my_i。
output[[plotname]] <- renderPlot({
  par(mfrow = c(2,2))
  p1 = plot(...)
  p2 = plot(...)
  p3 = plot(...)
  p4 = plot(...)
})
output[[plotname]] <- renderPlot({
  qp1 <- qplot(1:my_i, 1:my_i)
  qp2 <- qplot(1:my_i, 1:my_i)
  qp3 <- qplot(1:my_i, 1:my_i)
  qp4 <- qplot(1:my_i, 1:my_i)
  multiplot(qp1, qp2, qp3, qp4, cols=2)
})