How to develop a shiny web application to display word and images from an XML file?

I am developing a shiny application to display words and images from an XML file one after another in the given time interval (let's say 1 second) on shiny UI. If a user selects a file, it read the file and display the 1st word on UI then after 1-second 1st word disappears and 2nd word displayed on UI and then again after 1-second 2nd word disappears and 3rd word displayed on UI and so on. If there is an image in the file then it should also display on UI and after 1 second it should also disappear. I wrote a code to get a word from a sentence in each second of interval. Right now it is displayed on R console but I want to display these words and images in the shiny UI. Below is an illustration of the problem (it's not my actual code for cleanliness reasons, but it works as an illustration): Here is my code:

library(shiny)
library(tokenizers)
library(magick)
library(stringr)
library(qdap)
library(xml2)


ui <- dashboardPage(skin = 'purple',
                   dashboardHeader(title = "Document Reader"),
                   dashboardSidebar(width = 200,
                                    sidebarMenu(
                                      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
                                    )
                   ),
                   dashboardBody(skin = 'purple',
                                 tabItems(
                                   # First tab content
                                   tabItem(tabName = "dashboard",
                                           h2("Dashboard tab content"),
                                           fluidRow(
                                             box(title = "Uploading File",
                                                 fileInput("file1", "Choose a Document:"),
                                                 actionButton(inputId = "submit",label = "Read")

                                             ),
                                             column(8,verbatimTextOutput("opt1"))
                                           )
                                   )
                                 )
                   )
)


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

 dw <- eventReactive(input$submit,{
   filepath <- input$file1$datapath
   doc <- read_xml(filepath)
   nodeset <- xml_children(doc)
   for(i in 1:length(nodeset)){
     wpm <- as.integer(xml_attr(nodeset[i], "wpm"))
     #print(paste("WPM", wpm))
     data2 <-xml_find_all(nodeset[i], ".//p | .//localImage")
     line <-  xml_text(data2)
     image <-  xml_attr(data2, "descr")
     image <- image[!is.na(image)]

     for(j in line){
       #print(j)
       if ( j == ""){
         tiger =  image_read(image)
         sink(tempfile())
         print(tiger)
         sink()
       }
       else{
         words = unlist(strsplit(j, ' '))
         words = words[words !=""]
         words = str_remove_all(words, "\n")
         total = length(words)
         for (w in 1:total){
           len = length(words[[total]])
           for (w1 in 1:len){
             if (str_detect(words[[w]][w1],'[.]')){
               print(words[[w]][w1])
               Sys.sleep(1)
             } else {
               print(words[[w]][w1])
               Sys.sleep(0.5)
             }
           }
         }
       }
     }
   }
 })
 output$opt1 <- renderUI({
   dw()
 })
}

shinyApp(ui=ui, server=server)

Here is my XML file:

<?xml version="1.0"?>
<data>
    <section wpm = "200">
         <p>History</p>
         <p>The society that sparked change at Cambridge</p>
         <p>The ancient university owes its scientific status to an unexpected source, finds Georgina Ferry.</p>
    <localImage id="0" caption = "The Cambridge Observatory was founded in 1823, four years after the Cambridge Philosophical Society" descr="/home/i9/DocumentReader/DocumentReader/image/Image1.png"/>
         <p>We conclude this section by discussing the problem of classification,since it will serve as a prototypical problem for a significant part of this book. It occurs frequently in practice: for instance, when performing spam filtering, we are interested in a yes/no answer as to whether an e-mail contains relevant information or not.</p>
    </section>
</data>

How can I display these words on shiny UI.

Hi,

It's odd to see that the exact same scenario appears on this forum in the span of a few days :slight_smile:
I just answered the same one in this post:

If this is a problem given for a competition in coding or a homework for a class, I think this is not the way to solve the issue... If you can give me a good reason why you have the exact same specific problem and need it so be solved by the community, I'm happy to help, but do look a the other post first.

PJ

HI PJ,

Yes, that question was posted by me 2 days ago. Right now my that ID is not working so I posted by another ID.
Actually that code is working fine for txt file. But when I tried to implement same logic with XML file, it still print words on R console.

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