Skip to contents

Renders R code with syntax highlighting using the highlight.js library.

Usage

renderRcode(
  expr,
  env = parent.frame(),
  quoted = FALSE,
  outputArgs = list(),
  delay = 100
)

rcodeOutput(outputId)

Arguments

expr

An expression to evaluate.

env

The parent environment for the reactive expression. By default, this is the calling environment, the same as when defining an ordinary non-reactive expression. If expr is a quosure and quoted is TRUE, then env is ignored.

quoted

If it is TRUE, then the quote()ed value of expr will be used when expr is evaluated. If expr is a quosure and you would like to use its expression as a value for expr, then you must set quoted to TRUE.

outputArgs

List of additional arguments to pass to the output function.

delay

Delay in milliseconds before syntax highlighting starts.

outputId

Output variable to read the R code from.

Value

A render function similar to shiny::renderText().

Examples

if (interactive()) {
  library("shiny")

  ui <- fluidPage(
    fluidRow(
      column(
        4,
        textAreaInput(
          "rcode_in",
          label = NULL,
          width = "100%", height = "300px",
          value = "library('shiny')\n\nset.seed(42)\nx <- runif(10)"
        )
      ),
      column(
        8,
        rcodeOutput("rcode_out")
      )
    )
  )

  server <- function(input, output) {
    output$rcode_out <- renderRcode({
      htmltools::htmlEscape(input$rcode_in)
    })
  }

  shinyApp(ui = ui, server = server)
}