我正在尝试创建一个使用 csv 文件并从中提取特定信息的闪亮应用。当我加载一个文件并运行我的代码就可以了。但是现在,我希望用户能够选择要分析的CSV文件,因为每个文件都包含不同的书籍。这是我到目前为止的代码
library("shiny")
library("tidyverse")
library("stringr")
## Define UI
ui <- fluidPage(
titlePanel("Find words in the text"),
## Sidebar panel
sidebarLayout(
sidebarPanel(
## Textual input
selectInput("source", "Choose Text",
choices = c("Book 1" = 1,
"Book 2" = 2),
selected = "Book 1"),
textInput("word", "Insert words", value = ""),
helpText("Type the word or words (separated by comma)\n
that you would like to find"),
submitButton(text = "Search", icon("refresh")),
br(),
textOutput("text")
),
## Main panel for outputs
mainPanel(
tabsetPanel(
tabPanel("Main",
tableOutput("view")),
tabPanel("Summary",
tableOutput("summary"))
)
)
)
)
server <- function(input, output) {
k <- reactive({
k <- ifelse(input$source == 1,
read_delim("book1.csv", delim = ";"),
read_delim("book2.csv", delim = ";"))
})
output$view <- renderTable({
k %>%
filter(str_detect(text,
str_split(input$word, ",")[[1]]))
})
output$text <- renderText({
palabras <- str_split(input$word, ",")[[1]]
n.times <- select(k, text) %>%
str_count(c(palabras))
paste("The word ", c(palabras),
" appeared: ", n.times, " times")
})
output$summary <- renderTable({
p <- str_replace_all(input$word, ",", "|")
k %>%
extract(col = text,
into = "Keyword",
regex = str_c("(",p, ")"),
remove = T, convert = T) %>%
na.omit()
})
}
## Create Shiny app
shinyApp(ui = ui, server = server)
我还尝试将以k <- reactive({
开头的部分(其中k
是我想要拥有本书的小标题)更改为首先加载每本书然后将其发送的内容像这样k
book1 <- read_delim("book1.csv", delim = ";")
book2 <- read_delim("book2.csv", delim = ";")
k <- reactive ({
k <- ifelse(input$source == 1,
book1,
nook2)
})
我已经尝试了几天使用教程和stackoverflow,但是我找不到真正适合我的东西。我尝试了几种不同的方法,但是显然reactive
函数做错了什么。如果有人可以帮助我解决它并解释我的错误,我将非常感谢,因为这是我的第一个Shiny应用程序。
仅用于记录:如果我删除与reactive
部分相关的所有内容,而仅使用k
之类的文件将文件加载到k <- read_delim("book1.csv", delim = ";")
中,则我的应用非常适合单词计数。