2017-08-31 65 views
0

我已经使用grid.arrange查看了SO上的所有解决方案,但它并没有真正实现我想要的。在网格中显示未知数量的图表RShiny

我们假设我有一个在RShiny的反应环境中生成的grobs列表。

我想创建一个mainPanel其中那些图是2列(直到这一点,它的所有可行与grid.arrange),但是,其中每个行对应一个fluidRow元件。

这方面的一个准系统例子是

ui <- fluidPage(

    titlePanel("TEST"), 
    sidebarPanel(width=3, 
     actionButton(inputId = 'launchCalcButton',label = 'Launch Calc') 
    ), 
    mainPanel(
     uiOutput("resultsPlotUI") 
    ) 
) 

server <- function(input,output){ 

    graphsList <- eventReactive(input$launchCalcButton, { 
     a <- lapply(1:10,function(i){ 
      return(
       ggplot(data = data.frame(a=rnorm(10),b=rnorm(10)),aes(x=a,y=b)) 
       +geom_point() 
      ) 
     }) 
     return(a) 
    }) 
    output$resultsPlot <- renderPlot({ 
     do.call(grid.arrange,c(graphsList(),ncol=2)) 
    }) 

    output$resultsPlotUI <- renderUI({ 
     fluidRow(
      column(12, 
       plotOutput(
        outputId = 'resultsPlot' 
       ) 
      ) 
     ) 
    }) 
} 

app = shinyApp(ui,server) 
runApp(app) 

所有图形最终挤压成一个单一的线,而我希望他们线之间进行分割。

Screenshot

回答

0

你只需要设置高度参数为plotOutput

library(shiny) 
library(gridExtra) 
ui <- fluidPage(

    titlePanel("TEST"), 
    sidebarPanel(width=3, 
       actionButton(inputId = 'launchCalcButton',label = 'Launch Calc') 
), 
    mainPanel(
    uiOutput("resultsPlotUI") 
) 
) 

server <- function(input,output){ 

    graphsList <- eventReactive(input$launchCalcButton, { 
    a <- lapply(1:10,function(i){ 
     return(
     ggplot(data = data.frame(a=rnorm(10),b=rnorm(10)),aes(x=a,y=b)) 
     +geom_point() 
    ) 
    }) 
    return(a) 
    }) 
    output$resultsPlot <- renderPlot({ 
    l <- length(graphsList())/2 
    print(l) 
    do.call(grid.arrange,c(graphsList(),ncol=2)) 
    }) 

    output$resultsPlotUI <- renderUI({ 
    fluidRow(
     column(12, 
      plotOutput(
       outputId = 'resultsPlot', height = 600 
      ) 
    ) 
    ) 
    }) 
} 

app = shinyApp(ui,server) 
runApp(app) 

正是在这个地方:

output$resultsPlotUI <- renderUI({ 
    fluidRow(
     column(12, 
      plotOutput(
       outputId = 'resultsPlot', height = 600 
      ) 
    ) 
    ) 
    }) 

我已经将它设置为600像素(height = 600 )但你可以选择任何你想要的。

enter image description here

+0

感谢您的答案,但我想它使用默认的行高,并创建必要的UI新fluidRow的解决方案。 – Chapo