How to join R code and Shiny together

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)

I think you could improve your code sharing practice in the following ways

  1. declare your library dependencies - dont assume people will know what libraries your rely on ...
  2. provide an example data, in this case search.txt is private to you

that aside I think you have made a simple error
your tag style and renderprint relate to mytext object, but the verbatimtextoutput you placed is called link.
change link to mytext and that aspect should work.

I do have my doubts about your grepping on sentence tokens, I dont think those are simply characters so unless there is a custom grep function, I'm suprised it worked.

Sure . thank you . I corrected it.On other hand I am getting this output in R studio :

text122
"They say old maids have a mania for matchmaking, and though I don't feel that weakness in myself as yet, I know a little person who is very unhappy with her father."
text133
"The poor girl is very unhappy."
text759
"said Pierre, as if suddenly struck by a happy thought, "seriously, I have long been thinking of it...."
text1050
"I wish you many happy returns of your name day," said the visitor."
text1244
"She took his arm and with a happy face went with him into the adjoining sitting room."
text1263
"Sónya and Natásha looked at Véra with guilty, happy faces."




how do you want me to respond to that ?

I thought you had doubts on using grep with tokens .So just wanted to show it works ?

ok then, thanks viky

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.