I am trying to join text search in R with Shiny . However I am getting no output in Shiny . Not able to figure out the reason here . Maybe i wrote the wrong function integrating with Shiny ?
search.txt content <- “Well, Prince, so Genoa and Lucca are now just family estates of the Buonapartes. But I warn you, if you don’t tell me that this means war, if you still try to defend the infamies and horrors perpetrated by that Antichrist—I really believe he is Antichrist—I will have nothing more to do with you and you are no longer my friend, no longer my ‘faithful slave,’ as you call yourself! But how do you do? I see I have frightened you—sit down and tell me all the news.”
It was in July, 1805, and the speaker was the well-known Anna Pávlovna Schérer, maid of honor and favorite of the Empress Márya Fëdorovna. With these words she greeted Prince Vasíli Kurágin, a man of high rank and importance, who was the first to arrive at her reception. Anna Pávlovna had had a cough for some days. She was, as she said, suffering from la grippe; grippe being then a new word in St. Petersburg, used only by the elite... " (literature text file)
library(quanteda)
library(shiny)
library(tm)
library(tidytext)
library(tidyverse)
search_file <- readLines("search.txt")
search_corpus <- corpus(search_file)
sentences <- tokens(search_corpus,what="sentence")
z <- grep("good",sentences,value=TRUE) #working example
head(z)
make_sentences <- function(word, n = 10, use.seed = FALSE) {
sapply(seq_len(n), function(i) {
if (use.seed) set.seed(i)
grep(word,sentences,value=TRUE)
})
}
make_sentences("happy ") # seems to be working here in Rstudio
ui<- shinyUI(fluidPage(
# Application title
mainPanel(
img(src='image.jpg', align = "right"),
fluidRow(HTML("<strong> Search Bar")),
fluidRow(
br(),
p("User Name : ")),
br(),
br(),
fluidRow(HTML("<strong>Enter a word.Click \"Next words\" after</strong>") ),
fluidRow( p("\n") ),
# Sidebar layout
sidebarLayout(
sidebarPanel(
textInput("inputString","Enter a word here",value = " "),
submitButton("Next words")
),
mainPanel(
h4("Are you looking for this ?"),
tags$style("#mytext {white-space: pre-line;}"),
verbatimTextOutput("link")
)
)
)))
server <- function(input, output, session) {
output$mytext <- renderPrint({
sentences <- rep(make_sentences(input$inputString))
length(sentences)
cat(paste0(1:length(sentences)," ",sentences,sep= '\n'))
})
}
shinyApp(ui,server)