Hi,
Although the concept is super easy to implement in R, it turned out to be very tricky in Shiny
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