शुरू करेंमुफ़्त में शुरू करें

ज़रूरत पड़ने पर नया word cloud बनाएँ

आपने word cloud के रेंडर कोड को isolate करके उसे बार-बार अपडेट होने से रोक दिया है. अब आख़िरी चरण यह है कि word cloud तभी रेंडर हो जब उपयोगकर्ता चाहे. इसे actionButton() की मदद से किया जा सकता है.

यह अभ्यास पाठ्यक्रम का हिस्सा है

केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना

पाठ्यक्रम देखें

अभ्यास निर्देश

आपका काम Shiny ऐप में एक बटन जोड़ना है, और बटन दबाए जाने पर word cloud को दोबारा रेंडर कराना है. विशेष रूप से:

  • ऐप में एक action button जोड़ें, जिसका input ID "draw" और लेबल "Draw!" हो (पंक्ति 26).
  • word cloud रेंडरिंग फंक्शन में उस बटन को डिपेंडेंसी के रूप में जोड़ें ताकि बटन दबते ही word cloud फिर से रन हो (पंक्ति 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"),
      # Add a "draw" button to the app
      ___(inputId = ___, label = ___)
    ),
    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({
    # Add the draw button as a dependency to
    # cause the word cloud to re-render on click
    input$___
    isolate({
      create_wordcloud(data_source(), num_words = input$num,
                       background = input$col)
    })
  })
}

shinyApp(ui = ui, server = server)
कोड संपादित करें और चलाएँ