【问题标题】:adding a CSS Stylesheet in R shiny在 R 中添加 CSS 样式表
【发布时间】:2015-10-24 20:31:28
【问题描述】:

我正在制作我的第一个闪亮的应用程序,但在链接外部 .css 文件时遇到问题。我看过一些教程和参考资料,人们解释了如何做,甚至展示了示例代码,但我没有任何运气。我见过的大多数示例都使用了 shinyUI 或 fluidPage 函数,例如使用主题:

shinyUI(fluidPage(theme = "bootstrap.css",
       headerPanel("New Application"),
       sidebarPanel(
              sliderInput("obs", "Number of observations:", 
              min = 1, max = 1000, value = 500)
                   ),
       mainPanel(plotOutput("distPlot"))
                )
       )

或者这个使用tags$link:

shinyUI(fluidPage(
       tags$head(
                tags$link(rel = "stylesheet", type = "text/css", href = "bootstrap.css")
                ),
       headerPanel("New Application")
                 )
       )

或使用includeCSS

我单独使用fluidPage,没有shinyUI,但没有一个选项有效。我已经确认我的工作目录和 app-Directory 是我认为应该在的位置,并且包含保存 .css 文件的“www”子目录。唯一可行的是,如果我在我的tags$head 中添加一个tags$style 和一个HTML,如下所示:

fluidPage(
         tags$head(
             tags$style(
                       HTML(
                           "h1 {color:purple;}
                           .blue-item {color:blue;}
                           #dark {color:navy;}"
                            )
                       )
                   )
         )

但这并没有解决问题,因为我没有用这个命令链接 CSS 样式表,因此我没有改变我的应用程序的外观。

【问题讨论】:

  • This 文章可能对您感兴趣。
  • 检查您的bootstrap.css。我下载了一个,它并没有改变 UI 默认值,即使它被正确加载。改变一些,例如body{background-color;},我的是#fff,不像官方教程。编辑 css 后,更改实际上就在那里。
  • 我已经解决了这个问题,我将文件重命名为 app.R 而不是 .R 并且 RStudio 以不同的方式识别它并且能够加载 .css 文件。我发现的另一种选择是使用 install.packages("shinythemes") 安装 R-package 'shinythemes',并在 fluidPage 中定义主题如下:fluidPage(theme = shinytheme("cerulean"), tabsetPanel())

标签: html css r shiny


【解决方案1】:
To get CSS into your Shiny App, you 

Add style sheets with the www directory
Add CSS to your HTML header
Add styling directly to HTML tags

链接到样式表文件

<!DOCTYPE html>
<html>
  <head>
    <link type="text/css" rel="stylesheet" href="bootstrap.css"/>
  </head>
  <body>
  </body>
</html>

【讨论】:

    【解决方案2】:

    来自@esteban:

    我已经解决了这个问题,我将文件重命名为 app.R 而不是 &lt;mytestapp&gt;.R 并且 RStudio 以不同的方式识别它并且能够加载 .css 文件。我发现的另一种选择是使用install.packages("shinythemes") 安装R-package shinythemes 并在fluidPage 中定义主题,如下所示:

    fluidPage(
        theme = shinytheme("cerulean"),
        ### UI CODE HERE ###
    )
    

    【讨论】:

    • 将问题标记为已回答。
    猜你喜欢
    • 2015-08-05
    • 2013-04-20
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多