अलग-अलग आउटपुट अलग-अलग टैब पर रखें
जब सामग्री बहुत ज़्यादा हो और उसे हिस्सों में बाँटना हो, तब टैब उपयोगी रहते हैं। टैब बनाने के लिए, UI एलिमेंट्स को tabPanel() फंक्शन में लपेटें, और title आर्ग्युमेंट के ज़रिए टैब का शीर्षक दें।
UI में टैब दिखाने के लिए, सभी टैब पैनल्स को एक टैबसेट "कंटेनर" में समूहित करना होता है — यानी सभी टैब पैनल्स को tabsetPanel() के अंदर रखें।
आपका काम Shiny ऐप में टैब जोड़ना है, ताकि इनपुट्स और डाउनलोड बटन एक टैब में हों, प्लॉट दूसरे टैब में, और टेबल तीसरे टैब में। चूँकि यह केवल विज़ुअल बदलाव है, इसलिए सभी कोड बदलाव सिर्फ UI हिस्से में ही करने हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना
अभ्यास निर्देश
- तीन टैब पैनल्स के लिए कंटेनर बनाने हेतु
tabsetPanel()फंक्शन का उपयोग करें:- पहला टैब इनपुट्स के लिए हो, और टैब का नाम "Inputs" रखें।
- दूसरा टैब प्लॉट दिखाए, और टैब का नाम "Plot" रखें (लाइन 16)।
- तीसरा टैब टेबल दिखाए, और टैब का नाम "Table" रखें (लाइन 21)।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
ui <- fluidPage(
h1("Gapminder"),
# Create a container for tab panels
___(
# Create an "Inputs" tab
tabPanel(
title = ___,
sliderInput(inputId = "life", label = "Life expectancy",
min = 0, max = 120,
value = c(30, 50)),
selectInput("continent", "Continent",
choices = c("All", levels(gapminder$continent))),
downloadButton("download_data")
),
# Create a "Plot" tab
___(
title = "Plot",
plotOutput("plot")
),
# Create "Table" tab
___(
title = ___,
DT::dataTableOutput("table")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
data <- gapminder
data <- subset(
data,
lifeExp >= input$life[1] & lifeExp <= input$life[2]
)
if (input$continent != "All") {
data <- subset(
data,
continent == input$continent
)
}
data
})
output$table <- DT::renderDataTable({
data <- filtered_data()
data
})
output$download_data <- downloadHandler(
filename = "gapminder_data.csv",
content = function(file) {
data <- filtered_data()
write.csv(data, file, row.names = FALSE)
}
)
output$plot <- renderPlot({
data <- filtered_data()
ggplot(data, aes(gdpPercap, lifeExp)) +
geom_point() +
scale_x_log10()
})
}
shinyApp(ui, server)