首页 > 解决方案 > 闪亮的应用程序:`shinyjs`解析错误,但脚本作为`script`标签工作

问题描述

我用视频流制作了一个简单的 Shiny 应用程序(如果可以,使用笔记本电脑的摄像头)。整个Javascript是:

const player = document.getElementById('player');

const constraints = {
    video: true
};

// Attach the video stream to the video element and autoplay.
navigator.mediaDevices.getUserMedia(constraints)
 .then((stream) => {
   player.srcObject = stream;
});

如果我把它包装在这样一个闪亮的应用程序中,

ui <- fluidPage(
  HTML('<video id="player" controls autoplay></video>'),
  tags$script(HTML(<<<CODE ABOVE>>>))
)
server <- function(input,output){}
shinyApp(ui,server)

它工作正常。

相反,如果我使用该shinyjs包,请将 Javascript 放在它自己的文件“startvid.js”中,并制作如下应用程序:

ui <- fluidPage(
  HTML('<video id="player" controls autoplay></video>'),

  useShinyjs(),
  extendShinyjs(script = "www/startvid.js")
)

解析时出现语法错误:

Error: shinyjs: Error parsing the JavaScript file: SyntaxError: Unexpected token >.

这里发生了什么?

标签: rshinyshinyjs

解决方案


我注意到你=>在代码中,我认为这可能是原因。

对我来说更换线路

var vector_out_it = Array.from(table_data, x => x[0]);

有线条

var vector_out_it = [];
for(it = 0; it < table_data.length; it ++) {vector_out_it.push(table_data[it][0])}

解决了问题

PS 出于某种原因,我的闪亮服务器在使用 js 胖数组运算符时遇到了困难(有关说明,请参阅 https://www.sitepoint.com/es6-arrow-functions-new-fat-concise-syntax-javascript/

它可能取决于系统配置,因为对我来说extendShinyjs在本地运行良好(仅在应用程序部署后,我收到您描述的错误:

Warning: Error in : shinyjs: Error parsing the JavaScript file: SyntaxError: Unexpected token >.

推荐阅读