单击actionButton()后隐藏模块化闪亮应用程序中最初显示的框和表格

问题描述 投票:0回答:1

在这里,我想在模块化闪亮应用程序中单击操作按钮后隐藏最初显示的框和数据表,但只显示框,然后保持稳定。

app.r

library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)

# Load the modules
source("sideUI.R")
source("sideServer.R")
source("tabUI.R")
source("tabServer.R")

# Build UI & server and then run the app
ui <- dashboardPage(
  dashboardHeader(title = "Text Hiding Example"),
  dashboardSidebar(sideUI("side")),  # Sidebar with the action button
  dashboardBody(
    useShinyjs(),  # Initialize shinyjs
    tabUI("tabPL")  # Table UI module
  )
)

server <- function(input, output, session) {
  # Use the reactive in another module
  btn_input <- sideServer("side")
  tabServer("tabPL", btn = btn_input$btn)
}

shinyApp(ui, server)

tabUI.R

tabUI <- function(id) {
  ns <- NS(id)
  
  tagList(
    div(
      id = ns("showtab"),
      box(
        title = h3("Check our data", style = 'font-size:20px;color:white;
                     font-family: "Georgia", Times, "Times New Roman", serif;'), 
        status = "primary", 
        solidHeader = TRUE,
        width=12,
        height = 430,
        collapsible = TRUE,
        tagList(
          dataTableOutput(ns("table"),height = 400)))
    )
  )
}

tabServer.R

tabServer <- function(id, btn) {
  moduleServer(
    id,
    function(input, output, session) {
      ns <- session$ns  # Namespace function
      
      # Observe button click event
      observeEvent(btn(), {
        output$table<-renderDataTable({
          datatable(iris)
        })
        
        shinyjs::hide("showtab")  # Hide the text with correct namespace
      })
    }
  )
}

sideUI.R

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

sideServer.R

sideServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      return(btn = reactive(input$action))  # Return the button input as reactive
    }
  )
}
r shiny module dt box
1个回答
0
投票

一些事情:

  1. 您必须将
    btn = reactive(input$action))
    包裹在
    list()
    中的
    sideServer.R
    中(否则
    btn_input
    不是包含名为
    btn
    的元素的列表)。 或者,在
    btn_input
    中传递
    btn_input$btn
    而不是
    app.R
    。 您需要执行其中一个操作[这与您上一个问题中的问题相同]

2. 使用`DT::renderDT()`和`DT::DTOutput()`,而不是`shiny::datatableOutput()`和`shiny::renderDataTable()`
  1. tabServer.R()
    应该这样重写:
tabServer <- function(id, btn) {
  moduleServer(
    id,
    function(input, output, session) {
      output$table<-DT::renderDT({DT::datatable(iris)})
      # Observe button click event
      observeEvent(btn(), shinyjs::hide("showtab"))
    }
  )
}
  1. tabUI.R
    应调整:
tabUI <- function(id) {
  ns <- NS(id)
  
  tagList(
    div(
      id = ns("showtab"),
      box(
        DT::DTOutput(ns("table")),
        title = h3("Check our data", style = 'font-size:20px;color:white;
                     font-family: "Georgia", Times, "Times New Roman", serif;'), 
        status = "primary", 
        solidHeader = TRUE,
        width=12,
        height = 430,
        collapsible = TRUE
      )
    )
  )
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.