我是Shiny的新手,所以我遇到了ui和服务器如何互相交流的麻烦 . 我想创建一个带有动态过滤器参数的 ggplot2
条形图,因此我可以在 textInput
小部件中输入一个单词,因此条形图会发生变化 .
数据:
我正在处理来自300个文档的50,000个单词,这就是为什么我需要textInput但是这里有一些示例数据:
library(tidyverse)
library(shiny)
example_df <- tibble::tribble(
~doc, ~word, ~n,
"A", "sustainable", 5L,
"A", "migrants", 2L,
"A", "building", 4L,
"B", "sustainable", 2L,
"B", "together", 1L,
"B", "building", 5L
)
UI:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput(inputId = "word",
label = "Word:",
value = "sustainable"),
),
mainPanel(
plotOutput(outputId = "barchart")
)
)
)
使用此代码我已经遇到了一个我不明白的错误:
Error in tag("form", list(...)) : argument is missing, with no default
服务器:
server <- function(input, output) {
output$barchart <- renderPlot({
example_df %>%
filter(word == input) %>%
arrange(desc(n)) %>%
head(20) %>%
ggplot(aes(x = reorder(doc, n),
y = n)) +
geom_col() +
theme_minimal()
})
}
我知道这个闪亮的代码可能很疯狂,但所有帮助都非常感谢!
1 回答
你有两个小错误:
首先:
你只需要一个额外的尾随逗号,这是不必要的,这就是你得到错误的原因 .
其次:
您没有指定要使用的输入 . 正确的语法是
filter(word == input$word)
,其中'word'是您的textInput的id .完整的更正代码: