bretauv
bretauv

Reputation: 8577

R shiny : is there a special font for writing R code in verbatimTextOuput?

I'm writing a shinyapp which displays some R code in a verbatimTextOutput environment and I would like to know if there is a way to display this code as Rstudio displays it (with green font after hashtag for example).

I know that it is possible to change font with css (even if I don't know how to do it) but is there a "package" that automatically displays code as Rstudio does ?

Here's a reproducible example :

library(shiny)

ui <- fluidPage(
  titlePanel(""),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(verbatimTextOutput("base", placeholder = FALSE))
    )
  )

server <- function(input, output) {

  output$base <- renderPrint({
    cat("# I would like this to be written in green (or other color)",
        "library(this could be in blue)",
        sep = "\n")
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 116

Answers (3)

bretauv
bretauv

Reputation: 8577

After having developed my app, I can say that Stephane Laurent's answer is the one that corresponds the most to what I wanted to do (i.e display reactive R code in a shiny app).

The shinyAce package is really useful, you can find some examples here (https://trestletech.github.io/shinyAce/).

Thanks anyway for your answers !

Upvotes: 1

DSGym
DSGym

Reputation: 2867

You can not exactly do what you wanted to do, but there are two solutions of which I hope you will like one :-).

You can either use HTML Tags (use paste0 to assign colors dynamically) or set the style with css for your verbatimOutput. Two colors is unfortunately not possible, since VerbatimOutput does not handle inline CSS.

library(shiny)

ui <- fluidPage(
    tags$head(tags$style(HTML("
                            #base2 {
                              color: blue;
                            }
                            "))),
    titlePanel(""),
    sidebarLayout(
        sidebarPanel(),
        mainPanel(selectizeInput("color", "Colors", choices = c("green", "blue", "red"), selected = "green", multiple = FALSE),
                  uiOutput("base", placeholder = FALSE),
                  tags$br(),
                  verbatimTextOutput("base2"))
    )
)

server <- function(input, output) {

    output$base <- renderUI({
        HTML(paste0('<span style="color:', input$color, '">I would like this to be written in green</span> (or other color)<br>
          <span style="color:blue">library(this could be in blue)</span>'))
    })

    output$base2 <- renderPrint({
        cat("# I would like this to be written in green (or other color)",
            "library(this could be in blue)",
            sep = "\n")
    })

}

shinyApp(ui = ui, server = server)

Upvotes: 1

Aleksandr
Aleksandr

Reputation: 1914

I would suggest to hardcode html markup like this:

library(shiny)

ui <- fluidPage(
  titlePanel(""),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(uiOutput("base", placeholder = FALSE))
  )
)

server <- function(input, output) {

  output$base <- renderUI({
    HTML("<form class = 'well'>
            <p style = 'color: green;'>I would like this to be written in green (or other color)</p>
            <p style = 'color: blue;'>library(this could be in blue)</p>
          </form>")
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions