library(shiny) library(ggplot2)
data(“birthwt”)
ui <- fluidPage( titlePanel(“Low Birth Weight by Uterine Irritability”), sidebarLayout( sidebarPanel( # Add a checkbox to filter by smoking status checkboxInput(“smokeFilter”, “Filter by Smoking Status:”, choices = c(“All”, “Smoker”, “Non-smoker”)), # Add a slider to adjust the physician visit threshold sliderInput(“ftvThreshold”, “Physician Visits Threshold:”, min = 1, max = 4, value = 2, step = 1) ), mainPanel( plotOutput(“birthweightPlot”) ) ) )
server <- function(input, output) { output\(birthweightPlot <- renderPlot({ birthwt2 <- within(birthwt, { race <- factor(race, labels = c("white", "black", "other")) ptd <- factor(ptl > 0) # premature labors ftv <- factor(ftv) levels(ftv)[ftv > input\)ftvThreshold] <- “2+” smoke <- factor(smoke > 0) ht <- factor(ht > 0) ui <- factor(ui > 0) })
# Filter by smoking status if selected
if (input$smokeFilter != "All") {
birthwt2 <- birthwt2[birthwt2$smoke == (input$smokeFilter == "Smoker")]
}
ggplot(birthwt2, aes(x = ui, fill = as.factor(low))) +
geom_bar(position = "fill") +
labs(title = "Low Birth Weight by Uterine Irritability",
x = "Uterine Irritability",
y = "Proportion",
fill = "Low Birth Weight") +
scale_x_discrete(labels = c("No", "Yes"))
}) }
shinyApp(ui = ui, server = server)