डेटा सोर्स चुनें (ui)
पिछले कुछ अभ्यासों में, आपने वर्ड क्लाउड के लिए 3 अलग-अलग सोर्स इस्तेमाल किए: Art of War किताब, एक टेक्स्ट फील्ड, और एक टेक्स्ट फ़ाइल. लेकिन, किसी भी समय केवल एक ही सोर्स काम कर रहा था. इस अभ्यास में, आप यूज़र को यह चुनने का तरीका देंगे कि वर्ड क्लाउड के लिए कौन-सा डेटा सोर्स इस्तेमाल करना है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना
अभ्यास निर्देश
आपका काम ऐप में ऐसे रेडियो बटन जोड़ना है, जो यूज़र को चुनने दें कि शब्दों का सोर्स Art of War किताब हो, textarea हो, या अपलोड की गई फ़ाइल. विशेष रूप से:
- "Word source" लेबल के साथ एक रेडियो बटन इनपुट जोड़ें, जिसमें तीन विकल्प हों:
- विकल्पों के मान क्रमशः "book", "own", और "file" होने चाहिए. यूज़र को दिखने वाले नाम क्रमशः "Art of War", "Use your own words", और "Upload a file" होने चाहिए.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
# Add radio buttons input
___(
inputId = "source",
label = ___,
choices = c(
# First choice is "book", with "Art of War" displaying
"Art of War" = "book",
# Second choice is "own", with "Use your own words" displaying
___ = "own",
# Third choice is "file", with "Upload a file" displaying
___ = ___
)
),
textAreaInput("text", "Enter text", rows = 7),
fileInput("file", "Select a file"),
numericInput("num", "Maximum number of words",
value = 100, min = 5),
colourInput("col", "Background color", value = "white")
),
mainPanel(
wordcloud2Output("cloud")
)
)
)
server <- function(input, output) {
input_file <- reactive({
if (is.null(input$file)) {
return("")
}
readLines(input$file$datapath)
})
output$cloud <- renderWordcloud2({
create_wordcloud(input_file(), num_words = input$num,
background = input$col)
})
}
shinyApp(ui = ui, server = server)