首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用闪亮的应用程序中的滑块控制grid.arrange中的水平和垂直空间

用闪亮的应用程序中的滑块控制grid.arrange中的水平和垂直空间
EN

Stack Overflow用户
提问于 2021-12-09 13:36:34
回答 1查看 151关注 0票数 1

我需要创建一个闪亮的应用程序,在这个应用程序中,用户通过滑块决定要显示多少个图表和多少列。要合并我使用的函数grid.arrange (但它不是强制性的)的图形,重要的是要有ggplot对象。我需要coord_fixed()。这是一个小的可执行示例:

代码语言:javascript
复制
library(shiny)
library(shinythemes)
library(readr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(grid)
library(gridExtra)
library(ggforce) 
library(shinythemes)

ui <- fluidPage(
 
    navbarPage("MotorBrain", 
               
      tabPanel("Configurazione",
               sidebarLayout(
                   sidebarPanel( 
                      sliderInput("input1", "N. utenti",
                                    min = 1, max = 133,
                                    value = 3),
                       sliderInput("nCol1", "Ncols",
                                    min = 1, max = 32,
                                    value = 1),
                       actionButton("goButton1", "Visualizza")),
                       fluidRow()))),
      tabPanel("Visualizzazione", 
            fluidRow(
                   uiOutput("outputTest1"))
               
 ))




server <- function(input, output,session) {
    
 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()
                             
 pl[[t]] <-p
                         
 }
                        
output$plot_test1<- renderPlot({
                    
 grid.arrange(grobs=pl,ncol=input$nCol1,top="")
                                
  })
                        
plotOutput(outputId = "plot_test1")
                        
 })                 

}

shinyApp(ui = ui, server = server)

我需要再添加两个滑块,用户可以用它来决定在网格中的不同图形之间放置多少水平和垂直的空白。我该怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-10 00:58:13

gridExtra在列之间增加了不可控制的空间,因此一种解决方案是使用patchwork,在这里没有添加额外的空间。然后我们可以使用margin技巧来添加空间:

代码语言:javascript
复制
library(shiny)
library(shinythemes)
library(readr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(grid)
library(ggforce) 
library(patchwork)

ui <- fluidPage(
    
    navbarPage("MotorBrain", 
               
               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) {
    
    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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70290871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档