我确信我的
useShinyJS()
放置位置不正确,但我尝试了多个位置,但就是无法让它工作。在下面的代码中,当点击 Run
时,它应该 hide()
两个元素 10 秒,然后使它们再次可见。什么事情都没有发生,都是因为放置的问题,我应该放置在哪里useShinyJS()
?
library(shiny)
library(shinyWidgets)
library(shinyjs)
library(bslib)
library(DT)
mod_ui <- function(id){
ns <- NS(id)
tagList(
useShinyjs(),
layout_sidebar(
sidebar = sidebar(
div(id = "b", pickerInput(ns("test_button"), choices = c("A", "B", "C"))),
actionButton(ns("generate_graphs"), "Run")
),
div(id = "a", card(DTOutput(NS(id, "table_mainpanel"))))
)
)
}
mod_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
observeEvent(input$generate_graphs,
{
print("Hiding")
shinyjs::hide(id = "a")
shinyjs::hide(id = "b")
print("Sleeping")
Sys.sleep(10)
shinyjs::show(id = "a")
shinyjs::show(id = "b")
print("Done")
})
}
)}
app <- function(){
ui <- page_fluid(
page_navbar(
title = "Test",
collapsible = TRUE,
id = "navbar",
fillable = "Dashboard",
nav_menu(
'Data section',
nav_panel('Upload Data', mod_ui("upload_data")),
)))
server <- function(session, input, output){
mod_server("upload_data")
}
shinyApp(ui, server)
}
app()
您需要将
asis
参数设置为 TRUE
:
library(shiny)
library(shinyWidgets)
library(shinyjs)
library(bslib)
library(DT)
mod_ui <- function(id) {
ns <- NS(id)
tagList(layout_sidebar(sidebar = sidebar(
div(id = "b", pickerInput(
ns("test_button"), choices = c("A", "B", "C")
)), actionButton(ns("generate_graphs"), "Run")
), div(id = "a", card(DTOutput(
NS(id, "table_mainpanel")
)))))
}
mod_server <- function(id) {
moduleServer(id, function(input, output, session) {
elements_hidden <- reactiveVal(NULL)
observeEvent(input$generate_graphs, {
print("Hiding")
shinyjs::hide(id = "a", asis = TRUE)
shinyjs::hide(id = "b", asis = TRUE)
elements_hidden(Sys.time())
})
observe({
req(elements_hidden())
if((Sys.time() - elements_hidden()) < 10L){
invalidateLater(500L)
print("...sleeping...")
} else {
shinyjs::show(id = "a", asis = TRUE)
shinyjs::show(id = "b", asis = TRUE)
print("Done")
}
})
})
}
shinyApp(ui = page_fluid(
useShinyjs(),
page_navbar(
title = "Test",
collapsible = TRUE,
id = "navbar",
fillable = "Dashboard",
nav_menu('Data section', nav_panel('Upload Data', mod_ui("upload_data")), )
)
), server = function(session, input, output) {
mod_server("upload_data")
})
关于您在
observeEvent
内的方法,请参阅此相关答案。