css - 仅在 Shiny 仪表板的特定 tabItem 上应用 css 格式

标签 css r shiny shinydashboard

我有下面的 Shiny 应用程序,我想仅在 Shiny 仪表板的特定 tabItem 上应用 css 格式,但它适用于两者。如何指定它仅在 1 日应用?

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(menuItem("Welcome", tabName = "tab1", icon = icon("house")),
                   menuItem("Information", tabName = "tab2", icon = icon("table"))),
  dashboardBody(
    tabItems(
      tabItem("tab1",
              tags$head(tags$style(HTML('
      
  body{
  font-size: 12pt;
  font-family: "Montserrat Light", sans-serif;
  text-align: justify;
  background-color: linen;
}
  H1.title{
  font-size: 44pt;
  font-family: "Chronicle Display Light", Times, serif;
  text-align: right;
  background-color: linen;
}
  H1{
  font-size: 44pt;
  font-family: "Chronicle Display Light", Times, serif;
  text-align: right;
  background-color: linen;
}
  H2{
  font-size: 16pt;
  font-weight: bold;
  font-family: "Chronicle Display Light", Times, serif;
  text-align: left;
  background-color: linen;
}

    '))),
              fluidRow(column(3,h3("Concent"))),
              tags$hr(),
              fluidRow(column(3,h5(strong("Investigators")))),
              fluidRow(column(9,"The investigators of this project are:")),
              fluidRow(column(9,"Dr Adam Hodgkins","(",tags$a (href="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="48292c29250820272c2f2321263b662b272566293d" rel="noreferrer noopener nofollow">[email protected]</a>","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026366636f426a6d6665696b6c712c616d6f2c6377" rel="noreferrer noopener nofollow">[email protected]</a>"),")")),
              fluidRow(column(9,"Dr Hodgkins can be contacted by telephone on 0414 296 699. ")),
              tags$hr(),
              fluidRow(column(3,h5(strong("Consent")))),
              fluidRow(column(12,"The practice owners have been given information about the research project titled “Life, death and statins: Survival analysis of elderly general practice patients in relation to statin prescriptions.”")),
              fluidRow(column(12,"The practice owners have been provided the opportunity to discuss the research with the investigators who are conducting this research as part of the University of Wollongong. ")),
              fluidRow(column(12,"The practice owners have been advised of any possible risks or burdens associated with this research and have had the opportunity to ask the investigators any questions they may have about the research and my participation.


")),
              tags$hr(),
              fluidRow(column(12,"I understand our practice’s participation is voluntary, our practice is free to choose not to participate and is free to withdraw from the research at any time. Our practice’s choice to not participate or to withdraw consent will not affect its relationship with the researchers or the University of Wollongong. 


"))
              ),
      tabItem("tab2",
              fluidRow(column(3,h3("Concent"))),
              tags$hr(),
              fluidRow(column(3,h5(strong("Investigators")))),
              fluidRow(column(9,"The investigators of this project are:")),
              fluidRow(column(9,"Dr Adam Hodgkins","(",tags$a (href="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddbcb9bcb09db5b2b9bab6b4b3aef3beb2b0f3bca8" rel="noreferrer noopener nofollow">[email protected]</a>","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f39297929eb39b9c9794989a9d80dd909c9edd9286" rel="noreferrer noopener nofollow">[email protected]</a>"),")")),
              fluidRow(column(9,"Dr Hodgkins can be contacted by telephone on 0414 296 699. ")),
              tags$hr(),
              fluidRow(column(3,h5(strong("Consent")))),
              fluidRow(column(12,"The practice owners have been given information about the research project titled “Life, death and statins: Survival analysis of elderly general practice patients in relation to statin prescriptions.”")),
              fluidRow(column(12,"The practice owners have been provided the opportunity to discuss the research with the investigators who are conducting this research as part of the University of Wollongong. ")),
              fluidRow(column(12,"The practice owners have been advised of any possible risks or burdens associated with this research and have had the opportunity to ask the investigators any questions they may have about the research and my participation.


")))
    )
    
  )
)

server <- function(input, output) { }

shinyApp(ui, server)

最佳答案

尽量减少问题代码。您的 menuItem 周围还缺少一个 sidebarMenu。这里使用代码的简化版本来强调要做什么。

首先将样式标签移至仪表板主体。即使您将其放在 tabItem 中,它也始终会转到页面的头部!

要将样式限制为特定的 tabItem,请在样式表中的选择器前面添加 #shiny-tab-TABNAME(将 TABNAME 替换为您的选项卡名称)

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Welcome", tabName = "tab1", icon = icon("house")),
      menuItem("Information", tabName = "tab2", icon = icon("table"))
    )
  ),
  dashboardBody(
    tags$head(
      tags$style(
        HTML('
          #shiny-tab-tab2 h1 {
            color: red;
          }
        ')
      )
    ),
    tabItems(
      tabItem(
        "tab1", tags$h1('TAB1')
      ),
      tabItem(
        "tab2", tags$h1('TAB2')
      )
    )
    
  )
)

server <- function(input, output) {
  
}

shinyApp(ui, server)

关于css - 仅在 Shiny 仪表板的特定 tabItem 上应用 css 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65510715/

相关文章:

r - 合并两个表并为 R 中的每个表添加标题

r - 为什么 R 报告相同函数的命名空间冲突?

windows - 部署 R Shiny App 时的可移植浏览器问题

css - Webpack scss 加载器、css 加载器和 post css

html - css 表格中心在 Firefox 中不工作

r - dplyr “Select”-错误:找到重复的列名

r - 让 R Shiny DataTables 最小示例在 RMarkdown 文档中工作

javascript - 将 CSS 类应用于所有行但不应用于表标题

php - 试图使页眉背景图像出现在某些页面上

r - 如何将 geom_point 添加到 stat_density_ridges