r - 如何使用反应函数在 R Shiny 中创建 DF
问题描述
在完成 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)
我得到的错误是:
警告:错误:输入必须是向量,而不是函数。
我真的很期待听到社区的回答并提高我的反应式编程技能!
谢谢!
解决方案
方案一:获取实际代码create_wordcloud
功能如下:
create_wordcloud <- function(data, num_words = 100, background = "white") {
# If text is provided, convert it to a dataframe of word frequencies
if (is.character(data)) {
corpus <- Corpus(VectorSource(data))
corpus <- tm_map(corpus, tolower)
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removeWords, stopwords("english"))
tdm <- as.matrix(TermDocumentMatrix(corpus))
data <- sort(rowSums(tdm), decreasing = TRUE)
data <- data.frame(word = names(data), freq = as.numeric(data))
}
# Make sure a proper num_words is provided
if (!is.numeric(num_words) || num_words < 3) {
num_words <- 3
}
# Grab the top n most common words
data <- head(data, n = num_words)
if (nrow(data) == 0) {
return(NULL)
}
wordcloud2(data, backgroundColor = background)
}
解决方案2:@Xiang 在评论部分提供:
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) %>%
arrange(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)
推荐阅读
- angular - Angular:FormControl valueChanges Observable 触发所有更改
- google-apps-script - 所有函数同时执行
- python - 使用 ctypes 将音频数据从 Python 传递到 C
- reactjs - 在 ag-grid 中渲染隐藏的自定义元素
- vuejs2 - veutify - 默认情况下如何标记所有字段是必需的
- wordpress - 使用 Apache 对不在 URI 白名单中的每个请求使用 HTTP 404 响应
- linux - Yocto 为静态库构建失败并出现错误“未找到匹配”
- c# - 如何在没有模型绑定的视图中动态添加元素?
- css - 为什么设置了css max-width,但div仍然展开?
- ruby-on-rails - 排除使用friendly_id?