Shiny - Use patchwork and coord_fixed with no white space in page

I need to create a shiny app in which the user decides how many plots to display and in how many columns via sliders. To merge the plots I used Patchwork, so that I could control the spaces between the plots using the cursor.
The plots have to use coord_fixed().

My question is: How can I control the size of the individual plots in the grid so that they occupy all the available space on the page?

This is a small executable example:

library(shiny)
library(shinythemes)
library(readr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(grid)
library(ggforce) 
library(patchwork)

ui <- fluidPage(
    
    navbarPage("MotorBrain", 
               tabsetPanel(id = "inTabset",
                        tabPanel("Configurazione",
                        sidebarLayout(
                            sidebarPanel( 
                                sliderInput("input1", "N. utenti",
                                            min = 1, max = 133,
                                            value = 3),
                                sliderInput("nCol1", "Ncols",
                                            min = 1, max = 32,
                                            value = 1),
                                sliderInput("h_space", "top bottom space (pt)",
                                            min = 1, max = 100,
                                            value = 1),
                                sliderInput("v_space", " left right space (pt)",
                                            min = 1, max = 100,
                                            value = 1),
                                actionButton("goButton1", "Visualizza")),
                            fluidRow())),
    tabPanel("Visualizzazione", 
             fluidRow(
                 uiOutput("outputTest1"))
             
    ))))




server <- function(input, output,session) {
    
    observeEvent(input$goButton1, {
        updateTabsetPanel(session, "inTabset", selected = "Visualizzazione")
    })
    
    input1<-eventReactive(input$goButton1,{
        input$input1
    })
    output$outputTest1 <- renderUI({ 
        pl <- vector("list", length =  input1())
        for(t in 1: input1()) {
            p<-ggplot() +
                geom_point(x=runif(10000,0, 1),y=runif(500,0, 1))+
                coord_fixed() +
                # the space is added to both sides, so we need to reduce by half
                theme(plot.margin = unit(c(
                    input$h_space/2,
                    input$v_space/2,
                    input$h_space/2,
                    input$v_space/2
                ), "pt"))
            
            pl[[t]] <-p
            
        }
        
        output$plot_test1<- renderPlot({
            Reduce(`+`, pl) +
                plot_layout(ncol = input$nCol1)
        })
        
        plotOutput(outputId = "plot_test1")
        
    })                 
    
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.