【问题标题】:How to change the font family of verbatimTextOutput to be the same as the input in Shiny and Shinydashboard?如何将verbatimTextOutput的字体系列更改为与Shiny和Shinydashboard中的输入相同?
【发布时间】:2020-02-15 15:24:12
【问题描述】:

我想将verbatimTextOutput 的字体系列更改为与Shiny 和Shinydashboard 中的输入相同。这是一个例子。

# 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)

通过运行应用程序并输入一个数字,我们可以看到numericInputverbatimTextOutput 中的字体系列是不同的。

根据这个答案 (https://stackoverflow.com/a/48037443/7669809) 和这个答案 (https://stackoverflow.com/a/50784117/7669809),我编辑了我的脚本如下。

# 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)

但字体系列还是不一样。

看来我还没有使用正确的字体系列。请告诉我如何实现这一目标。

【问题讨论】:

    标签: html css r shiny shinydashboard


    【解决方案1】:

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

    所以你的tags$head 将是:

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

    编辑

    在 Chrome 中,如果您右键单击并单击 Inspect 然后向下滚动以查找相关样式元素:

    在右下角你可以看到:

    【讨论】:

    • 我刚刚编辑了答案以找出字体信息
    • 再次感谢。 Stack Overflow 不允许我立即奖励你奖金。当我被允许这样做时,我会这样做。
    猜你喜欢
    • 2020-02-23
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多