使用你自己的文字
當你想讓使用者輸入比一般 textInput() 更長的文字時,textAreaInput() 就很實用。文字區域會跨越多列,帶有垂直捲軸,並提供 rows 參數用來設定可見的列數。
除了體積更大之外,文字區域輸入在其他各方面的行為都和文字輸入非常相似。
本練習屬於課程
案例研究:使用 R 的 Shiny 建置網頁應用程式
練習說明
你的任務是加入一個文字區域輸入,讓使用者用自己的文字來產生文字雲。具體來說:
- 新增一個包含 7 列、
inputId為 "text"、標籤為 "Enter text" 的文字區域輸入。 - 使用該文字區域的值作為文字雲的
data來源,而不是使用artofwar書籍(第 20 行)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
# Add a textarea input
___,
numericInput("num", "Maximum number of words",
value = 100, min = 5),
colourInput("col", "Background color", value = "white")
),
mainPanel(
wordcloud2Output("cloud")
)
)
)
server <- function(input, output) {
output$cloud <- renderWordcloud2({
# Use the textarea's value as the word cloud data source
create_wordcloud(data = ___, num_words = input$num,
background = input$col)
})
}
shinyApp(ui = ui, server = server)