首页 > 解决方案 > 单个闪亮应用程序中不同变量的散点图

问题描述

我使用 R shiny 为单个应用程序中不同变量的散点图创建多个选项卡。我的代码看起来没问题,但错误提示我没有“定义我的主面板”。有人可以帮助我哪里出错,或者如果我的整个方法不准确,请在这里输入代码,请告诉我!

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)

标签: rmatrixshinytabsscatter-plot

解决方案


mainPanelsidebarLayout()函数的参数。所以,你只需要把它移到sidebarLayout()函数中:

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"))
      )
    )
  )
)

推荐阅读