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

六月ゝ 毕业季﹏ 提交于 2020-01-23 03:54:05

问题


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.

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.

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


回答1:


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;
        }"
    )
  )
)

EDIT

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

And on bottom right you can see:



来源:https://stackoverflow.com/questions/58454087/how-to-change-the-font-family-of-verbatimtextoutput-to-be-the-same-as-the-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!