在这里,我想在模块化闪亮应用程序中单击操作按钮后隐藏最初显示的框和数据表,但只显示框,然后保持稳定。
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
}
)
}
一些事情:
btn = reactive(input$action))
包裹在 list()
中的 sideServer.R
中(否则 btn_input
不是包含名为 btn
的元素的列表)。 或者,在 btn_input
中传递 btn_input$btn
而不是 app.R
。 您需要执行其中一个操作[这与您上一个问题中的问题相同]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"))
}
)
}
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
)
)
)
}