您有两个小错误:
首先:
sidebarPanel(
textInput(inputId = "word",
label = "Word:",
value = "sustainable"),
)
您只是有一个额外的尾随逗号,这是不必要的,这也是您得到错误的原因。
其次:
example_df %>%
filter(word == input)
您没有指定要使用的输入。正确的语法是
filter(word == input$word)
其中“word”是您的textInput的ID。
全部更正的代码:
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 <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput(inputId = "word",
label = "Word:",
value = "sustainable")
),
mainPanel(
plotOutput(outputId = "barchart")
)
)
)
server <- function(input, output) {
output$barchart <- renderPlot({
example_df %>%
filter(word == input$word) %>%
arrange(desc(n)) %>%
head(20) %>%
ggplot(aes(x = reorder(doc, n),
y = n)) +
geom_col() +
theme_minimal()
})
}
shinyApp(ui, server)