0

I would like to create a Shiny R application that can take unformatted Stata code input by the user, add html tags, and return the entire block of code for easy copy and paste into an html publishing venue such as blogs or webpages.

I already have the R code that can handle the formatting A Stata HTML syntax highlighter in R. And most of the Shiny implementation seems very easy. The major challenge I am having is creating an html textbox or other object that can easily take a reactive element from the Shiny's server.R and return it to the user without formatting the html tags.

Example:

Stata code input through a text box

clear
set obs 4000
gen id = _n
gen eta1 = rnormal()
gen eta2 = rnormal()

XX Shiny submit button XX

Return in another text box

<span style="color: #9900FF">set</span> <span style="color: #0000CC"><b>obs</b></span> 4000
<span style="color: #0000CC"><b>gen</b></span> id = <span style="color: #9900FF">_n</span>
<span style="color: #0000CC"><b>gen</b></span> eta1 = <span style="color: #9900FF">rnormal</span>()
<span style="color: #0000CC"><b>gen</b></span> eta2 = <span style="color: #9900FF">rnormal</span>()

Overall, I think this is generally a long question for a potentially very simple answer. Thanks for your consideration.

4

1 回答 1

2

renderText()不解析 HTML 标签。例如,如果你这样做:

output$code <- renderText({

  paste0(
    '<span style="color: #9900FF">set</span> <span style="color: #0000CC"><b>obs</b></span> 4000',
    '<span style="color: #0000CC"><b>gen</b></span> id = <span style="color: #9900FF">_n</span>',
    '<span style="color: #0000CC"><b>gen</b></span> eta1 = <span style="color: #9900FF">rnormal</span>',
    '<span style="color: #0000CC"><b>gen</b></span> eta2 = <span style="color: #9900FF">rnormal</span>'  
  )

})

这是你的ui.R

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Code"),

  sidebarPanel(

  ),

  mainPanel(
    verbatimTextOutput("code")
  )

))

内容只是文本。


但由于您尚未发布您的 ui.R(或 index.html),我不确定您是如何呈现输出的。如果您在显示原始文本而不是解析的 HTML 时遇到问题,您可以随时<&lt;>替换,&gt;如下所示:

html <- '<span>text</span>'
x <- gsub('<', '&lt;', html)
gsub('>', '&gt;', x)

这将产生:&lt;span&gt;text&lt;/span&gt;并且不应在浏览器中显示为已解析的 HTML。

于 2013-08-14T08:46:45.617 回答