verbatimTextOutput for multiple languages programming code

30 Views Asked by At

I have this shiny app:

library(shiny)

# Define UI 
ui <- fluidPage(
  tags$head(
    tags$style(
      HTML(
        "
        .form-control {
            border-radius: 4px 4px 4px 4px;
        }
        #pythonCode, #javaCode {
        font-size: 14px;
        width: 300px; 
        height: 300px; 
        max-width: 100%;
        padding: 6px 12px; 
        white-space: pre-wrap;
        }
        "
      )
    )
  ),
  titlePanel("Multiple languages example"),
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(
        tabPanel("Python", verbatimTextOutput("pythonCode")),
        tabPanel("Java", verbatimTextOutput("javaCode"))
      )
    ),
    mainPanel()
  )
)

server <- function(input, output) {
  
  output$pythonCode <- renderText({readLines('helloworld.py')})
  output$javaCode <- renderText({readLines('helloworld.java')})
}

shinyApp(ui = ui, server = server)

It shows:

enter image description here

Is there anyway to set the style to the content of the verbatimTextoutput in base of the language programming? I would like to add line numbers also.

1

There are 1 best solutions below

0
Lev On

Using shinyAce package for the editor and rclipboard package for the copy-to-clipboard button it is working:

library(shiny)
library(shinyAce)
library(rclipboard)

ui <- fluidPage(
  titlePanel("Multiple languages example"),
  rclipboardSetup(),
  splitLayout(
    cellWidths = c("50%"),
  #sidebarLayout(
    #sidebarPanel(
      tabsetPanel(
        tabPanel("Python",
                 aceEditor(
                   outputId = "python",
                   mode="python",
                   theme="cobalt",
                   readOnly = T,
                   height = "300px"
                 )
                 ,uiOutput("pythonCopyui")
        ),
        tabPanel("Java",
                 aceEditor(
                   outputId = "java",
                   mode="java",
                   theme="cobalt",
                   readOnly = T,
                   height = "300px"
                 )
                 ,uiOutput("javaCopyui")
        )
      )
    #)
   # ,mainPanel()
  #)
)
)

server <- function(input, output, session) {
  file_content <- readLines("helloworld.py")
  file_content <- paste(file_content, collapse = "\n")
  updateAceEditor(session, "python", value = file_content)
  
  file_content <- readLines("helloworld.java")
  file_content <- paste(file_content, collapse = "\n")
  updateAceEditor(session, "java", value = file_content)

  output$pythonCopyui <- renderUI({
    rclipButton(
      inputId = "pythoncopy",
      label = "Copy code to clipboard",
      clipText = input$python,
      icon = icon("clipboard"),
      options = list(delay = list(show = 800, hide = 100), trigger = "hover")
    )
  })  
  
  
  output$javaCopyui <- renderUI({
    rclipButton(
      inputId = "javacopy",
      label = "Copy code to clipboard",
      clipText = input$java,
      icon = icon("clipboard"),
      options = list(delay = list(show = 800, hide = 100), trigger = "hover")
    )
  })
  
}

shinyApp(ui = ui, server = server)

enter image description here