टेबल को इंटरैक्टिव बनाएँ
Shiny ऐप में डेटा दिखाने के लिए DT पैकेज की Datatables अक्सर बिल्ट-इन टेबल्स से बेहतर होती हैं। Shiny टेबल्स को केवल दो छोटे कोड बदलावों से Datatables में बदला जा सकता है: tableOutput() और renderTable() की जगह आप DT::dataTableOutput() और DT::renderDataTable() का उपयोग करें। Datatables में कस्टमाइज़ेशन के कई विकल्प होते हैं, लेकिन हम यहाँ कोई विशेष विकल्प इस्तेमाल नहीं करेंगे.
ध्यान दें कि DT पैकेज के साथ प्रचलन यह है कि DT पैकेज को लोड नहीं किया जाता, बल्कि datatable फंक्शंस को कॉल करते समय DT:: प्रीफ़िक्स का उपयोग किया जाता है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना
अभ्यास निर्देश
पिछले कोडिंग अभ्यास वाले Shiny ऐप का कोड बिना किसी बदलाव के दिया गया है। आपका काम बेसिक Shiny टेबल को DT टेबल से बदलना है। विशेष रूप से:
- UI में, टेबल आउटपुट फंक्शन को
DTdatatable आउटपुट से बदलें (पंक्ति 11). - सर्वर में, टेबल रेंडरिंग फंक्शन को
DTdatatable रेंडर फंक्शन से बदलें (पंक्ति 31).
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
ui <- fluidPage(
h1("Gapminder"),
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"),
plotOutput("plot"),
# Replace the tableOutput() with DT's version
tableOutput("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
})
# Replace the renderTable() with DT's version
output$table <- renderTable({
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)