資料集:https : //www.kaggle.com/mirichoi0218/insurance
嗨,我對閃亮的圖書館有疑問。想要創建根據年齡、地區和兒童顯示費用的應用程式。不幸的是,直方圖不起作用。
ui <- fluidPage(
titlePanel("Insurance charges"),
sidebarLayout(
sidebarPanel(
sliderInput("ageInput", "Age", 18, 64, 41),
radioButtons("childrenInput", "Liczba dzieci",
choices = c("0", "1", "2", "3",'4','5'),
selected = "0"),
selectInput("regionInput", "Region",
choices = c("NE", "NW", "SE","SW"))
),
mainPanel(
plotOutput("coolplot"),
br(), br(),
tableOutput("results")
)
)
)
server <- function(input, output) {
output$coolplot <- renderPlot({
filtered <-
insurance %>%
filter(age >= input$ageInput[1],
age <= input$ageInput[2],
children == input$childrenInput,
region == input$regionInput
)
ggplot(filtered, aes(insurance$charges))
geom_histogram()
})
}
shinyApp(ui = ui, server = server)
uj5u.com熱心網友回復:
您的代碼的主要問題是
你地圖
insurance$charges上x在ggplot()。這就是為什么您總是會得到整個資料集的直方圖。只需用于aes(charges)使用filtered資料集中的列。不確定。也許你重新編碼了你的資料,但是 Kaggle 資料集中的區域有標簽“northeast”,“northwest”,...。考慮到這一點,你可以
choices在selectInput最后,你必須使用長度為2的向量為
value的sliderInput獲得范圍滑塊。
作為一個關于如何提供最小可重現示例的示例,我使用了一個隨機的假資料集作為 reprex。
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
titlePanel("Insurance charges"),
sidebarLayout(
sidebarPanel(
sliderInput("ageInput", "Age", min = 18, max = 64, value = c(18, 41)),
radioButtons("childrenInput", "Liczba dzieci",
choices = c("0", "1", "2", "3", "4", "5"),
selected = "0"
),
selectInput("regionInput", "Region",
choices = c("NE" = "northeast", "NW" = "northwest", "SE" = "southeast", "SW" = "southwest")
)
),
mainPanel(
plotOutput("coolplot"),
br(), br(),
tableOutput("results")
)
)
)
server <- function(input, output) {
output$coolplot <- renderPlot({
filtered <- insurance %>%
filter(
age >= input$ageInput[1],
age <= input$ageInput[2],
children == input$childrenInput,
region == input$regionInput
)
ggplot(filtered, aes(charges))
geom_histogram()
})
}
shinyApp(ui = ui, server = server)

資料
set.seed(123)
insurance <- data.frame(
age = sample(18:64, 1000, replace = TRUE),
children = sample(0:5, 1000, replace = TRUE),
region = sample(c("southwest", "southeast", "northwest", "northeast"), 1000, replace = TRUE),
charges = runif(1000, 3000, 40000)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409162.html
標籤:
