在闪亮的应用中包含自定义css

时间:2017-12-29 07:23:11

标签: r shiny

我有以下闪亮的应用

ui.R

data("iris")
library(ggplot2)
library(dplyr)

ui <- fluidPage(theme = "mystyle.css",
  tabsetPanel(
    tabPanel("Tab 1", "Hello"),
    div(id = "box1", style="color:#0000FF", selectInput("boxplot1", "Choose a dataset:",
                choices = c(list("setosa", "versicolor", "virginica")))),
    div(id = "box2", selectInput("boxplot1", "Choose a dataset:",
                choices = c(list("setosa", "versicolor", "virginica"))))
  ),
  mainPanel(plotOutput("plot_boxplot"))
)

server.R

library(shiny)
library(datasets)

shinyServer(function(input, output) {

  filtered <- reactive({
    iris %>%
      filter(Sepal.Length >= 0)
  })

  output$plot_boxplot <- renderPlot({
    ggplot(filtered(), aes(x=Species, y=Sepal.Length)) + 
      geom_point(size = 4) + 
      geom_boxplot() +
      ylab("Sepal Length")  +
      stat_summary(fun.y=mean, geom="point", shape=5, size=4) 
  })


  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
})

一切正常。但是我想包含一些自定义CSS。

如果我拿到我的第一个输入框,将其包装成div语句并包含一些像这样的.css:

    div(id = "box1", style="color:#0000FF", selectInput("boxplot1", "Choose a dataset:",
            choices = c(list("setosa", "versicolor", "virginica")))),

有效。但是,出于组织目的,我想将其存储为单独的.css文件。如果我在我的www文件夹中创建并包含一个链接:

ui <- fluidPage(theme = "mystyle.css",

并将以下内容添加到文件mystyle.css:

#box1{
 width: 20%;
 height: 100px;
 style="color:#0000FF";
}

我没有得到相同的结果。检查所有代码:

mystyle.css

#box1{
 width: 20%;
 height: 100px;
 style="color:#0000FF";
}

ui.R

data("iris")
library(ggplot2)
library(dplyr)

ui <- fluidPage(theme = "mystyle.css",
  tabsetPanel(
    tabPanel("Tab 1", "Hello"),
    div(id = "box1", selectInput("boxplot1", "Choose a dataset:",
                choices = c(list("setosa", "versicolor", "virginica")))),
    div(id = "box2", selectInput("boxplot1", "Choose a dataset:",
                choices = c(list("setosa", "versicolor", "virginica"))))
  ),
  mainPanel(plotOutput("plot_boxplot"))
)

server.R

library(shiny)
library(datasets)

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {

  filtered <- reactive({
    iris %>%
      filter(Sepal.Length >= 0)
  })

  output$plot_boxplot <- renderPlot({
    ggplot(filtered(), aes(x=Species, y=Sepal.Length)) + 
      geom_point(size = 4) + 
      geom_boxplot() +
      ylab("Sepal Length")  +
      stat_summary(fun.y=mean, geom="point", shape=5, size=4) 
  })


  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
})

对我在这里做错了什么的想法?

1 个答案:

答案 0 :(得分:2)

您是否尝试使用IncludeCSS(),如建议的here? 是不是你的.CSS文件没有按照shiny's tutorial的建议保存在 www 文件夹中?