Search code examples
htmlcssrshinyshinydashboard

How to change the font family of verbatimTextOutput to be the same as the input in Shiny and Shinydashboard?


I would like to change the font family of the verbatimTextOutput to be the same as the input in Shiny and Shinydashboard. Here is an example.

# Load the packages
library(shiny)
library(shinydashboard)

# User Interface
ui <- dashboardPage(
    header = dashboardHeader(title = ""),
    sidebar = dashboardSidebar(
      sidebarMenu(
        menuItem(
          text = "Example",
          tabName = "tab1"
        )
      )
    ),
    body = dashboardBody(
      tabItems(
        tabItem(
          tabName = "tab1",
          fluidRow(
            column(
              width = 4,
              numericInput(inputId = "Number", label = "A numeric input", value = NA),
              strong("The same number as the numeric input"),
              verbatimTextOutput("Number_out")
            )
          )
        )
      )
    )
  )

server <- function(input, output, session){
  output$Number_out <- renderText(as.character(input$Number))
}

# Run the app
shinyApp(ui, server)

By running the app and type in a number, we can see that the font family is different in the numericInput and the verbatimTextOutput.

enter image description here

Based on this answer (https://stackoverflow.com/a/48037443/7669809) and this answer (https://stackoverflow.com/a/50784117/7669809), I edited my script as follows.

# Load the packages
library(shiny)
library(shinydashboard)

# User Interface
ui <- dashboardPage(
    header = dashboardHeader(title = ""),
    sidebar = dashboardSidebar(
      sidebarMenu(
        menuItem(
          text = "Example",
          tabName = "tab1"
        )
      )
    ),
    body = dashboardBody(
      tags$head(
        tags$style(
          HTML(
            '#Number_out {
            font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
            font-size: 12px;
            }'
          )
          )
        ),
      tabItems(
        tabItem(
          tabName = "tab1",
          fluidRow(
            column(
              width = 4,
              numericInput(inputId = "Number", label = "A numeric input", value = NA),
              strong("The same number as the numeric input"),
              verbatimTextOutput("Number_out")
            )
          )
        )
      )
    )
  )

server <- function(input, output, session){
  output$Number_out <- renderText(as.character(input$Number))
}

# Run the app
shinyApp(ui, server)

But the font family is still not the same.

enter image description here

It seems like I have not used the correct font family yet. Please let me know how I can achieve this.


Solution

  • Try font-family: 'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;

    So your tags$head will be:

    tags$head(
      tags$style(
        HTML(
          "#Number_out {
           font-family:  'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;
           font-size: 14px;
            }"
        )
      )
    )
    

    enter image description here

    EDIT

    In Chrome, if you right click and click on Inspect then scroll down to find relevant style elements: enter image description here

    And on bottom right you can see:

    enter image description here