在R闪亮中添加一个CSS样式表

时间:2015-08-02 18:45:08

标签: html css r shiny

我正在创建我的第一个闪亮的应用程序,并且无法链接外部.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$style内添加tags$head和HTML,就像这样:

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

但它没有解决问题,因为我没有使用此命令链接CSS样式表,因此我不会更改我的应用程序的外观。

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

来自@esteban:

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

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