लगातार नए word clouds न बनाएँ
वर्ड क्लाउड ऐप में अब कई अलग-अलग इनपुट हैं, और इनमें से किसी भी इनपुट को बदलने पर वर्ड क्लाउड नए पैरामीटर्स के साथ दोबारा ड्रॉ हो जाता है, जैसा अपेक्षित है.
लेकिन यह व्यवहार कभी-कभी परेशान भी कर सकता है. उदाहरण के लिए, जब आप textarea में टेक्स्ट टाइप कर रहे होते हैं, तो वर्ड क्लाउड आपके टाइपिंग पूरी होने का इंतज़ार किए बिना बार-बार बनता रहता है. इसे isolate() से नियंत्रित किया जा सकता है.
renderWordcloud2() के अंदर वर्ड क्लाउड रेंडर करने वाला सारा कोड हटा दिया गया है. आपका कार्य है वर्ड क्लाउड को फिर से बनाना और उसे isolate करना ताकि पैरामीटर्स बदलने पर अपने-आप नया वर्ड क्लाउड ट्रिगर न हो.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना
अभ्यास निर्देश
- सुनिश्चित करें कि पूरा वर्ड क्लाउड जनरेट करने वाला फंक्शन isolate किया गया है (पंक्ति 54).
create_wordcloud()को आवश्यक इनपुट्स और रिएक्टिव वैरिएबल्स से आर्ग्युमेंट्स दें. इस फंक्शन के आर्ग्युमेंट्स हैंdata,num_words, औरbackground(पंक्ति 56).
इसका नतीजा शुरुआत में ऐप टूटा हुआ लग सकता है, क्योंकि आप नया वर्ड क्लाउड नहीं बना पाएँगे, लेकिन इसे अगले अभ्यास में ठीक किया जाएगा.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
radioButtons(
inputId = "source",
label = "Word source",
choices = c(
"Art of War" = "book",
"Use your own words" = "own",
"Upload a file" = "file"
)
),
conditionalPanel(
condition = "input.source == 'own'",
textAreaInput("text", "Enter text", rows = 7)
),
conditionalPanel(
condition = "input.source == 'file'",
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) {
data_source <- reactive({
if (input$source == "book") {
data <- artofwar
} else if (input$source == "own") {
data <- input$text
} else if (input$source == "file") {
data <- input_file()
}
return(data)
})
input_file <- reactive({
if (is.null(input$file)) {
return("")
}
readLines(input$file$datapath)
})
output$cloud <- renderWordcloud2({
# Isolate the code to render the word cloud so that it will
# not automatically re-render on every parameter change
___({
# Render the word cloud using inputs and reactives
create_wordcloud(data = ___, num_words = ___,
background = ___)
})
})
}
shinyApp(ui = ui, server = server)