簡體   English   中英

在R閃亮中添加一個CSS樣式表

[英]adding a CSS Stylesheet in 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

我在沒有shinyUI的情況下單獨使用fluidPage ,但沒有一個選項有效。 我已經確認我的工作目錄和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樣式表與此命令鏈接,因此我不會更改我的應用程序的外觀。

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>

來自@esteban:

我已經解決了這個問題,我將文件重命名為app.R而不是<mytestapp>.R和RStudio以不同方式識別它並且能夠加載.css文件。 我找到的另一個替代方法是使用install.packages("shinythemes")安裝R-package shinythemes主題,並在fluidPage定義主題,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM