How to display `pdfsearch` output in Shiny in table format displaying columns of text , page number , line number?

I created a fairly straight forward pdfsearch search option with Shiny . However I want the output to be in the form of table for easy readability. I tried using Data Table package , but do not seem to get the right format I need or haven't figured out how exactly to use DT with dynamic text inputs

library(pdfsearch)
library(shiny)
library(DT)


file <- system.file('pdf', 'rhyme.pdf', package = 'pdfsearch')

word_sentence <-function(word) {keyword_search(file,    
                                               keyword = word,
                                               path = TRUE)}



retobj <- function(word) {
  x <- word_sentence(word)
  page <- x$page_num
  line <- x$line_num
  text <- x$line_text
  return(paste0(text," "," *Page no-",page," *line-",line))
}


ui<- shinyUI(fluidPage(dashboardBody(
  img(src='image.jpg', align = "left"),style = "padding-top:20px",
  

  br(),
  br(),
  br(),
  br(),
  
  fluidRow(
    column(width = 2,
           h5(HTML("<strong>Enter a word.Click \"SEARCH\" </strong>")),
           wellPanel(
             textInput("inputString","Enter a word here",value=" "),
             submitButton("SEARCH"),
             
           )),
    
    column(width= 7,style = "max-height: 90vh; overflow-y: auto;",
           h4("Search Results"), 
           wellPanel(            
             tags$style("#mytext { white-space: pre-line; }"),
             textOutput("mytext")
           ))
         )
       )
     ) 
    ) 


server <- function(input, output, session) {
  output$mytext <- renderPrint({
    sentences <- retobj(input$inputString)
    length(sentences)
    cat(paste0(1:length(sentences)," - ",sentences,sep= '\n'))
  })
}


shinyApp(ui,server)

I am now getting the output when i search the word 'hill' for eg. as :

1.jack went to the hill blah blah *Page no- 12 * line no -34
2.jack went rolling on hill blah  *Page no 17 *line no - 56
3.jill climbed the hill blah blah * Page no 34  * line no -89
''''
20......

I would like the output to be in table format displayed in Shiny such as :

  Text                                           Page no       Line No 
    
      jack went to the hill blah                   12            34
    
      jack went rolling on hill blah               17            56
     
      jill climbed the hill   blah                 34            89
     
      '    ''''''''''

using renderDataTablein server part and tableOutput in ui of Shiny gives me output in R console instead of Shiny and I tried
retob <- function({.....tableHTML(text,page,line)} but it was giving me error as says not matrix format.Is there a cleaner way to just format the content in the above mentioned way ?

Your data needs to be in a data frame format in order to display it in a table. You can try the following:

library(pdfsearch)
library(shiny)
library(DT)

#I used the example pdf that comes with the package. You can replace it with your own
file <- system.file('pdf', '1610.00147.pdf', package = 'pdfsearch')

#UI
ui <- shinyUI(fluidPage(
  dashboardBody(
    img(src = 'image.jpg', align = "left"),
    style = "padding-top:20px",
    br(),
    br(),
    br(),
    br(),
    fluidRow(
      column(width = 2,
             h5(
               HTML("<strong>Enter a word.Click \"SEARCH\" </strong>")
             ),
             wellPanel(
               textInput("inputString", "Enter a word here", value = " "),
               submitButton("SEARCH"),
             )),
      column(
        width = 7,
        style = "max-height: 90vh; overflow-y: auto;",
        h4("Search Results"),
        wellPanel(
          tags$style("#mytext { white-space: pre-line; }"),
          dataTableOutput("mytext")
        )
      )
    )
  )
))

#Server
server <- function(input, output, session) {
  
  #To be responsive to your input it needs to be in a reactive context
  data <- reactive({

  #Perform the search 
  word_sentence <- keyword_search(file,
                                  keyword = input$inputString,
                                   path = TRUE)
  
  #make a data frame with the columns you want to display in the table
  data <- data.frame(
    text = unlist(word_sentence$line_text), #Unlist 
    page = word_sentence$page_num,
    line = word_sentence$line_num,
    stringsAsFactors = FALSE
  )
  
  #change column names
  names(data) <- c("Text", "Page No", "Line No")

  return(data) 
    
  }) 
  

  output$mytext <- DT::renderDataTable({
  
  table <- DT::datatable(data(),
      rownames = FALSE,
      options = list(searching = TRUE, lengthChange = TRUE
      )) 
  })
}


shinyApp(ui,server)

You can customize the table the way you want. Have a look here:

https://rstudio.github.io/DT/options.html

1 Like

cool ! works smoothly . any way I can remove the input$inputString search bar and have only DT search bar since DT gives a search bar by default ? I can remove DT search bar but I want to keep it as I could get highlighting and other options with and remove inputString one but giving me no output at all when I remove it ? any chance you know a way around ?

if you remove input$inputString there is no keyword input and you will get an error. You could replace input$inputString with for example "" to get all hits:

word_sentence <- keyword_search(file,
                                  keyword = "",
                                   path = TRUE)
1 Like

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