切换到另一个 tabPanel 时,反应性和渲染不起作用

Reactivity and Renders doesn't work when switching to another tabPanel

我在下面有一个可重现的示例,其中只有第一个 tabPanel 在工作,但是当我切换到另一个面板时,我没有得到任何渲染(切换也变得不可交互)。我已经研究了 conditionalPanel 但是我看到他们在不使用 mainPanel 的情况下完成了我想知道是否可以有标签,其中每个标签都有自己的 mainPanel ,所以我可以看到不同的侧边栏和包含在不同选项卡中的输出。欢迎任何帮助!


options(scipen = 99999) #converts the sci numbers to their regular format
library(shiny)
library(shinyjs)
library(shinyWidgets)
library(shinyalert)
library(esquisse)
library(DT)
library(dplyr)
#library(devtools)
#library(remotes)
#remotes::install_github("dreamRs/esquisse")
library(hrbrthemes)
library(ggthemes)
library(ggplot2)
library(svglite)

ui <- fluidPage(
  
  shinyjs::useShinyjs(), # enables javascript/jQuery enhanchments
  
  # Create Right Side Text
  navbarPage( 
    
    title= div(HTML("G<em>T</em>")),
    
    #General reports
    tabPanel("General Reports",
             
             shinyWidgets::materialSwitch(inputId = "toggleSidebar", label = "Toggle Panel: ",
                                          value = TRUE, status = "warning"),
             sidebarLayout(
               # radio/action buttons
               sidebarPanel(
                 
                 id = "Sidebar",
                 
                 shinyWidgets::prettyRadioButtons(
                   inputId = "controller",
                   label = "Choose:", 
                   choices = c("About"= 1,
                               "iris"= 2),
                   icon= icon("check"),
                   selected = 1,
                   status = "success",
                   animation="smooth"
                 ),
                 
                 br(),
                 br(),
                 a(actionButton(inputId = "admin_email", label = "Contact", 
                                icon = icon("envelope", lib = "font-awesome")),
                   href="mailto:xyz@email.us")
               ),
               
               #panel where output is shown from server
               mainPanel(
                 id = "main_panel",
                 
                 tabsetPanel(
                   id = "hidden_tabs",
                   type = "hidden",
                   tabPanelBody(
                     "panel1", "Text coming soon."
                   ),
                   
                   tabPanelBody(
                     "panel2", 
                     tabsetPanel(
                       tabPanel("Data", DT::DTOutput('panel2_data')),
                       tabPanel(
                         "DIY Plot",
                         esquisse::esquisse_ui(
                           id = "esquisse2",
                           header = FALSE,
                           container = esquisseContainer(
                             width = "100%", height = "760px", fixed = FALSE
                           ),
                           controls = c("labs", "parameters", "appearance", "filters", "code")
                         )
                       )
                     )
                   )
                 )
               )
             )
    ),
    
    # monthly reports
    tabPanel("Extra General Reports",
             
             shinyWidgets::materialSwitch(inputId = "toggleSidebar", label = "Toggle Panel: ",
                                          value = TRUE, status = "warning"),
             sidebarLayout(
               # radio/action buttons
               sidebarPanel(
                 
                 id = "Sidebar",
                 
                 shinyWidgets::prettyRadioButtons(
                   inputId = "controller",
                   label = "Choose:", 
                   choices = c("About"= 3,
                               "mtcars"= 4),
                   icon= icon("check"),
                   selected = 3,
                   status = "success",
                   animation="smooth"
                 ),
                 
                 br(),
                 br(),
                 a(actionButton(inputId = "admin_email", label = "Contact", 
                                icon = icon("envelope", lib = "font-awesome")),
                   href="mailto:xyz@email.us")
               ),
               
               #panel where output is shown from server
               mainPanel(
                 id = "main_panel",
                 
                 tabsetPanel(
                   id = "hidden_tabs",
                   type = "hidden",
                   tabPanelBody(
                     "panel3", "Text coming soon."
                   ),
                   
                   tabPanelBody(
                     "panel4", 
                     tabsetPanel(
                       tabPanel("Data", DT::DTOutput('panel4_data')),
                       tabPanel(
                         "DIY Plot",
                         esquisse::esquisse_ui(
                           id = "esquisse4",
                           header = FALSE,
                           container = esquisseContainer(
                             width = "100%", height = "760px", fixed = FALSE
                           ),
                           controls = c("labs", "parameters", "appearance", "filters", "code")
                         )
                       )
                     )
                   )
                 )
               )
             )
    ),
    
    #resizes the navbar tabs/button
    tags$head(tags$style(HTML('.navbar-brand {width: 270px; font-size:35px; text-align:left;
                              font-family: "serif";')))
  )
)

server <- function(input, output, session) {
  
  # this event hides the side panel when toggled on/off
  observeEvent(input$toggleSidebar, {
    shinyjs::toggle(id = "Sidebar", condition = input$toggleSidebar)
    if(!isTRUE(input$toggleSidebar)) {
      shinyjs::runjs("$('#main_panel').removeClass('col-sm-8').addClass('col-sm-12')")
    } else {
      shinyjs::runjs("$('#main_panel').removeClass('col-sm-12').addClass('col-sm-8')")
    }
    
  })
  
  
  # here we put all the data
  data_sets <- list(df1 = data.frame(), 
                    df2 = iris,
                    df3 = data.frame(), 
                    df4 = mtcars)
  
  # store current dataset in reactive so we can work with plot panels
  data_to_use <- reactiveValues(name = "df", data = data.frame())
  
  # modules only need to be called it once but individually for esquisse

  esquisse::esquisse_server(id = "esquisse2", data_rv = data_to_use)
  esquisse::esquisse_server(id = "esquisse4", data_rv = data_to_use)

  
  observeEvent(input$controller, {
    
    # skip first panel since it is used to display navigation
    updateTabsetPanel(session, inputId= "hidden_tabs", selected = paste0("panel", input$controller))
    
    # enswure value is avilable throught selected tabSet
    req(input$controller)
    
    # get current data and df name
    data_to_use$data <- data_sets[[as.numeric(input$controller)]]
    data_to_use$name <- names(data_sets[as.numeric(input$controller)])
    
    # update table and sum. Use server = FALSE to get full table
    output[[paste0('panel',  input$controller, '_data')]] <- DT::renderDT(server = FALSE, {
      DT::datatable(data_to_use$data,
                    filter = 'top', 
                    extensions = 'Buttons')})
    
    
  })

}


#runs the app
shinyApp(ui= ui, server= server)

您有两个 radioButtons,每个边栏一个,但它们都有 inputId = "controller"。与 inputId = "toggleSidebar" 相同。 InputIds 需要在 shiny 中是唯一的!

我建议您为整个应用程序使用一个侧边栏,或者由于两个选项卡本质上相同,您也可以使用 modules