आपका पहला shinyApp
आपने देखा कि इनपुट्स और आउटपुट्स को shinyApp में कैसे जोड़ा जा सकता है. आपने यह भी देखा कि स्लीप स्टडी के नतीजों को संप्रेषित करने के लिए shinyApp कैसे बनाया जा सकता है.
अब आपका लक्ष्य है कि आप एक shinyApp बनाएँ जो दो मुख्य परिणाम बताए:
- नींद के घंटों का वितरण
- अलग-अलग आयु समूहों में माध्यिका (median) नींद के घंटे
इस अभ्यास में, डेटा को sleep नाम के dplyr डेटा फ्रेम में संग्रहीत किया गया है, और shiny तथा tidyverse लाइब्रेरी पहले से लोड हैं.
अब आपकी बारी है — अपना खुद का shinyApp बनाइए!
यह अभ्यास पाठ्यक्रम का हिस्सा है
shinydashboard के साथ डैशबोर्ड बनाना
अभ्यास निर्देश
mainPanelमें "histogram" और "barchart" नाम के दो plot outputs जोड़ें.checkboxGroupInput()केchoiceNamesआर्ग्युमेंट में, इसकी सामग्री को "calendar", "briefcase" और "gift" नाम के आइकॉन से बदलें.- दो आउटपुट परिभाषित करें: एक
histogramऔर दूसराbarchart. - shiny ऐप को रेंडर करने के लिए
shinyApp()फ़ंक्शन का उपयोग करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
ui <- fluidPage(
titlePanel("Sleeping habits in America"),
fluidRow(
# Place two plots here, called "histogram" and "barchart"
mainPanel(___("histogram"), ___("barchart")),
inputPanel(sliderInput("binwidth",
label = "Bin width",
min = 0.1, max = 2,
step = 0.01, value=0.25),
checkboxGroupInput("days", "Choose types of days:",
# Replace the list elements with icons called "calendar", "briefcase" and "gift"
choiceNames = list("All days",
"Non-holiday weekdays",
"Weekend days/holidays"),
choiceValues = list("All days",
"Nonholiday weekdays",
"Weekend days and holidays"))),
"In general, across the different age groups, Americans seem to get adequate daily rest." ))
server <- function(input, output, session) {
# Define the histogram and barchart
output$histogram <- ___({
ggplot(sleep, aes(x=`Avg hrs per day sleeping`)) +
geom_histogram(binwidth = input$binwidth, col='white') +
theme_classic()
})
___ <- ___({
filter(sleep, `Type of Days` %in% input$days) %>%
group_by(`Type of Days`, `Age Group`) %>%
summarize(`Median hours` = median(`Avg hrs per day sleeping`)) %>%
ggplot(aes(x = `Median hours`, y = `Age Group`, fill = `Type of Days`)) +
geom_col(position = 'dodge') + theme_classic()
})
}
# Use shinyApp() to render the shinyApp
___