Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在应用程序中包含一个javascript文件_Javascript_R_Shiny - Fatal编程技术网

在应用程序中包含一个javascript文件

在应用程序中包含一个javascript文件,javascript,r,shiny,Javascript,R,Shiny,我需要在我闪亮的应用程序中包含一个js库。目前,我使用includeHTML将脚本直接包含到html代码中。e、 g includeHTML('URL.js') 如果我使用标签$script,当我尝试浏览js文件时,浏览器将显示“未找到”,例如 http://127.0.0.1:7106/URL.js tags$script(src = 'URL.js') 现在我把URL.js放在ui.r和server.r的同一个文件夹中 我应该在哪里存储URL.js文件?或者是否有其他方式包含js文件

我需要在我闪亮的应用程序中包含一个js库。目前,我使用includeHTML将脚本直接包含到html代码中。e、 g

includeHTML('URL.js')
如果我使用标签$script,当我尝试浏览js文件时,浏览器将显示“未找到”,例如

http://127.0.0.1:7106/URL.js

tags$script(src = 'URL.js')
现在我把URL.js放在ui.r和server.r的同一个文件夹中

我应该在哪里存储URL.js文件?或者是否有其他方式包含js文件


谢谢您的建议。

您需要做的是:

  • 在与
    server.R
    ui.R
    相同的文件夹中创建
    www
    文件夹
  • 将javascript文件放入
    www
    文件夹
  • 在UI中放置标签$head(标签$script(src=“hoge.js”)
  • 该文件夹看起来像:

    ├── server.R
    ├── ui.R
    └── www
        └── hoge.js
    
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      ... snip ...
      <script src="shared/slider/js/jquery.slider.min.js"></script>
      <script src="hoge.js"></script>
    </head>
    
    ui.R的
    ui.R
    类似于

    library(shiny)
    shinyUI(pageWithSidebar(
      headerPanel("New Application"),
      sidebarPanel(
        sliderInput("obs", 
                    "Number of observations:", 
                    min = 1, 
                    max = 1000, 
                    value = 500)
      ),
      mainPanel(
        plotOutput("distPlot"),
        tags$head(tags$script(src="hoge.js"))
      )
    ))
    
    server.R

    library(shiny)
    shinyServer(function(input, output) {
      output$distPlot <- renderPlot({
        dist <- rnorm(input$obs)
        hist(dist)
      })
    })
    
    用户界面 其中任何一个都可以

    1. tags$head(HTML("<script type='text/javascript' src='js/hoge.js'></script>"))
    
    2. HTML('<head>
                  <link rel="stylesheet" type="text/css" href="stylesheet.css">
                  <script type="text/javascript" src="js/hoge.js"></script>
              </head>')
    
    1。标签$head(HTML(“”)
    2.HTML('
    ')
    
    另一种方法是使用:

    includeScript("mapManipulator.js"),
    

    另一个尚未讨论的选项是,只需完全删除ui.R文件,然后将整个内容编码为自定义HTML文件。这里的细节


    在本文中,默认的HTML表单元素会自动用作server.R中的输入,但您也可以使用本指南为shiny构建自定义输入(或输出)元素

    我的首选方式如下:

    ui.R:

    app.js:

    shinyjs.alerta = function(text){
      alert(text);
    }
    
    服务器.R

      js$alerta("alerta alerta antifascista")
    
    您还可以包含如下代码:

    库导入后的ui.R:

    jsCode <- "shinyjs.alerta = function(text){alert(text);}"
    

    来自server.R的调用将是相同的

    或使用
    includeScript
    ,它接受一个参数
    path
    作为应用程序目录的基本路径
    IncludeDescript
    但是会将文件中的javascript放入内联。谢谢@kohske。这正是我所需要的。你认为有可能对一个闪亮的应用程序做出广告反应吗?有可能在所有脚本的第一个位置添加脚本,然后再添加所有包脚本吗?例如,我想制作一张有很多层的传单地图,所以我需要添加
    L_preference_CANVAS=true
    之前添加code>,但您提供的解决方案会在末尾添加脚本
      js$alerta("alerta alerta antifascista")
    
    jsCode <- "shinyjs.alerta = function(text){alert(text);}"
    
    extendShinyjs(text = jsCode, functions = c("alerta")),