How to loop through to output all possible sentences from word search ? I am getting just the first sentence

I created a text search whereby searching for a word, displays all the relevant sentences having that word in them. I want to create a loop which displays all possible sentences from search in Shiny.However I am getting just first sentence of the output

library(quanteda)
library(shiny)
library(tidyverse)
library(htmltools)
library(shinythemes)


war <- readLines("war.txt")


war_corpus <- corpus(war)


sentences <- tokens(war_corpus,what="sentence")


make_sentences <- function(word) {
  grep(word,sentences,value= TRUE)}


sentence_line <- function(word) {
  grep(word,sentences,value= FALSE)}



ui<- shinyUI(fluidPage(
  
  
  # Application title
  mainPanel(
    img(src='image.jpg', align = "right"),
    
    br(),
    br(),
    br(),
    
    fluidRow(
      column(2,
             h5(HTML("<strong>Enter a word.Click \"SEARCH\" </strong>")),
             wellPanel(
               textInput("inputString","Enter a word here",value=" "),
               submitButton("Search"),
             )),
      
      column(5,
             h4("Search Results"), 
             wellPanel(                       
               tags$style("#mytext { white-space: pre-line; }"),
               htmlOutput("mytext")
             )),
      
      column(5, offset = 1.5,
             h6("Uploaded File"),
             wellPanel(
               htmlOutput("showfile"))
      )
    )
    
  ),#Mainpanel
  
)#fluidpage
)#shinyUi



server <- function(input, output, session) {
  output$mytext <- renderUI({
    
    res <- make_sentences(input$inputString)[1]
    res1<- sentence_line(input$inputString)[1]
    tagList(
      tags$a(href=paste('#',res1,sep=""),res1),tags$div(res)
    )
    
  })
  output$showfile <- renderText({
    includeHTML("www/final_tokens.html")
  })
}

As you can see in the screenshot, for the word 'good' i am getting only the first sentence while actually I should be getting all sentences with word 'good' from txt file.(27 sentences in this case)

materials to study that relate to iteration/looping

1 Like

How about using purrr::map2 and removing the [1] index in your function calls? Check this out:

server <- function(input, output, session) {
  output$mytext <- renderUI({
    
    res <- make_sentences(input$inputString)
    res1<- sentence_line(input$inputString)
    
    purrr::map2(.x = res, .y = res1, .f = ~ 
                  tagList(
                    tags$a(href=paste('#',.y,sep=""),.y),tags$div(.x)
                  )
    )
    
  })
  output$showfile <- renderText({
    includeHTML("www/final_tokens.html")
  })
}
1 Like

perfect ! i had used :

lapply(1:m, function(i){
    res <- make_sentences(input$inputString)[i]
    res1<- sentence_line(input$inputString)[i]
    
    tagList(
        tags$a(href=paste('#',res1,sep=""),res1),tags$div(res))

above worked for me too but gives me almost 50 instances .so if a word has 10 instances ,it gives out 10 sentences just like I wanted but also gives NA after for rest 40 lines . yours is much more effective . thanks

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