Home > front end >  Place plot at bottom left (below sidebar panel)
Place plot at bottom left (below sidebar panel)

Time:03-21

I have this app.

library(shiny)
library(tidyverse)
library(dplyr)


a <- 1:5
df <- tibble(a, b = a * 2, c = b * 3)

a <- c(2, 3, 4, 1, 5)
test <- tibble(a, b = a * 2, c = b * 3)

ui <- fluidPage(
    
    titlePanel("The title"),
    

                    sidebarLayout(
                        sidebarPanel(
                            
                            fluidRow(
                                column(6, 
                                       wellPanel(
                                           
                                           selectInput("features", "Choose a feature",
                                                       c("All", "b", "c"))
                                       ),
                                       
                                       column(6, 
                                              selectInput("features_dens", "Choose a feature for density plots",
                                                          c("All", "b", "c"))
                                       )
                                )
                            )
                            
                            
                            
                            
                            
                            
                        ),
                        
                        
                        # Show a plot of the generated distribution
                        mainPanel(
                            tabsetPanel(
                                
                                tabPanel("Plot",
                                         fluidRow(column(10, plotOutput("actual")),
                                                  column(10, plotOutput("resids")))
                                ),
                                tabPanel("Summary", verbatimTextOutput("summary")),
                                tabPanel("Table", dataTableOutput("sum_stats_table"))
                            )
                            
                            
                            
                        )
                    )
    
    
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    output$summary <- renderPrint({
        
        if (input$features == "All"){
            df_lm <- lm(a~., data = df)
        } else if (input$features == "b") {
            df_lm <- lm(a~ b, data = df)
        } else if (input$features == "c") {
            df_lm <- lm(a~ c, data = df)
        }
        
        summary(df_lm)
        
        
    })
    
    output$sum_stats_table <- renderDataTable({
        
        
        if (input$features == "All"){
            df_lm <- lm(a~., data = df)
        } else if (input$features == "b") {
            df_lm <- lm(a~ b, data = df)
        } else if (input$features == "c") {
            df_lm <- lm(a~ c , data = df)
        }
        
        psych::describe(df, fast = TRUE)
        
    })
    
    output$actual <- renderPlot({
        
        if (input$features == "All"){
            df_lm <- lm(a~., data = df)
        } else if (input$features == "b") {
            df_lm <- lm(a~ b, data = df)
        } else if (input$features == "c") {
            df_lm <- lm(a~ c ,data = df)
        }
        
        preds <- predict(df_lm, test)
        
        ggplot(data = test, aes(x = preds, y = a))  
            geom_point(alpha = 0.8, color = "darkgreen")  
            geom_smooth(color="darkblue")  
            geom_line(aes(x = (a),
                          y = (a)),
                      color = "blue", linetype=2)  
            labs(title="Actual vs Predicted",
                 x ="Predicted", y = "Actual")
        
    })
    
    output$resids <- renderPlot({
        
        if (input$features == "All"){
            df_lm <- lm(a~., data = df)
        } else if (input$features == "b") {
            df_lm <- lm(a~ b, data = df)
        } else if (input$features == "c") {
            df_lm <- lm(a~ c , data = df)
        }
        
        preds <- predict(df_lm, test)
        
        ggplot(data=test, aes(x = preds,
                              y = preds - a))  
            geom_point(alpha=0.8, color="darkgreen")  
            geom_smooth(color="darkblue")  
            labs(title="Residuals",
                 x ="Predicted", y = "residual error (prediction - actual)")
        
    })
    
    output$density <- renderPlot(
        {
            ggplot(df, aes(x=input$features_dens))  
                geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth = 0.5) 
                geom_density(alpha=0.7, fill='#336eff')
            
        }
    )
    
}

# Run the application 
shinyApp(ui = ui, server = server)

which produces the following image:

image

I want to place the output$density plot below the sidebar panel (bottom left)

CodePudding user response:

You have to make your own sidebar layout. Like this:

ui <- fluidPage(
  titlePanel("The title"),
  fluidRow(
    # sidebar
    column(
      width = 4,
      tags$form(
        class = "well",
        fluidRow(
          column(
            6,
            wellPanel(
              selectInput(
                "features", "Choose a feature",
                c("All", "b", "c")
              )
            ),
            column(
              6,
              selectInput(
                "features_dens", "Choose a feature for density plots",
                c("All", "b", "c")
              )
            )
          )
        )
      ),
      br(),
      plotOutput("density")
    ),
    
    # main panel
    # Show a plot of the generated distribution
    column(
      width = 8,
      role = "main",
      tabsetPanel(
        tabPanel(
          "Plot",
          fluidRow(
            column(10, plotOutput("actual")),
            column(10, plotOutput("resids"))
          )
        ),
        tabPanel("Summary", verbatimTextOutput("summary")),
        tabPanel("Table", dataTableOutput("sum_stats_table"))
      )
    )
  )
)

enter image description here

There's an error in your server code:

  output$density <- renderPlot({
    ggplot(df, aes(x = input$features_dens))  
      geom_histogram(aes(y = ..density..), colour = "black", fill = "white", binwidth = 0.5)  
      geom_density(alpha = 0.7, fill = "#336eff")
  })

to be replaced with:

  output$density <- renderPlot({
    ggplot(df, aes_string(x = input$features_dens))  
      ......
  })

But note that this renders an error when you choose "All".

  • Related