我正在创建我的第一个闪亮的应用程序,并且无法链接外部.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样式表,因此我不会更改我的应用程序的外观。
答案 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 ###
)