词云 Shiny 应用
已为您提供名为 artofwar 的示例数据集,包含整本《孙子兵法》的文本。您可以运行 head(artofwar) 或 tail(artofwar) 来查看该文本的前几段和最后几段。
正如视频中所述,词云是一种此前未见过的输出,因此需要一组新的输出与渲染函数:wordcloud2Output() 和 renderWordcloud2()。这些输出函数来自 wordcloud2 包。
本练习是课程的一部分
案例研究:使用 R 的 Shiny 构建 Web 应用
练习说明
函数 create_wordcloud()、数据集 artofwar,以及所有必要的包都已在您的工作区中可用。
- 在 UI 中为词云输出添加一个占位符,输出 ID 为
cloud。 - 渲染词云对象,并将其赋值到
output列表中的正确项(第 11 行)。
顺便说一下,create_wordcloud() 是我们为您预先定义的函数。您无法在自己电脑上的 RStudio 中运行它。若想查看其内容,请在控制台输入 create_wordcloud。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define UI for the application
ui <- fluidPage(
h1("Word Cloud"),
# Add the word cloud output placeholder to the UI
___(outputId = "cloud")
)
# Define the server logic
server <- function(input, output) {
# Render the word cloud and assign it to the output list
output$___ <- ___({
# Create a word cloud object
create_wordcloud(artofwar)
})
}
# Run the application
shinyApp(ui = ui, server = server)