Search code examples
rmatrixshinytabsscatter-plot

Scatter plots for different variables in a single shiny app


I use R shiny to create multiple tabs for the scatter plot of different variables in a single app. My code seems alright, but the error says I have not "defined my main panel." Can someone please help where I have gone wrong, or if my whole approach is inaccurateenter code here, please let me know!

library(shiny)
library(tidyverse)
India <- read.csv("D:/R/Practice 3/Indiadata.csv")

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Deaths vs all variables "),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          selectInput("Deaths", "All variables:",
                      choices = c("cases"="total_cases","vaccinations"="total_vaccinations",
                                  "people vaccinated"="people_vaccinated","people fully vaccinated"="people_fully_vaccinated",
                                  "total booster"="total_boosters","new vaccinations"="new_vaccinations", "median age"="median_age"))
   
        )
        ),
        
        mainPanel(
          tabsetPanel(type = "tabs",
                      tabPanel(plotOutput("plot1")),
                      tabPanel(plotOutput("plot2")),
                      tabPanel(plotOutput("plot3")),
                      tabPanel(plotOutput("plot4")),
                      tabPanel(plotOutput("plot5")),
                      tabPanel(plotOutput("plot6")),
                      tabPanel(plotOutput("plot7"))
        
    )
)
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$plot1 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=total_cases))
       
    })
    output$plot2 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=total_vaccinations))
      
    })
    output$plot3 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=people_vaccinated))
      
    })
    output$plot4 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=people_fully_vaccinated))
      
    })
    output$plot5 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=total_boosters))
      
    })
    output$plot6 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=new_vaccinations))
      
    })
    output$plot7 <- renderPlot({
      ggplot(India,aes(y=total_deaths,x=median_age))
      
    })
}

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

Solution

  • mainPanel is an argument of the sidebarLayout() function. So, you just need to move it up into the sidebarLayout() function:

    ui <- fluidPage(
      
      # Application title
      titlePanel("Deaths vs all variables "),
      
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
        sidebarPanel(
          selectInput("Deaths", "All variables:",
                      choices = c("cases"="total_cases","vaccinations"="total_vaccinations",
                                  "people vaccinated"="people_vaccinated","people fully vaccinated"="people_fully_vaccinated",
                                  "total booster"="total_boosters","new vaccinations"="new_vaccinations", "median age"="median_age"))
        ),
        mainPanel(
          tabsetPanel(type = "tabs",
                      tabPanel(plotOutput("plot1")),
                      tabPanel(plotOutput("plot2")),
                      tabPanel(plotOutput("plot3")),
                      tabPanel(plotOutput("plot4")),
                      tabPanel(plotOutput("plot5")),
                      tabPanel(plotOutput("plot6")),
                      tabPanel(plotOutput("plot7"))
          )
        )
      )
    )