使用您自己的文本
当您希望用户输入比普通 textInput() 更长的文本时,textAreaInput() 会非常有用。文本区域可以跨越多行,带有垂直滚动条,并提供 rows 参数来控制可见行数。
除了体积更大之外,文本区域输入在其他方面与文本输入的行为非常相似。
本练习是课程的一部分
案例研究:使用 R 的 Shiny 构建 Web 应用
练习说明
您的任务是添加一个文本区域输入,让用户用自己的文字来生成词云。具体要求:
- 添加一个包含 7 行的文本区域输入,
inputId为 "text",label为 "Enter text"。 - 使用该文本区域的取值作为词云的
data数据源,而不是使用《孙子兵法》文本(第 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)