एक महाद्वीप सेलेक्टर जोड़ें: select input
जब चुनने के लिए बहुत सारे विकल्प हों, तो रेडियो बटन काफी जगह घेर सकते हैं और हमेशा उपयुक्त नहीं होते. Select input—जिसे 'dropdown list' भी कहते हैं—का उपयोग भी यूज़र से सूची में से विकल्प चुनवाने के लिए किया जा सकता है, लेकिन अधिक कॉम्पैक्ट तरीके से. Select input में सभी विकल्प एक स्क्रॉलेबल सूची में दिखते हैं, इसलिए बहुत सारे विकल्प होने पर भी यह काम करता है.
रेडियो बटनों की तरह, select input में भी choices और selected पैरामीटर होते हैं. इसके अलावा, select input में multiple आर्ग्युमेंट होता है, जो TRUE होने पर यूज़र को एक से अधिक मान चुनने देता है.
पिछले अभ्यास वाले Shiny ऐप का कोड यहाँ थोड़े बदलावों के साथ दिया गया है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना
अभ्यास निर्देश
- UI में ID "continents" और लेबल "Continents" के साथ एक
selectInput()जोड़ें, जिसमें डिफ़ॉल्ट महाद्वीप "Europe" सेट हो.- सूची में दिए जाने वाले विकल्प gapminder डेटासेट में मौजूद सभी अलग-अलग महाद्वीप होने चाहिए.
- यूज़र को एक साथ एक से अधिक महाद्वीप चुनने की अनुमति दें.
- सर्वर में ऐसा कोड जोड़ें कि केवल चुने गए महाद्वीपों का डेटा ही दिखे, इसके लिए gapminder डेटासेट को subset करें (लाइन 23).
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("title", "Title", "GDP vs life exp"),
numericInput("size", "Point size", 1, 1),
checkboxInput("fit", "Add line of best fit", FALSE),
radioButtons("color", "Point color",
choices = c("blue", "red", "green", "black")),
# Add a continent dropdown selector
___(___, ___,
choices = levels(___),
multiple = ___,
selected = ___)
),
mainPanel(
plotOutput("plot")
)
)
)
# Define the server logic
server <- function(input, output) {
output$plot <- renderPlot({
# Subset the gapminder dataset by the chosen continents
data <- subset(gapminder,
___ %in% ___$continents)
p <- ggplot(data, aes(gdpPercap, lifeExp)) +
geom_point(size = input$size, col = input$color) +
scale_x_log10() +
ggtitle(input$title)
if (input$fit) {
p <- p + geom_smooth(method = "lm")
}
p
})
}
shinyApp(ui = ui, server = server)