点击 actionButton() 后在模块化闪亮应用程序中隐藏图像

Hide image in modularized shiny app after hitting actionButton()

在下面闪亮的应用程序中,我希望在第一次加载应用程序时显示图像,然后在单击 actionButton() 后隐藏图像。

library(shiny)
library(shinyjs)
library(shinydashboard)
library(dplyr)
# Some data

sideUI <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(ns("action"),"Submit")
  )
  
}


# In this case this server not needed but using uiOuput/renderUI in real case
# sideServer <- function(id) { moduleServer(id,function(input, output, session) { })}

# Define the UI and server functions for the map
sideServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      return(btn = reactive(input$action))
    })
}
imgUI <- function(id) {
  ns <- NS(id)
  
  tagList(
    div(
      id = "showimg", 
      img(src='apps.png', align = "center"))
  )
}

textServer <- function(id, btn) {
  moduleServer(
    id,
    function(input, output, session) {
      observeEvent(btn(), {
        shinyjs::hide(id = "showimg")
      })
    })
  
  
}
# Build ui & server and then run
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(sideUI("side")),
  dashboardBody(useShinyjs(),imgUI("imgPL"))
)
server <- function(input, output, session) {
  
  # use the reactive in another module
  city_input <- sideServer("side")
  textServer("imgPL", btn = city_input$btn)
  
}
shinyApp(ui, server)    

您忘记将 div-id 包裹在 ns() 周围。

library(shiny)
library(shinyjs)
library(shinydashboard)
library(dplyr)
# Some data

sideUI <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(ns("action"),"Submit")
  )
  
}


# In this case this server not needed but using uiOuput/renderUI in real case
# sideServer <- function(id) { moduleServer(id,function(input, output, session) { })}

# Define the UI and server functions for the map
sideServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      return( 
        list(
          btn = reactive(input$action)
        ) 
      )
    })
}
imgUI <- function(id) {
  ns <- NS(id)
  
  tagList(
    div(
      id = ns("showimg"),  # <-------------------- don't forget this for UI elemtns in modules
      img(src='img.png', align = "center"))
  )
}

textServer <- function(id, btn) {
  moduleServer(
    id,
    function(input, output, session) {
      observeEvent(btn(), {
        shinyjs::hide(id = "showimg")
      })
    }
  )
}

    
    
# Build ui & server and then run
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(sideUI("side")),
  dashboardBody(
      useShinyjs(),
      imgUI("imgPL")
    
    )
)
server <- function(input, output, session) {
  
  # use the reactive in another module
  city_input <- sideServer("side")
  
  textServer("imgPL", btn = city_input$btn)
  
}
shinyApp(ui, server)