stringdist not working

I am developing and application which takes two documents takes their text and matches the text. But not able to display. How to overcome this?

Here is my reprex-

library(shiny)
library(RecordLinkage)
library(tidyverse)
library(dplyr)
library(shinydashboard)
library(tesseract)
library(magick)
library(stringdist)


ui<- fluidPage(
  fluidRow(
    column(4,
           fileInput("image1", "Upload Image 1")),
    column(4,
           fileInput("image2", "Upload Image 2")),
    column(8,
           actionButton("submit", "Submit"), 
           actionButton("compare", "Analyze"))
  ),
  fluidRow(column(12,
                  box(width = 12,
                      uiOutput("text"),
                      title = "Text Output 1"
                  )
  )),
  fluidRow(column(12,
                  box(width = 12,
                      uiOutput("text1"),
                      title = "Text Output 2"
                  )
  )),
  fluidRow(column(12,
                  box(width = 12,
                      uiOutput("analysis"),
                      title = "Analysis"
                  )
  ))
  
)
server<- function(input, output, session)
{
  
  
  observeEvent(input$submit, {
    req(input$image1)
    output$text<- renderUI({
     t1<- c(image_ocr(image_read(input$image1, path = input$image1$datapath)))
    t<- as.character(t1)
     return(t1)
     })
  })
  observeEvent(input$submit, {
    req(input$image2)
    output$text1<- renderUI({
      t2<-c(image_ocr(image_read(input$image2, path = input$image2$datapath)))
      t2<-as.character(t2)
    return(t2)
      })
  })
 
  a<- reactive({
   c(input$text)
  })
  
  b<- reactive({
    c(input$text1)
  })
  
  observe( {
    output$analysis<- renderUI({
      d<- stringdist(a(),b() ,method = "cosine")
      return(d)
    }) 
  })
}

shinyApp(ui,server)

I dont see any UI inside your renderUI
https://shiny.rstudio.com/reference/shiny/latest/renderUI.html

also, its unusual to wrap that in observe...

Well, I would like to have the output as a Text. I can have textInput for the same. But how to print the result?

new code-

library(shiny)
library(RecordLinkage)
library(tidyverse)
library(dplyr)
library(shinydashboard)
library(tesseract)
library(magick)
library(stringdist)

ui<- fluidPage(
fluidRow(
column(4,
fileInput("image1", "Upload Image 1")),
column(4,
fileInput("image2", "Upload Image 2")),
column(8,
actionButton("submit", "Submit"),
actionButton("compare", "Analyze"))
),
fluidRow(column(12,
box(width = 12,
uiOutput("text"),
title = "Text Output 1"
)
)),
fluidRow(column(12,
box(width = 12,
uiOutput("text1"),
title = "Text Output 2"
)
)),
fluidRow(column(12,
box(width = 12,
uiOutput("analysis"),
title = "Analysis"
)
))

)
server<- function(input, output, session)
{

observeEvent(input$submit, {
req(input$image1)
output$text<- renderUI({
t1<- c(image_ocr(image_read(input$image1, path = input$image1$datapath)))
t<- as.character(t1)
return(t1)
})
})
observeEvent(input$submit, {
req(input$image2)
output$text1<- renderUI({
t2<-c(image_ocr(image_read(input$image2, path = input$image2$datapath)))
t2<-as.character(t2)
return(t2)
})
})

a<- reactive({
c(input$text)
})

b<- reactive({
c(input$text1)
})

observe(input$compare, {
output$analysis<- renderUI({
d<- stringdist(a(),b() ,method = "cosine")
return(d)
})
})
}

shinyApp(ui,server)

library(shiny)

ui <- fluidPage(
    sliderInput("myslide",label = "slideme",min = 0,max=10,value = 5),
    textOutput("analysis")
)

server <- function(input, output, session) {
  
  mycompute <- reactive({
    input$myslide ^ 2
  })
  output$analysis <- renderText({
    req(mycompute())
  })
}

shinyApp(ui, server)

Changed code- but still not working-

library(shiny)
library(RecordLinkage)
library(tidyverse)
library(dplyr)
library(shinydashboard)
library(tesseract)
library(magick)
library(stringdist)


ui<- fluidPage(
  fluidRow(
    column(4,
           fileInput("image1", "Upload Image 1")),
    column(4,
           fileInput("image2", "Upload Image 2")),
    column(8,
           actionButton("submit", "Submit"), 
           actionButton("compare", "Analyze"))
  ),
  fluidRow(column(12,
                  box(width = 12,
                      uiOutput("text"),
                      title = "Text Output 1"
                  )
  )),
  fluidRow(column(12,
                  box(width = 12,
                      uiOutput("text1"),
                      title = "Text Output 2"
                  )
  )),
  fluidRow(column(12,
                  box(width = 12,
                      textOutput("analysis"),
                      title = "Analysis"
                  )
  ))
  
)
server<- function(input, output, session)
{
  
  
  observeEvent(input$submit, {
    req(input$image1)
    output$text<- renderUI({
      t1<- c(image_ocr(image_read(input$image1, path = input$image1$datapath)))
      t<- as.character(t1)
      return(t1)
    })
  })
  observeEvent(input$submit, {
    req(input$image2)
    output$text1<- renderUI({
      t2<-c(image_ocr(image_read(input$image2, path = input$image2$datapath)))
      t2<-as.character(t2)
      return(t2)
    })
  })
  
  a<- reactive({
    HTML(stringdist(input$text,input$text1 ,method = "cosine"))
  })
  
  
  
  observeEvent(input$compare,{
    output$analysis<- renderText({
      req(a())
    }) 
  })
}

shinyApp(ui,server)```

please read the documentation for renderUI,
renderUI places shiny outputs, but you dont provide any.
you can skip studying about the proper use of renderUI, probably though, because if you want pure text output then you would use a textOutput (or verbatimtextoutput) and renderText (or renderPrint). Only these two know how to print text... renderUI doesn't know how on its own.

I did. and mistakenly gave another code. Kindly look the updated code above.

ok, so for the rest of the code you have other problems.
You are treating outputs (text and text1 ) as if they are simultaneously inputs (input$text)
they are not. an input$text would only be available to you if you place an textInput widget.
If you can extract character from the image succesfully then you would want to have that be a reactive() and assign that a name like text1, and then pass text1() into your renderUI for output$text, while also passing text1() into your a <- reactive() to compute on.

1 Like

It worked.. Thanks alot Nir. You saved my day..!

Just one thing, when I am comparing the same document the result is showing 0 rather that 100%. Why?

because there is zero difference ?

Thanks a ton Nir.. :slight_smile: