1

Example below. I would like to add a sentence to my app via the UI section. Not my call to verbatimTextOutput("Some Text Here \n and a new line here"):

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot"),
           verbatimTextOutput("Some Text Here \n and a new line here")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

When I run this I see this: enter image description here

Have been reading this post since I want to be able to add new lines with \n within a string of text, but cannot seem to be able to display just text in the app.

How can I add "Some Text Here \n and a new line here" in my app?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

4

As the name indicates, verbatimTextOutput is for rendering an output. It is usually used with renderPrint in the Shiny server file. That is, in server:

output[["mytext"]] <- renderPrint({
  "Some Text Here \n and a new line here"
})

and in ui:

verbatimTextOutput("mytext")

But since you don't need a reactive text, you can simply use a p tag in ui (and nothing in server:

tags$p("Some Text Here \n and a new line here")

If the linebreak does not work (I have not tried), you can try:

tags$p("Some Text Here", br(), "and a new line here")

or

HTML("Some Text Here <br> and a new line here")
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225