How to Insert New Line in R Shiny String

how to insert new line in R shiny string

To my knowledge, there are only two options to display multiple lines within shiny. One way with using verbatimTextOutput which will provide a gray box around you text (personal preference). The other is to use renderUI and htmlOutput to use raw html. Here is a basic working example to demonstrate the results.

require(shiny)
runApp(
list(
ui = pageWithSidebar(
headerPanel("multi-line test"),
sidebarPanel(
p("Demo Page.")
),
mainPanel(
verbatimTextOutput("text"),
htmlOutput("text2")
)
),
server = function(input, output){

output$text <- renderText({
paste("hello", "world", sep="\n")
})

output$text2 <- renderUI({
HTML(paste("hello", "world", sep="<br/>"))
})

}
)
)

This yields the following figure:

Sample Image

Shiny app with renderText function need to add new line

Try this:

library(shiny)

ui <- fluidPage(
verbatimTextOutput("value"),
htmlOutput("value2")
)

server <- function( session,input, output) {

output$value <- renderText({
paste("Line 1", "Line 2", sep="\n")
})

output$value2 <- renderUI({
HTML(paste("Line 1", "Line 2",sep ="<br/>"))
})
}

shinyApp(ui, server)

Sample Image

R Shiny Multi Line Row In Table From Text Input

Just replace the "/n" character representing a new line in a string by the corresponding html tag to do so. Look at my solution below.

library(shiny)
library(DT)
library(stringr)

ui <- fluidPage(
titlePanel("Multi-line row in Shiny Table"),
mainPanel(
# Add Text
textAreaInput(inputId = "Long_Text", label = "TEXT:", rows = 5, resize = "both"), br(),

actionButton("Add_text", "New Text"), br(),

# Display Text as Table
DT::dataTableOutput("Text_Table")
)
)

server <- function(input, output, session) {

# Generate Reactive Text Data
Text_DF <- reactiveValues(data = data.frame(Text = character()))

# Add New Text
observeEvent(input$Add_text, {
# Combine New Text With Old Text
Text_DF$data <- rbind(
Text_DF$data,
data.frame(Text = str_replace_all(input$Long_Text, "\n", "<br>")) # Here the hack
)
})

# Generate Table
output$Text_Table = renderDataTable({
Text_DF$data
}, escape = FALSE)

}

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

Why does R Shiny not recognize my line break command?

If you paste after changing the text to HTML, it will be character again.

library(shiny)

long_text <- htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult.")



ui <- fluidPage(

sidebarLayout(
sidebarPanel(
br(),
actionButton(inputId = "text_info",
label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
),

mainPanel(
)
)
)

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

observeEvent(input$text_info, {
showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
})
}

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

Sample Image

how to add multiple line breaks conveniently in shiny?

I have no idea if that's convenient for you, but it saves you some typing.

linebreaks(n) repeats <br/> n times and parses it as HTML.

library(shiny)


linebreaks <- function(n){HTML(strrep(br(), n))}

ui <- fluidPage(

titlePanel(
p(
h1("first sentence", align = "center"),

linebreaks(10),

h3("second sentence", align = "center")
)
)
)

Outputting multiple lines of text with renderText() in R shiny

You can use renderUI and htmlOutput instead of renderText and textOutput.

require(shiny)
runApp(list(ui = pageWithSidebar(
headerPanel("censusVis"),
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White", "Percent Black",
"Percent Hispanic", "Percent Asian"),
selected = "Percent White"),
sliderInput("range",
label = "Range of interest:",
min = 0, max = 100, value = c(0, 100))
),
mainPanel(textOutput("text1"),
textOutput("text2"),
htmlOutput("text")
)
),
server = function(input, output) {
output$text1 <- renderText({paste("You have selected", input$var)})
output$text2 <- renderText({paste("You have chosen a range that goes from",
input$range[1], "to", input$range[2])})
output$text <- renderUI({
str1 <- paste("You have selected", input$var)
str2 <- paste("You have chosen a range that goes from",
input$range[1], "to", input$range[2])
HTML(paste(str1, str2, sep = '<br/>'))

})
}
)
)

Note you need to use <br/> as a line break. Also the text you wish to display needs to be HTML escaped so use the HTML function.

Shiny R renderText paste new line and bold

Something like this?

app.R

    library(shiny)

ui <- shinyUI(fluidPage(


titlePanel("HTML"),


sidebarLayout(
sidebarPanel(
textInput("length",
"Enter your length:"),
textInput("weight",
"Enter your weigth:")

),


mainPanel(
htmlOutput("testHTML")
)
)
))


server <- shinyServer(function(input, output) {

output$testHTML <- renderText({
paste("<b>Your length is: ", input$length, "<br>", "Your weight is: ", input$weight, "</b>")
})
})


shinyApp(ui = ui, server = server)

R shiny break line in button label

You could use HTML function like this:

library(shiny)

shinyApp(
ui = fluidPage(
actionButton("btnId", HTML("I want a line break here <br/> since the label is too long"))),
server = function(input, output){})


Related Topics



Leave a reply



Submit