在Shiny应用程序中包含js/css脚本 [英] Including js/css scripts in Shiny app

查看:70
本文介绍了在Shiny应用程序中包含js/css脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Shiny应用程序,但是由于它应该获取构建绘图数据的参数,因此我决定将我的应用程序另存为一个函数(使用本教程:

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

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

在将Shinny应用程序另存为函数时,您有任何建议如何包含脚本的建议吗?预先感谢!

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

这是我的示例代码:

  app<-函数(数据){ShinyApp(ui = fluidPage(fluidRow(#我创建了一个包含了'www'文件夹启动Shiny应用程序的软件包中的#tags $ head(includeScript(system.file('www','script.js',package ='myPackage'))),tags $ head(includeCSS(system.file('www','style.css',package ='myPackage'))),#一些UI的东西)),服务器=函数(输入,输出,会话){#一些服务器的东西}} 

I am building a Shiny application, but as it should get arguments for data for building plots, I decided to save my app as a function (using this tutorial: http://shiny.rstudio.com/articles/function.html ).

Everything is working besides javascript and stylesheets that I would like to include (it was working earlier when I had standard structure: ui.R, server.R and included js/css files were in 'www' folder).

I tried to: - adding the files in the 'www' folder as before ([script.R, www folder: [style.css, script.js]]) - adding the files in separate folder, but in the same directory as the script launching the Shiny app ([script.R, scripts folder: [style.css, script.js]]) - adding the files in the same directory as my script launching the Shiny app ([script.R, style.css, script.js])

For including files I use code like: tags$head(tags$script(src="graph.js"))

Do you have any suggestions how to include scripts when you save your Shinny application as a function? Thanks in advance!

解决方案

For anyone who may be having the same issue, I finally came up with the solution :-) There are 'include' functions in Shiny that let you specify a file with the absolute/relative path. Here is the reference: http://shiny.rstudio.com/reference/shiny/latest/include.html

And this is my example code:

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
  }
}

这篇关于在Shiny应用程序中包含js/css脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆