闪亮的反应图

时间:2021-02-06 00:24:25

标签: r shiny

我在下面有这个闪亮的代码,我想改进它。 直方图未显示且侧边栏无法正常工作

library(shiny)
library(ggplot2)

#View(midwest)


# User interface ----

            ui <- fluidPage(
               titlePanel("Midwest"),
    
                     sidebarLayout(
                          sidebarPanel(
                              sliderInput(inputId = "bins", 
                                   label = "Population Density",
                                      min = 0, max = 8000, value = 2000, step = 1000),
            
                   radioButtons("radio", 
                         label = "States",
                         choices = midwest$state,
                         selected = "IL"),
            
                   selectInput("select", "Population Total", 
                        choices = midwest$poptotal)
            
            
        ),
        
               mainPanel(plotOutput(outputId = "distPlot"),
                  
                    tableOutput("view"))
    )
)

            server <- function(input, output) {
    
    
          output$distPlot <- renderPlot({
        
                 x    <- midwest$area
                 x <- na.omit(x)
                 bins <- seq(min(x), max(x),length.out = input$bins +1)
        
                  hist(x, breaks = bins, col = "#003366", border = "white")
        
        
    })
                  output$view <- renderTable({
                  head(midwest)
    })
}

# Run the app ----
                shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

我们可以做到

library(shiny)
library(ggplot2)
data(midwest)
ui <- fluidPage(
  titlePanel("Midwest"),
  
  sidebarLayout(
    sidebarPanel(
     
      
      selectInput("state", 
                   label = "States",
                   choices = unique(midwest$state),
                   selected = "IL")
      
      
    ),
    
    mainPanel(plotOutput(outputId = "distPlot"),
              
              tableOutput("view"))
  )
  
)


server <- function(input, output) {
  
  
 
  output$distPlot <- renderPlot({
    submidwest <- subset(midwest, state == input$state)
    
    x    <- submidwest$area
    
    x <- na.omit(x)
    
    
    hist(x, col = "#003366", border = "white")
    
    
  })
  output$view <- renderTable({
    head(midwest)
  })
  
}


shinyApp(ui = ui, server = server)