【发布时间】:2021-07-12 16:55:52
【问题描述】:
我想了解我们如何与纯 JS(或 Jquery)交互代码的服务器部分。我准备了两个示例-基本上我想在用户单击按钮时隐藏 h4( )。第一个示例按预期工作,但第二个示例不起作用,因为 h4() 在服务器的 renderUI() 中。我知道这可以通过 observeEvent 或 shinyJS 包解决,但我更感兴趣的是让它与纯 JS 一起工作,而不是在服务器中运行 JS 代码。
示例 1
library(shiny)
ui <- fluidPage(
fluidRow(
h4("Testing"),
actionButton("Disable", "Disable Element"),
tags$script("
$(document).ready(function() {
$('#Disable').click(function(){
$('h4').css('display', 'none');
});
})")
)
)
shinyApp(ui, server = function(input, output) { })
示例 2
ui <- fluidPage(
fluidRow(
h4("Testing"),
uiOutput("myfun"),
tags$script("
$(document).ready(function() {
$('#Disable').click(function(){
$('h4').css('display', 'none');
});
})")
)
)
shinyApp(ui, server = function(input, output) {
output$myfun <- renderUI({
tagList(
actionButton("Disable", "Disable Element")
)
})
})
【问题讨论】: