[英]Shiny with reactive plot
我在下面有這個 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)
我們可以做的
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.