2

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)
Subaru Spirit
  • 394
  • 3
  • 19
  • Please add your code to the question. – Ed_Gravy Nov 17 '22 at 15:18
  • 1
    Added a reproducible code – Subaru Spirit Nov 17 '22 at 15:40
  • Those are likely messages. This question may [help](https://stackoverflow.com/questions/30474538/possible-to-show-console-messages-written-with-message-in-a-shiny-ui) – SmokeyShakers Nov 17 '22 at 20:55
  • @SmokeyShakers I looked at that one before raising this ticket but doesn't seem to help with this case unfortunately – Subaru Spirit Nov 18 '22 at 09:01
  • 2
    The text you see is coming from [this](https://github.com/ropensci/pdftools/blob/5020fed4f9cdadf6fd09264fa8a0d044cf0a047a/src/bindings.cpp#L421) C++ code, which displays as standard out in R, not a R typical exception (message, warning, error), so it is the same as you use `cat` method in R. This is still a challenging topic in Shiny to capture in real-time and send it to UI. So the short answer for now is, no, you can't. – lz100 Nov 20 '22 at 01:21
  • @lz100 thanks for looking through the source code, I suppose I can only reduce the dpi to make it run faster – Subaru Spirit Nov 21 '22 at 09:40

0 Answers0