0

I have some problems displaying plots. They are added dynamically: the more variables selected the more plots are plotted. The problem is there are no space respect.

This is the code:

dades <- iris
binary_variable <- factor(sample(x = c(0, 1), size = nrow(dades), replace = TRUE))
dades <- cbind(iris, binary_variable)

ui <- fluidPage(
  column(2, ),
  column(8, 
         
         fluidRow(
           
           column(4, 
                  selectInput("resposta", "Dependent variable", choices = names(dades))
           ),
           column(4,
                  textInput("explicatives", "Independent variables")
           ),
           column(4,
                  actionButton("executar", "Run")
           )
         ),
         fluidRow(align = "center",
                  verbatimTextOutput("resultat"),
                  uiOutput("grafics")
         )
         
  ),
  column(2, )
)

server <- function(input, output, session) {
  
  model <- reactive({
    
    if(input$executar == 0){
      
      return(invisible(NULL))
      
    }else{
      
      isolate({
        
        resposta2 <- factor(dades[, input$resposta])
        etiquetes <- levels(resposta2)
        levels(resposta2) <- c(0, 1)
        resposta2 <- factor(resposta2, levels = c(0, 1), labels = etiquetes)
        
        f <- as.formula(paste0("resposta2 ~ ", input$explicatives))
        
        
        glm(formula = f, family = binomial, data = dades)
        
      })
      
    }
    
  })

  output$resultat <- renderPrint({
    
    if(input$executar == 0){
      
      return(invisible(NULL))
      
    }else{
      
      isolate({
        
        summary(model())
        
      })
      
    }
    
  })

  observe({
    
    if(input$executar == 0) {
      
      return(invisible(NULL))
      
    } else {
      
      lapply(names(model()$model)[-1], function(par){
        
        
        if (is.factor(model()$model[, par]) || is.character(model()$model[, par]) || is.integer(model()$model[, par])) {
          
          taula <- as.data.frame(table(model()$model$resposta2, model()$model[, par]))
          p <- plot_ly(taula, x = ~ Var1, y = ~Freq, color = ~Var2, type = "bar") %>% 
            layout(title = NULL, xaxis = list(title = ""), yaxis = list(title = ""), height = 500, width = 500, inline = TRUE)
          output[[paste("plot", par, sep = "_")]] <- renderPlotly({
            p
          })
          
        } else if (is.numeric(model()$model[, par])){
          
          p <- plot_ly(model()$model, y = ~model()$model[, par], color = ~resposta2, type = "box") %>%
            layout(title = NULL, xaxis = list(title = ""), yaxis = list(title = ""), height = 500, width = 500, inline = TRUE)
          output[[paste("plot", par, sep = "_")]] <- renderPlotly({
            p
          })
          
          
        }
        
        
      })
      
    }
    
  })
  
  output$grafics <- renderUI({
    
    if(input$executar == 0) {
      
      return(invisible(NULL))
      
    } else {
      
      plot_output_list <- lapply(names(model()$model)[-1], function(par) {
        plotname <- paste("plot", par, sep = "_")
        plotlyOutput(plotname)
      })
      
      do.call(flowLayout, plot_output_list)
      
    }
    
  })

}

shinyApp(ui, server)

In the "Dependent variable" input you must select "binary_variable" and in the "Independent variables" input something like "Sepal.Length + Sepal.Width + Species". The problem is the plots are like superimposed, it's like there are not enough space between them. How can I fix this?

Miquel
  • 442
  • 3
  • 14

1 Answers1

1

While you cannot specify the width and height in layout(), you can let it autosize. Also, it is better to put the legend at the bottom, as multiple plots are displayed horizontally. Try this

ui <- fluidPage(
  column(2, ),
  column(8, 
         
         fluidRow(
           
           column(4, 
                  selectInput("resposta", "Dependent variable", choices = names(dades))
           ),
           column(4,
                  textInput("explicatives", "Independent variables")
           ),
           column(4,
                  actionButton("executar", "Run")
           )
         ),
         fluidRow(# align = "center",
           column(12, verbatimTextOutput("resultat")),
           column(12, uiOutput("grafics"))
         )
         
  ),
  column(2, )
)

server <- function(input, output, session) {
  
  model <- reactive({
    
    if(input$executar == 0){
      
      return(invisible(NULL))
      
    }else{
      
      isolate({
        
        resposta2 <- factor(dades[, input$resposta])
        etiquetes <- levels(resposta2)
        levels(resposta2) <- c(0, 1)
        resposta2 <- factor(resposta2, levels = c(0, 1), labels = etiquetes)
        
        f <- as.formula(paste0("resposta2 ~ ", input$explicatives))
        
        glm(formula = f, family = binomial, data = dades)
        
      })
      
    }
    
  })
  
  output$resultat <- renderPrint({
    
    if(input$executar == 0){
      
      return(invisible(NULL))
      
    }else{
      
      isolate({
        
        summary(model())
        
      })
      
    }
    
  })
  
  observe({
    
    if(input$executar == 0) {
      
      return(invisible(NULL))
      
    } else {
      
      lapply(names(model()$model)[-1], function(par){
        
        
        if (is.factor(model()$model[, par]) || is.character(model()$model[, par]) || is.integer(model()$model[, par])) {
          
          taula <- as.data.frame(table(model()$model$resposta2, model()$model[, par]))
          p <- plot_ly(taula, x = ~ Var1, y = ~Freq, color = ~Var2, type = "bar") %>% 
            layout(legend = list(orientation = "h"), title = NULL, xaxis = list(title = ""), yaxis = list(title = ""), autosize=TRUE )
          output[[paste("plot", par, sep = "_")]] <- renderPlotly({
            p
          })
          
        } else if (is.numeric(model()$model[, par])){
          
          p <- plot_ly(model()$model, y = ~model()$model[, par], color = ~resposta2, type = "box") %>%
            layout(legend = list(orientation = "h"), title = NULL, xaxis = list(title = ""), yaxis = list(title = ""), autosize=TRUE )
          output[[paste("plot", par, sep = "_")]] <- renderPlotly({
            p
          })
          
        }
      })
    }
    
  })
  
  output$grafics <- renderUI({
    
    if(input$executar == 0) {
      
      return(invisible(NULL))
      
    } else {
      
      plot_output_list <- lapply(names(model()$model)[-1], function(par) {
        plotname <- paste("plot", par, sep = "_")
        plotlyOutput(plotname)
      })
      
      do.call(flowLayout, plot_output_list)
      
    }
    
  })
  
}

shinyApp(ui, server)

output

YBS
  • 19,324
  • 2
  • 9
  • 27