4
$\begingroup$

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
'''''
$\endgroup$

2 Answers 2

0
$\begingroup$

Maybe you could put the sentences in a dataframe and used renderTable, something like this:

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

[edit]

In order to only have them line by line something like this might work:

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

Alternatively you could have a numbered list by using the corresponding HTML tags: insert the whole list inside <ol> ... </ol> and every sentence in a <li> .. </li>.

$\endgroup$
5
  • $\begingroup$ It works but it has tags and names of columns like data frame : <table class = 'table shiny-table table- spacing-s' style = 'width:auto;'> <thead> <tr> <th style='text-align: left;'> sentences </th> </tr> </thead> <tbody> <tr> <td> Prince in your age and then </td> </tr> <tr> <td> Prince understood all anna pávlovna and </td> </tr> <tr> <td> Prince gave him full confidence,” said </td> </tr>... $\endgroup$
    – Vikram
    Commented Jun 30, 2020 at 20:57
  • $\begingroup$ @d123 I proposed an alternative option, see edit. $\endgroup$
    – Erwan
    Commented Jun 30, 2020 at 22:51
  • $\begingroup$ i am still getting similar output as earlier with tags and column names $\endgroup$
    – Vikram
    Commented Jul 1, 2020 at 15:44
  • $\begingroup$ Hmm I'm not sure what happens in the case of renderTable, since the content is not supposed to contain these tags. You could try adding escape=FALSE, i.e. renderTable(sentences(), escape=FALSE) but I don't think that's the problem. In the second option I was wrong, it's a bit more complex than I thought, see stackoverflow.com/questions/23233497/… $\endgroup$
    – Erwan
    Commented Jul 1, 2020 at 16:28
  • $\begingroup$ @d123 Then you should write it as an answer to your question and accept it, so that in the future other people facing the same issue find the solution easily. $\endgroup$
    – Erwan
    Commented Jul 1, 2020 at 17:04
0
$\begingroup$

Worked for me with some tweaks

  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'))
        })
      }
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.