如何在闪亮的Web应用程序中按特定的时间间隔一个接一个地显示句子中的单词?

时间:2019-07-11 14:37:05

标签: r shiny shinydashboard

我正在开发一个闪亮的应用程序,以在闪亮的UI上给定的时间间隔(比如说1秒)中一个接一个地显示XML文件中的单词和图像。如果用户选择了文件,它将读取文件并在UI上显示第一个单词,然后在1秒后第一个单词消失并在UI上显示第二个单词,然后在1秒后第二个单词消失并在UI上显示第三个单词,等等上。如果文件中有图像,则它也应显示在UI上,并且1秒钟后也应消失。 我编写了一个代码,以便在间隔的每一秒内从一个句子中得到一个单词。现在它显示在R控制台上,但我想在闪亮的UI中显示这些单词和图像。 以下是问题的说明(出于清洁原因,这不是我的实际代码,但仅作为说明): 这是我的代码:

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)

这是我的XML文件:

<?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>

如何在闪亮的用户界面上显示这些单词。

1 个答案:

答案 0 :(得分:0)

我找到一种解决方案,这要感谢Google并混合使用不同的来源:

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"))

                                              ,box(verbatimTextOutput("var"),column(8,uiOutput("opt1"))
                                            ),

                                              box(actionButton(inputId = "display",label = "display")
                                              )
                                    )                              



                    ))))

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

  rv <- reactiveValues(words = character(0), n = 0,
                       sentence = "")
  # action button 
    # output$var <- renderText({
    #   if(input$display < length(input_file1()))
    #     return(input_file1()[input$display])
    # })
    # 

   observe({
     if(input$submit){
       rv$sentence <- unlist(strsplit(readLines(input$text$datapath), ' '))
     }
    # Re-execute this reactive expression immediately after 
    # it finishes (though you can also specify a longer time:
    # e.g. 1000 means re-execute after 1000 milliseconds have
    # passed since it was last executed)
    invalidateLater(1000, session)
    isolate({
      if (rv$n >= length(rv$sentence)) {
        return()
      } else {
        rv$n <- rv$n + 1
        rv$words <- rv$sentence[rv$n]
      }
      })
    })
    output$var <- renderPrint({
       return(rv$words)
    })


}

shinyApp(ui, server)

总结:

  1. 您不能同时使用reactiveEvent()observe()
  2. 您可以使用invalidateLater来重做一个Observ()真的很有用
  3. 您必须进行一些更改才能使用xml进行操作,但从理论上讲,它会起到希望的作用

来源:

  1. R Shiny: reactiveValues vs reactive
  2. Outputting multiple lines of text with renderText() in R shiny
  3. https://gist.github.com/bborgesr/61409e3852feb991336757f06392e52a