UI 完全内置于 HTML 的 Shiny 应用程序 - ShinyJS display/hide 元素不起作用
Shiny app with UI entirely built in HTML - ShinyJS display/hide elements don't work
我有一个闪亮的应用程序 UI 完全用 HTML 编写,我需要具有特定 ID 的 HTML 元素为 hidden/shown,具体取决于数据是否满足特定条件。
下面是我已有的简化版本,我使用了 ShinyJS,但是函数 show() 和 hide() 在我的应用程序中没有做任何事情。
我需要做什么才能完成这项工作?如果使用 ShinyJS 不可能,那么我可以在闪亮的应用程序中使用 show/hide html 元素的另一种方法是什么?
提前致谢。
app.R
server <- function(input, output) {
library(shinyjs)
useShinyjs(html = TRUE)
data <- reactive({
return("abcde")
})
observe({
if ( data() == "abcde" ) {
hide("visibility-area")
} else {
show("visibility-area")
}
})
output$some-output <- renderPrint({
cat(data())
})
}
shinyApp(ui = htmlTemplate("www/index.html"), server)
www/index.html
<!DOCTYPE html>
<html>
<head>
<script src="shinyjs/inject.js"></script>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
<title>test</title>
</head>
<body>
<div id="visibility-area">
<p><span id="some-output" class="shiny-text-output"></span></p>
</div>
</body>
</html>
global.R
shiny::addResourcePath("shinyjs", system.file("srcjs", package = "shinyjs"))
当 运行 提供的示例时,我在 javascript 控制台中看到以下错误消息:
Uncaught ReferenceError: Shiny is not defined
来自 shinyjs/inject.js
脚本。问题是脚本试图在 Shiny
对象上调用方法,但该对象尚不存在。在加载定义该对象的 shared/shiny.js
文件之前,您已经加载了该文件。更改 <head>
中包含的顺序似乎可以解决问题。 (我最后才搬的)
<head>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
<script src="shinyjs/inject.js"></script>
<title>test</title>
</head>
我有一个闪亮的应用程序 UI 完全用 HTML 编写,我需要具有特定 ID 的 HTML 元素为 hidden/shown,具体取决于数据是否满足特定条件。
下面是我已有的简化版本,我使用了 ShinyJS,但是函数 show() 和 hide() 在我的应用程序中没有做任何事情。 我需要做什么才能完成这项工作?如果使用 ShinyJS 不可能,那么我可以在闪亮的应用程序中使用 show/hide html 元素的另一种方法是什么? 提前致谢。
app.R
server <- function(input, output) {
library(shinyjs)
useShinyjs(html = TRUE)
data <- reactive({
return("abcde")
})
observe({
if ( data() == "abcde" ) {
hide("visibility-area")
} else {
show("visibility-area")
}
})
output$some-output <- renderPrint({
cat(data())
})
}
shinyApp(ui = htmlTemplate("www/index.html"), server)
www/index.html
<!DOCTYPE html>
<html>
<head>
<script src="shinyjs/inject.js"></script>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
<title>test</title>
</head>
<body>
<div id="visibility-area">
<p><span id="some-output" class="shiny-text-output"></span></p>
</div>
</body>
</html>
global.R
shiny::addResourcePath("shinyjs", system.file("srcjs", package = "shinyjs"))
当 运行 提供的示例时,我在 javascript 控制台中看到以下错误消息:
Uncaught ReferenceError: Shiny is not defined
来自 shinyjs/inject.js
脚本。问题是脚本试图在 Shiny
对象上调用方法,但该对象尚不存在。在加载定义该对象的 shared/shiny.js
文件之前,您已经加载了该文件。更改 <head>
中包含的顺序似乎可以解决问题。 (我最后才搬的)
<head>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
<script src="shinyjs/inject.js"></script>
<title>test</title>
</head>