Outputting Multiple Lines of Text with Rendertext() in R Shiny

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.

Outputting multiple lines of text in R Shiny using renderUI

Use paste's collapse argument instead of the for-loop:

library(shiny)

my_sentences <- LETTERS

ui <- fluidPage(
uiOutput("myText")
)

server <- function(input, output, session) {
output$myText <- renderUI({
HTML(paste0(my_sentences, collapse = "<br>"))
})
}

shinyApp(ui, server)

Multi-line text output in R Shiny

I figured it out: I needed to use the collapse argument in paste such that <br> was inserted between every item in the character vector capture.output(print(summary(m0))).

The next problem was that whitespace would be ignored (because HTML), which I fixed by sandwiching the output between <pre> tags in order to preserve whitespace verbatim. The solution in server.R looks like this:

output$modelParameters <- renderUI(
HTML(
paste(
c("<pre>", capture.output(print(summary(m0))), "</pre>"),
collapse = "<br>"
)
)
)

When called in ui.R by htmlOutput("modelParameters"), it outputs the text, formatted appropriately in a grey box, as intended.

printing multiple lines of renderText in Shiny

The syntax in your call to mainPanel() is slightly off.

To fix it, use this

# Show a plot of the generated distribution
mainPanel(
textOutput("caption1"),
textOutput("caption2")
)

instead of this

# Show a plot of the generated distribution
mainPanel({
textOutput("caption1")
textOutput("caption2")
})

The problem with your version is that in R, whenever an expression enclosed in braces is evaluated, only the result of the final statement in it is returned. You can see that this is more generally the case by trying something like this:

{5
6
9}
# [1] 9

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)

Loop and renderText in R shiny

Something like this

library(tidyverse)


renderText({

emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)

df_text <- c()
for (row in 1:nrow(emp.data)) {
name <- emp.data[row, "emp_name"]
salary <- emp.data[row, "salary"]
df_text[row] <- paste(" Employee ", name ,"has a total of ",salary," dollars")
}

df_text

})

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

Paste multiple values into output of renderText

Like this:

paste(c("You have selected", input$var), collapse = " ")

R shiny : display text as code on several lines

I have previously used cat() for this purpose:

library(shiny)

ui <- fluidPage(
mainPanel(verbatimTextOutput("vtout"))
)

server <- function(input, output) {
output$vtout <- renderPrint({
cat("just", "some", "code", sep = "\n")
})
}

shinyApp(ui, server)

Sample Image



Related Topics



Leave a reply



Submit