在完成 DataCamp 课程后,我正在尝试创建一个 wordcloud 闪亮的应用程序。在课程中,他们使用自定义create_wordcloud()
函数来制作应用程序。如果有人有它的代码,那会让我的生活更轻松。
无论如何,我正在尝试自己的方式,因为我没有自定义函数并且将使用wordcloud2()
函数。
我在使用响应式函数制作 Shiny 应用程序时遇到了麻烦。本质上,我试图让用户可以选择单词的数量并使用 UI 更改 wordcloud 中的背景。为此,我需要将用户提供的文本转换为数据框,按字数排序 df,然后将 df 子集化为用户在应用程序 UI 中选择的任何数字。
这是我的代码:
library(shiny)
library(colourpicker)
library(tidyverse)
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
# Add a textarea input
textAreaInput("text", "Enter text", rows = 7),
numericInput("num", "Maximum number of words", 25),
colourInput("col", "Background color", value = "white")
),
mainPanel(
wordcloud2Output("cloud")
)
)
)
server <- function(input, output) {
df_data <- reactive({
input$text %>%
term_stats(., drop_punct = TRUE, drop = stopwords_en) %>%
order_by(desc(count))
})
output$cloud <- renderWordcloud2({
# Use the textarea's value as the word cloud data source
wordcloud2(data = df_data()[1:input$num, ],
backgroundColor = input$col)
})
}
shinyApp(ui = ui, server = server)
我得到的错误是:
警告:错误:输入必须是向量,而不是函数。
我真的很期待听到社区的回答并提高我的反应式编程技能!
谢谢!