Subaru Spirit
Subaru Spirit

Reputation: 474

R Shiny how to show status messages from the console (pdf_ocr_text)

When I use pdf_ocr_text from pdftools for example:text1 <- pdf_ocr_text("0.pdf", dpi = 300), it will show the status in the R console like below.

Converting page 1 to 0_1.png... done!
Converting page 2 to 0_2.png... done!
Converting page 3 to 0_3.png... done!
Converting page 4 to 0_4.png... done!

But how can I show this status when I use this in Shiny app? Because I want the user to see it's being processed rather than nothing is showing when they click the button (it can take a while for this to finish)?

Reproducible codes below, you can import any pdf files in there, but you will need to create a folder that's called www which should be in the same folder of your R file. Also run the app in external browser, otherwise don't work well.

library(tidyverse)
library(shiny)
library(pdftools)
library(tesseract)
library(tidytext)
library(reactable)
library(shinyFeedback)
library(shinyjs)
library(shinyalert)


ui <- shinyUI(fluidPage(
  useShinyjs(),
  useShinyalert(),
  shinyFeedback::useShinyFeedback(),
  
  sidebarLayout(
    sidebarPanel(
      titlePanel("Demo"),
      fileInput("file_import", "Upload Files ( . pdf format only)",
                multiple = T, accept = ".pdf"),
      disabled(actionButton("ocr_button","OCR (click this when nothing shows up)",
                            class = "btn-danger",
                            icon=icon("fa-sharp fa-solid fa-triangle-exclamation",
                                      lib = "font-awesome"))),
      textOutput("sometext"),
      tableOutput("files")
    ),
    
    mainPanel(
      uiOutput("pdfview"),
      reactableOutput("test")
    )
  )
))


server <- function(input, output, session) {
  ### display the pdf ########################################################
  x = reactiveVal(1)
  
  observeEvent(input$file_import,{
    enable("ocr_button")
    file.rename(input$file_import$datapath[x()], "0.pdf")
    file.copy("0.pdf","www", overwrite = T)
    
    output$pdfview <- renderUI({
      tags$iframe(style="height:1200px; width:100%", src="0.pdf")
    })
  })
  
  observeEvent(input$ocr_button, {
    ### OCR ###########################################################
    text1 <- reactive({pdf_ocr_text("0.pdf", dpi = 300)})
    ######################################################################
    output$sometext = renderText({
      text1()
    })
  })
}

shinyApp(ui, server)

Upvotes: 2

Views: 154

Answers (0)

Related Questions