如何在 Shiny 中给出文本预测句子的细分

数据挖掘 r 可视化
2021-10-14 21:13:20

我创建了一个文本预测应用程序,它根据工具栏中搜索的单词显示 10 个句子。但是,所有句子都水平显示在一个工具栏中。我希望句子顺序出现,即垂直,一个接一个

    ui<- shinyUI(fluidPage(
  
  # Application title
  mainPanel(
   img(src='image.jpg', align = "right"),
 
  #titlePanel(title=div(img(src="spsimage.jpg"))),
  
  fluidRow(HTML("<strong> Search Bar")),
  #fluidRow(HTML(" <strong>Date: 06-29-2020</strong>") ),
  
  fluidRow(
    br(),
    p("Text predictior app ")),
  br(),
  br(),
  
  fluidRow(HTML("<strong>Enter a word. Press \"Next words\" button to predict the following words</strong>") ),
  fluidRow( p("\n") ),
  
  # Sidebar layout
  sidebarLayout(
    
    sidebarPanel(
      textInput("inputString", "Enter a word here",value = " "),
      submitButton("Next words")
    ),
    
    mainPanel(
      h4("Predicted Next Word"),
      verbatimTextOutput("prediction"),
      textOutput('text1'),
      
    )
  )
)))


server <- function(input, output, session) {
  sentences <- reactive({
    make_sentences(input$inputString)
  })
  output$prediction <- renderText({ sentences() })
}

输出是 - Angry look for some time for Angry glance,而 duc d'enghien Angry look 是一种语气或愤怒的眼泪。“丽丝!” 是吗?愤怒的眼泪。“丽丝!” 说安娜公主愤怒的样子,就像她愤怒的样子一样,寻找三颗愤怒的眼泪。“丽丝!” 对着怒目而视的说着怒目而视,而烟花却不是

I would prefer -

1- Angry look for some time for
2- Angry glance, and the duc d’enghien
3- Angry look was a tone or Angry tears. “Lise!” Was that? 
4-The Angry tears. “Lise!” Said princess anna 
5-Angry look as much as she
'''''
2个回答

也许您可以将句子放在数据框中并使用renderTable,如下所示:

server <- function(input, output, session) {
  sentences <- reactive({
    data.frame(sentences=make_sentences(input$inputString))
  })
  output$prediction <- renderTable(sentences())
}

[编辑]

为了让它们逐行排列,这样的事情可能会起作用:

server <- function(input, output, session) {
  sentences <- reactive({
    paste(make_sentences(input$inputString),sep='<br/>')
  })
  output$prediction <- renderTable(sentences())
}

或者,您可以使用相应的 HTML 标记来创建编号列表:将整个列表插入其中,<ol> ... </ol>并将每个句子插入<li> .. </li>.

通过一些调整为我工作

  ui <- fluidpage(
         tags$style("#mytext {white-space: pre-line;}"),
        verbatimTextOutput("mytext")



  server <- function(input, output, session) {
             output$mytext <- renderPrint({
        
            sentences <- rep(make_sentences(input$inputString))
            length(sentences)
            cat(paste0(1:length(sentences),"  ",sentences,sep= '\n'))
        })
      }