How to give breakdown of text prediction sentences in Shiny

I have created a text prediction app that displays 10 sentences based on the word searched in the toolbar. However, all the sentences are being displayed in a single toolbar horizontally. I would like the sentences to appear sequentially i.e. vertically, one after another

   >  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() })
}

the output is coming as - Angry look for some time for Angry glance, and the duc d’enghien Angry look was a tone or Angry tears. “Lise!” Was that? The Angry tears. “Lise!” Said princess anna Angry look as much as she Angry look for three of the Angry tears. “Lise!” Said to the Angry look fixed on with an Angry glance, and fireworks are not

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

here is a solution with css style tag in the UI, another option (that you can read about, is adding a custom CSS file)

library(shiny)

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

server <- function(input, output, session) {
  output$mytext <- renderPrint({
    
    sentences <- rep("this is a sentence\n",5)
    length(sentences)
    cat(paste0(1:length(sentences)," ",sentences))
  })
}

shinyApp(ui, server)
1 Like

i modified your solution a little and got the output exactly i wanted :

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

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