How to display words of a file one at a time in specific time interval in shiny application?

I am developing a shiny application to display words and images from a file one after another in the interval of 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.
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):

library(shiny)
library(shinydashboard)
library(stringr)


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("text", "Enter Text:"),
                                                  actionButton(inputId = "submit",label = "Read")
                                                  
                                              ),
                                              column(8,uiOutput("opt1"))
                                            )
                                    )
                                  )
                    )
)

server <- function(input, output, session) {
  cat("\014")
    input_file1 <- eventReactive(input$submit,{
      filepath <- input$text$datapath
      doc <- readLines(filepath)
  words = unlist(strsplit(doc, ' '))
  total = length(words)
  for (w in 1:total){
    len = length(words[[total]])
    for (w1 in 1:len){
      print(words[[w]][w1])
      Sys.sleep(1)
      cat("\014")
    }
  }
})
  
  output$data_1 <- renderText({
    fileText_1 <- input_file1()
    return(fileText_1)
  })
  output$opt1 <- renderUI({
    verbatimTextOutput("data_1")
  })
}

shinyApp(ui, server)

How can I display these words on shiny UI?

Hi,

Although the concept is super easy to implement in R, it turned out to be very tricky in Shiny :slight_smile:I did manage to find a way using the invalidateLater function, but I'm sure my code is not the cleanest, so please let me know if you find any ways to make it tidier...

library(shiny)
library(shinydashboard)
library(stringr)


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("text", "Enter Text:"),
                                                  actionButton(inputId = "submit",label = "Read")
                                                  
                                              ),
                                              column(8,uiOutput("opt1"))
                                            )
                                    )
                                  )
                    )
)

server <- function(input, output, session) {
  
  words = reactiveVal()
  nextWord = reactiveVal()
  start = reactiveVal(F)
  i = reactiveVal(0)

  observeEvent(input$submit,{
    
    if(start() == F){
      filepath <- input$text$datapath
      doc <- readLines(filepath)
      words(unlist(strsplit(doc, ' ')))
      
      start(T)
      updateActionButton(session, "submit", label = "STOP")
    } else {
      start(F)
      updateActionButton(session, "submit", label = "Read again")
    }
    
    
    
  }, ignoreInit = T)
  
  observe({
    start()
    
    if(isolate(i() < length(words()) && start() ==T)){
      invalidateLater(1000)
    }
    
    isolate({
      if(i() < length(words()) && start() ==T){
        nextWord(words()[i()])
        i(i() + 1)
        print(i())
      }
    })
    
    
  })
  
  output$data_1 <- renderText({
    fileText_1 <- input_file1()
    return(fileText_1)
  })
  
  output$opt1 <- renderUI({
    tags$h1(nextWord())
  })
}

shinyApp(ui, server)

Good luck!
PJ

Hi PJ, Great!. It gives me overview of how to print on UI. Actually, I have to put so many conditions in between and also I have to render an image on UI, so let me try this. If I stuck anywhere I will back to you. Thank a lot for this piece of work...

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