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

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