New line in text output

6,648 views
Skip to first unread message

Aous Abdo

unread,
Mar 25, 2014, 2:50:12 PM3/25/14
to shiny-...@googlegroups.com

I noticed that "\n" doesn't do anything in cat when called as a shiny textOutput. How can I add a new line to a text output in shiny? 

Here is the code I am running:

server.R
.....
  output$Text <- renderPrint({
.... 
    text1 <- paste("Test 1 of length ", input$test1," hours : ",MaxFailFun(TL[1], input$TRV, input$CL ) ,sep="")
    # Add text entries for last two tests if selected by user
    if(length(colnames(df)>2)){
      if ("Test2" %in% colnames(df)){
        text2 <- paste("Test 2 of length ", input$test2," hours : ",MaxFailFun(TL[2], input$TRV, input$CL ) ,sep="")
      }
      if ("Test3" %in% colnames(df)){
        text3 <- paste("Test 3 of length ", input$test3," hours : ",MaxFailFun(TL[3], input$TRV, input$CL ) ,sep="")
      }
    }
    cat(paste(text1, text2, text3, sep="\n"))
  })

....

ui.R
.....
textOutput("Text")
....

Seth Green

unread,
Aug 31, 2015, 8:22:51 AM8/31/15
to Shiny - Web Framework for R
Did you ever hear back on this?  I'm having the same issue.
S

Joe Cheng

unread,
Aug 31, 2015, 3:20:07 PM8/31/15
to Seth Green, Shiny - Web Framework for R
You need verbatimTextOutput() instead of textOutput() if you want whitespace to be respected, due to the nature of HTML.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/9f35179c-da0d-418a-9719-93648e3cea6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Seth Green

unread,
Aug 31, 2015, 8:04:43 PM8/31/15
to Joe Cheng, Shiny - Web Framework for R
Thanks Joe.  I just like the formatting of the textOutput better, so I was trying to use that.  It's also reactive (as to whether or not it displays any text) so when the requisite checkbox isn't checked verbatimTextOutput creates an empty grey box.

Anyway, I found a way to do it using renderUI and htmlOutput here:

That seems to work, but I was wondering if there's an easier way.  Also, if there's a way to make spaces and tabs (because you can't seem to with the htmlOutput solution).

Thanks for getting back to me.
Seth

Joe Cheng

unread,
Sep 1, 2015, 6:08:28 PM9/1/15
to Seth Green, Shiny - Web Framework for R
Uhhh I don't recommend using renderUI and htmlOutput this way. You are taking text that is fundamentally text, and coercing to HTML without escaping (meaning if the text just happens to include a string that contains special HTML characters, it could be parsed incorrectly).

How about this instead:

...
textOutput("foo"),
tags$style(type="text/css", "#foo {white-space: pre-wrap;}")
...

This should give you all the things you want. (Replace the foo in #foo with the ID of your textOutput)

Joe Cheng

unread,
Sep 1, 2015, 6:15:34 PM9/1/15
to Seth Green, Shiny - Web Framework for R
Oh or better yet, in place of textOutput("foo"):

tagAppendAttributes(textOutput("foo"), style="white-space:pre-wrap;")

Seth Green

unread,
Sep 2, 2015, 7:48:22 AM9/2/15
to Joe Cheng, Shiny - Web Framework for R
Thanks so much Joe.  I'll try those out and be in touch if I have any issues.

While I have your ear, I've been having a stupid (hopefully simple) problem with sliderInput too.  Basically I'm trying to make a slider to choose a year, so I want it to read (for example) `1998` instead of `1,998` but whenever I put in the `sep=""` tag, it breaks the whole app and says `ERROR: unused argument (sep = "")`

I posted the whole question on stack overflow and here on the support.rstudio page, but I can't seem to find any answers.  Is that tag depreciated or is my syntax just messed up somehow?

Thanks so much,
Seth

Yihui Xie

unread,
Sep 2, 2015, 10:43:17 AM9/2/15
to Seth Green, Joe Cheng, Shiny - Web Framework for R
Just for the record: http://stackoverflow.com/q/32185200/559676
(Please refrain from bringing completely different questions to an
existing thread. We do read all posts here and try to answer them
before we run out our 24 hours :) You are welcome to point us to your
previous posts, though. Sorry for not answering your question
earlier.)

Regards,
Yihui
> https://groups.google.com/d/msgid/shiny-discuss/CACSmu0VbqJbWDNRhaJv8mq5kbNQGso2id-d5EGdBX8QY71eeeg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages