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

ज़रूरत के हिसाब से इनपुट्स दिखाएँ या छिपाएँ

अब इस word cloud ऐप में शब्द देने के तीन तरीके हैं. इनमें से दो तरीकों के साथ एक खास UI एलिमेंट जुड़ा है जो सिर्फ उन्हीं में काम आता है: जब यूज़र "own" word source चुनता है तो केवल उसी समय इस्तेमाल होने वाला एक textarea है, और जब यूज़र "file" source चुनता है तो सिर्फ उसी से संबंधित एक file input है. आदर्श रूप से, किसी भी समय पर केवल वही इनपुट्स दिखने चाहिए जिनकी ज़रूरत हो.

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

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

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

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

textarea पहले से conditionalPanel() में लिपटा हुआ है ताकि वह तभी दिखे जब यूज़र अपना खुद का टेक्स्ट इनपुट करना चुने. आपका काम है file input को भी शर्त के आधार पर केवल तब दिखाना जब यूज़र data source के रूप में file upload चुने. विशेष रूप से:

  • line 19 पर file input को एक conditional panel में लपेटें.
  • panel की condition तब पूरी होनी चाहिए जब यूज़र data source के radio buttons में "file" विकल्प चुने (line 22).

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

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)
      ),
      # Wrap the file input in a conditional panel
      ___(
        # The condition should be that the user selects
        # "file" from the radio buttons
        condition = ___,
        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({
    create_wordcloud(data_source(), num_words = input$num,
                        background = input$col)
  })
}

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