【问题标题】:Including js/css scripts in Shiny app在 Shiny 应用程序中包含 js/css 脚本
【发布时间】:2014-09-12 22:50:56
【问题描述】:

我正在构建一个 Shiny 应用程序,但由于它应该获取用于构建绘图的数据的参数,所以我决定将我的应用程序保存为一个函数(使用本教程:http://shiny.rstudio.com/articles/function.html)。

除了我想包含的 javascript 和样式表之外,一切都正常工作(当我有标准结构时,它在早期工作:ui.R、server.R 和包含的 js/css 文件位于“www”文件夹中)。

我试图: - 像以前一样在“www”文件夹中添加文件([script.R,www 文件夹:[style.css,script.js]]) - 将文件添加到单独的文件夹中,但与启动 Shiny 应用程序的脚本在同一目录中([script.R,scripts 文件夹:[style.css,script.js]]) - 将文件添加到与启动 Shiny 应用程序的脚本相同的目录中 ([script.R, style.css, script.js])

为了包含文件,我使用如下代码: 标签$head(tags$script(src="graph.js"))

当您将 Shinny 应用程序保存为函数时,您对如何包含脚本有什么建议吗? 提前致谢!

【问题讨论】:

    标签: javascript css r tags shiny


    【解决方案1】:

    对于任何可能遇到相同问题的人,我终于想出了解决方案 :-) Shiny 中有“包含”功能,可让您指定具有绝对/相对路径的文件。这是参考:http://shiny.rstudio.com/reference/shiny/latest/include.html

    这是我的示例代码:

    app <- function(data)
    {
     shinyApp(
      ui = fluidPage(
       fluidRow(
        # I created a 'www' folder that was included
        # in the package that is launching Shiny app
        tags$head(includeScript(system.file('www', 'script.js', package = 'myPackage'))),
        tags$head(includeCSS(system.file('www', 'style.css', package = 'myPackage'))),
        # some UI stuff
       )
      ),
      server = function(input, output, session) {
       # some server stuff
      }
    }
    

    【讨论】:

      【解决方案2】:

      现在不同了。 includeScript 不需要system.file

      所以: tags$head(includeScript(system.file('www', 'script.js', package = 'myPackage')))

      将是:

      tags$head(includeScript('www/script.js', 'type' = 'text/javascript', 'data-unique-tag' = 'unique'))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-14
        • 2017-02-23
        • 2020-04-01
        • 2018-03-28
        • 2014-06-29
        • 1970-01-01
        相关资源
        最近更新 更多