render HTML as-is from a textarea with renderUI

478 views
Skip to first unread message

Adrian Waddell

unread,
Jun 2, 2016, 5:18:08 AM6/2/16
to Shiny - Web Framework for R
Hello All,

I am working on a commenting system with shiny where a user can enter a comment which I then wish to eventually display within a uiOutput block. Does anyone have any suggestions how this is best done so that the formatting in the output is as entered in the textarea?

Here is a minimal working example with my current issues described below (note I need the tagList() call as I add some more meta-data)

ui.R:

library(shiny)

shinyUI(fluidPage(
 sidebarLayout(
   sidebarPanel(
     tags$textarea(id="my_textarea", rows=5, "Leave a comment...")
   ),
   mainPanel(
     uiOutput("my_output")
   )
 )
))


server.R:

library(shiny)

shinyServer(function(input, output) {

 output$my_output <- renderUI({
   txt <- input$my_textarea

   tagList(
     tags$p("The comment is:"),
     shiny::HTML(gsub("\n", "<br/>", txt))
   )

 })

})


My problem is that this is a hack as entering html tags such as "<br/>" in the text area creates a new-line in the output.

Does anyone have a solution to display the text exactly as it is shown in the textarea box. Preferably this solution should not enclose the comment within <pre>...</pre> as this won't lead to nice line-wrapping.

Thank you very much in advance,

Adrian

Here my sessionInfo()

> sessionInfo()
R version
3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04 LTS

locale
:
[1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C LC_TIME=de_CH.UTF-8        LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=de_CH.UTF-8    LC_MESSAGES=en_US.UTF-8 LC_PAPER=de_CH.UTF-8       LC_NAME=C
[9] LC_ADDRESS=C               LC_TELEPHONE=C LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C

attached
base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages
:
[1] shiny_0.13.2

loaded via a
namespace (and not attached):
[1] R6_2.1.2        htmltools_0.3.5 tools_3.3.0     Rcpp_0.12.5 jsonlite_0.9.20 digest_0.6.9
[7] xtable_1.8-2    httpuv_1.3.3    mime_0.4






Joe Cheng

unread,
Jun 2, 2016, 10:00:49 AM6/2/16
to Adrian Waddell, Shiny - Web Framework for R
Hi Adrian,

You can use CSS to give you pre behavior except with line wrapping:

tags$div(style="white-space: pre-wrap;", txt)
--
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/0d8ef825-fd1d-4ab0-81f2-b82a752af312%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adrian Waddell

unread,
Jun 2, 2016, 4:13:03 PM6/2/16
to Shiny - Web Framework for R, adrian....@gmail.com
Hi Joe,

Great, thank you very much. Your answer has saved me a lot of time.

Shiny is great, thank you for the good work.

Best,
Adrian
Reply all
Reply to author
Forward
0 new messages