【发布时间】:2021-07-04 11:04:00
【问题描述】:
似乎 Shiny 无法识别我的 .js 文件。问题是为什么?
内联js脚本文本运行流畅:
library(shiny)
header = dashboardHeader(disable = TRUE)
sidebar = dashboardSidebar(disable = TRUE)
body = dashboardBody(
shiny::tags$script(
HTML("document.body.style.backgroundColor = 'skyblue';")),
)
ui = dashboardPage(header = header, sidebar = sidebar, body = body)
server = function(input, output, session){
}
shinyApp(ui, server)
但是当我嵌入我的 .js 文件时(myscript.js 存储在 www 子目录中)
document.body.style.backgroundColor = "skyblue";
$("firstinput").on("keypress", function(){
alert("Pressed");
})
$(document).on('shiny:connected', function(event) {
alert('Connected to the server');
})
...像这样:
library(shiny)
header = dashboardHeader(disable = TRUE)
sidebar = dashboardSidebar(disable = TRUE)
body = dashboardBody(
shiny::tags$head(
shiny::tags$script(
src = "myscript.js"
)),
HTML('<input type="button" id="firstinput">')
)
ui = dashboardPage(header = header, sidebar = sidebar, body = body)
server = function(input, output, session){
}
shinyApp(ui, server)
什么都没应用...怎么会?
【问题讨论】:
-
请注意,
$("firstinput")应改为$("#firstinput")。但我不认为这是问题的原因。您的代码应该可以工作。您是否在浏览器控制台中收到错误消息? -
你的权利,我已经尝试过
$("#firstinput"),但它不起作用。当我使用tags$script(src = "myscript.js")而不是tags$head时,document.body.style.backgroundColor = "skyblue";和$(document).on('shiny:connected', function(event) {alert('Connected to the server');})正在运行
标签: javascript html r shiny