xyy
xyy

Reputation: 547

How to format text display in R Shiny?

I have a list object with each list containing a character string, and each character string in each list element represent a paragraph. I'm trying to display the result in a shiny application, where I can display each paragraph separately (with new line in between), but I haven't figured out if there's a way to do this.

Here's what I have so far:

shinyUI(
  fluidPage(

  # Application title.
  titlePanel("M&A Clearing House"),

  sidebarLayout(
    sidebarPanel(
      # Copy the line below to make a text input box
      textInput("company", label = h3("Company Name"), value = "Enter text..."),


      selectInput("year", "Choosing Year Range", 
                  choices = c("2014", "2015", "2016"),
                  selected="2015", multiple=T),

      actionButton("submit","Submit")
    ),

    mainPanel(
      tabsetPanel(
        tabPanel("All results", textOutput("allresult"))
      )
    )
  )
))


shinyServer(function(input, output, session) {

 # ....assuming the result generated would look like this:...
 # exampletext <- list()
 # exampletext[[1]] <- "this is a paragraph"
 # exampletext[[2]] <- "this is another paragraph"
 # exampletext ....assuming the result generated would look like this:...

  output$allresult <- renderPrint({
    return(unlist(exampletext()))
    }
  )

})

Upvotes: 2

Views: 4709

Answers (1)

Molx
Molx

Reputation: 6921

You can use lapply on your list to generate the p tags you want to output. On the UI code, replace the textOutput with this:

htmlOutput("allresult")

On the server code, use this instead:

output$allresult <- renderUI(lapply(exampletext, tags$p))

Here's a full working example:

library(shiny)
shinyApp(shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(),
      mainPanel(
        tabsetPanel(
          tabPanel("All results", 
                   htmlOutput("allresult"))
        )
      )
    )
  )
),
shinyServer(function(input, output) {
  exampletext <- rep(as.list("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."), 5)
  output$allresult <- renderUI(lapply(exampletext, tags$p))
}))

Upvotes: 4

Related Questions