首页 > 解决方案 > 切换到另一个 tabPanel 时反应性和渲染不起作用

问题描述

我在下面有一个可重现的示例,其中只有第一个tabPanel有效,但是当我切换到另一个面板时,我没有得到任何渲染(切换也变得不可交互)。我已经调查过了,conditionalPanel但是我看到它们在没有使用的情况下完成了。欢迎任何帮助!mainPanelmainPanel


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)

在此处输入图像描述 在此处输入图像描述

标签: rshinyshiny-servershinyappsshiny-reactivity

解决方案


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

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


推荐阅读