how to insert new line in R shiny string

前端 未结 5 1987
一整个雨季
一整个雨季 2020-12-01 15:50

In shiny, I have the following:

  output$sequenceText <- renderText({
    showSequence()
  })

showSequence <- reactive({
  selectedSeqs <- as.numer         


        
相关标签:
5条回答
  • 2020-12-01 16:11

    How about

      output$text2 <- renderUI({
        HTML('hello <br> world')
      })
    
    0 讨论(0)
  • 2020-12-01 16:15

    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:

    0 讨论(0)
  • 2020-12-01 16:17

    This is a different approach which is perhaps a bit simpler.

    ui <- fluidPage(
    
      htmlOutput("text")
    )
    
    server <- function(input, output) {
    
      output$text <- renderText({
    
        paste0("<p>", letters[1:10], "</p>")
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
  • 2020-12-01 16:30

    There is also the write(x, file = "") trick:

    renderPrint({
     write(showSequence(), file = "")
    })
    

    If the output is a verbatimTextOutput then this works.

    0 讨论(0)
  • 2020-12-01 16:32

    for anyone reading this, you might also want to use tags$br(), which you simply have to insert as argument after a piece of text. For example,

    tags$div(
        "a piece of text", tags$br(),
        "this will start from the new line now", tags$br(),
        "and this as well",
        "but not this" )
    
    0 讨论(0)
提交回复
热议问题