I have a Shiny app that generates computer code dynamically based on user inputs and presents it to the user so they can see exactly what query is being sent to a database. I have prism syntax highlighting working for code that is directly in the user interface function, so I know that prism is working; but for code that is generated in the server and sent to the user via renderText
and htmlOutput
the highlighting doesn't work.
Here is an image of a simple example:
Which was made with this R file (and prism.css and prism.js in the www
folder of the Shiny app)
ui <- shinyUI(fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "prism.css")
),
tags$body(
tags$script(src="prism.js")
),
HTML("<pre><code class='language-sql'>SELECT * FROM mytable WHERE 1=2
-- this chunk should be syntax highlighted and it is
</code></pre>"),
HTML("<pre>SELECT * FROM mytable WHERE 1=2
-- this chunk should not be syntax highlighted
</code></pre>"),
htmlOutput("sql")
)
)
# Define the server code
server <- function(input, output) {
txt <- "<pre><code class='language-sql'>SELECT * FROM mytable WHERE 1=2
-- this chunk should be syntax highlighted but isn't for some reason,
-- presumably connected to it getting to the UI via renderText and htmlOutput
</code></pre>"
output$sql <- renderText(txt)
}
In the DOM Explorer I can see that in the webpage generated by Shiny, the third (not working) chunk is within a <div class="shiny-html-output shiny-bound-output">
, then is correctly wrapped in <pre>
and <code class="language-sql">
tags like the first chunk. So being wrapped in that div is what is stopping prism.js from doing its highlighting thing.
What should I do to have the third chunk highlighted like the first?