打包闪亮的应用程序时将 ccs 文件存储在哪里

Where to store ccs file when packaging a shiny app

我正在用 R 打包一个闪亮的应用程序。我有五个文件

ui.R

    ui <-
    div(
        class = "navbar1",
navbarPage(
    div(tags$b("Test"), style = "color:#dcaaaa"),
           theme = "custom.css",
           tabPanel("Plot",
                    sidebarLayout(
                        sidebarPanel(
                            radioButtons("plotType", "Plot type",
                                         c("Scatter"="p", "Line"="l")
                            )
                        ),
                        mainPanel(
                            plotOutput("plot")
                        )
                    )
           ),
           tabPanel("Summary",
                    verbatimTextOutput("summary")
           ),
           navbarMenu("More",
                      tabPanel("Table",
                               DT::dataTableOutput("table")
                      ),
                      tabPanel("About",
                               fluidRow(
                                   # column(6,
                                   #        includeMarkdown("about.md")
                                   # ),
                                   column(9,
                                          img(class="img-polaroid",
                                              src=paste0("http://upload.wikimedia.org/",
                                                         "wikipedia/commons/9/92/",
                                                         "1919_Ford_Model_T_Highboy_Coupe.jpg")),
                                          tags$small(
                                              "Source: Photographed at the Bay State Antique ",
                                              "Automobile Club's July 10, 2005 show at the ",
                                              "Endicott Estate in Dedham, MA by ",
                                              a(href="http://commons.wikimedia.org/wiki/User:Sfoskett",
                                                "User:Sfoskett")
                                          )
                                   )
                               )
                      )
           )
)
)

server.R

server <- function(input, output, session) {
    output$plot <- renderPlot({
        plot(cars, type=input$plotType)
    })

    output$summary <- renderPrint({
        summary(cars)
    })

    output$table <- DT::renderDataTable({
        DT::datatable(cars)
    })
}

launchApp.R

#' launches the test shiny app
#'
#' @export launchApp
#'
#' @return shiny application object
#'
#' @example \dontrun {launchApp()}
#'
#' @import shiny 
#'


# wrapper for shiny::shinyApp()
launchApp <- function() {
  shinyApp(ui, server)
}

custom.css

.navbar1 .navbar{background-color: #2C6D26;}

.navbar1 .navbar-default .navbar-brand{color: white;}


.navbar1 .navbar-nav li a:hover, .navbar1 .navbar-nav > .active > a 
  {
    color: #fff !important;
    background-color:#2C6D26 !important;
    background-image: #fff !important;
  }

objective就是把这个app打包成MyPackage。在 http://r-pkgs.had.co.nz/inst.html 之后,我在 inst 下创建了一个文件夹 www。我组织文件如下:

| MyPackage/
    | R
        | ui.R
        | server.R
        | launchApp.R
    | inst
        | www
            | imgName.jpg
            | custom.css

图片可在 http://upload.wikimedia.org/wikipedia/commons/9/92/1919_Ford_Model_T_Highboy_Coupe.jpg 上找到 -- 将重命名为 imgName.jpg

可以构建包。 但是无法使包中的 css 和 jpg 文件正常工作。

。但是还是想不通

实现此目标的最佳方法是什么?非常感谢!

您应该为此使用 addResourcePath(就像您 link 的回答一样)。在您的包中创建一个函数,如下所示:

.onLoad <- function(...) {
  shiny::addResourcePath(
    prefix = "custom-assets", # custom prefix that will be used to reference your directory
    directoryPath = system.file("www", package = "MyPackage") # path to resource in your package
  )
}

之后您将能够参考自定义前缀以在您的应用中查找您的文件:

tags$link(href="custom-assets/styles.css", rel="stylesheet")

tags$img(class="img-polaroid", src = "custom-assets/imgName.jpg")

themenavBarPage 的参数,它在您的代码中的位置不正确。