【问题标题】:Navigate in the same dynamic tabPanel based on if condition in shiny app根据闪亮应用中的 if 条件在同一动态 tabPanel 中导航
【发布时间】:2020-04-22 14:30:32
【问题描述】:

我有下面的闪亮应用程序,我在其中基于数据框的一列创建选项卡面板。然后根据选择的单选按钮,我显示irismtcars 数据集的表。

问题是,例如,如果我在mtcars 数据集的Table 模式下并按Plot 模式,我想留在mtcars 面板并查看mtcars 绘图而不是返回iris 面板。我怎样才能做到这一点?

Uni<-data.frame(NAME=c("Iris","Mtcars"))

# app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(DT)
dbHeader <- dashboardHeaderPlus(

  title = "Tabs"
)

ui <- dashboardPagePlus(
  dbHeader,
  dashboardSidebar(
    uiOutput("r")
  ),
  dashboardBody(
    useShinyjs(),
    tags$hr(),
    tabsetPanel(
      id ="tabA",
      type = "tabs",
      tabPanel("Front",icon = icon("accusoft")),
      tabPanel("Data", icon = icon("table"),
               uiOutput("dyntab")

      )
    )
  )
)

server <- function(input, output) {
  output$dyntab<-renderUI({
    do.call(tabsetPanel, 
            c(id='tabB', 
              type="tabs",
              lapply(1:nrow(Uni), function(i) {
                tabPanel(Uni[i,],icon = icon("table"),
                         if(input$radioV2=="Table"){
                           renderDataTable({
                             if(input$tabB=="Iris"){
                               datatable(iris)
                             }
                             else{
                               datatable(mtcars)
                             }
                           })

                         }
                         else{
                           renderPlot({
                             if(input$tabB=="Iris"){
                               plot(iris)
                             }
                             else{
                               plot(mtcars)
                             }
                           })
                         }
                         )
              }))
    )
  })

  output$r<-renderUI({
    if(input$tabA=="Front"){
      return(NULL)
    }
    else{
      radioButtons("radioV2", label = "Choose Mode",
                   choices = c("Table","Plot"), 
                   selected = "Table")
    }

  })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:
    1. 您发生了一些事情,其中​​之一是每次更改选项卡时都会创建dyntab,现在已修复为仅在启动时呈现一次
    2. 我们将利用shinyjs 及其showhide 函数来显示radioButtons,而不是一直使用renderUI 创建它
    3. 我仍然不是 100% 在 dyntab 中使用上述方法,如您所见,我必须为 div 创建 id 以便 showhide 它发生这种情况因为它会随机分配id给您正在呈现的表格和图表
    4. 我还利用 hidden 函数在启动时隐藏 div

    Uni <- data.frame(NAME=c("Iris","Mtcars"))
    options(stringsAsFactors = F)
    
    # app.R ##
    library(shiny)
    library(shinydashboard)
    library(shinydashboardPlus)
    library(shinyjs)
    library(DT)
    dbHeader <- dashboardHeaderPlus(
        title = "Tabs"
    )
    
    ui <- dashboardPagePlus(
        dbHeader,
        dashboardSidebar(
            hidden(
                radioButtons("radioV2", label = "Choose Mode",choices = c("Table","Plot"), selected = "Table")
            )
        ),
        dashboardBody(
            useShinyjs(),
            tags$hr(),
            tabsetPanel(
                id ="tabA",
                type = "tabs",
                tabPanel("Front",icon = icon("accusoft")),
                tabPanel("Data", icon = icon("table"), uiOutput("dyntab")
    
                )
            )
        )
    )
    
    server <- function(input, output, session) {
    
        observeEvent(input$tabA,{
            if(input$tabA == "Front"){
                hide("radioV2")
            }
            else{
                show("radioV2")
            }
        })
    
        output$dyntab <- renderUI({
            do.call(tabsetPanel,
                    c(id='tabB',
                      type="tabs",
                      lapply(1:nrow(Uni), function(i) {
                          tabPanel(Uni[i,],icon = icon("table"),
                                   div(id = paste0("Table",Uni$NAME[i]),DT::renderDataTable({
                                       if(Uni$NAME[i] == "Iris"){
                                           datatable(iris)  
                                       }else{
                                           datatable(mtcars)
                                       }
    
                                   })),
                                   hidden(div(id = paste0("Plot",Uni$NAME[i]),renderPlot({
                                       if(Uni$NAME[i] == "Iris"){
                                           plot(iris)  
                                       }else{
                                           plot(mtcars)
                                       }
                                   })
                                   ))
                          )
                      })
                    )
            )
        })
    
        observeEvent(input$radioV2,{
            print(paste0(input$radioV2,input$tabB))
            if(input$radioV2 == 'Table'){
                show(paste0("Table",input$tabB))
                hide(paste0("Plot",input$tabB))
            }else{
                hide(paste0("Table",input$tabB))
                show(paste0("Plot",input$tabB))
            }
        })
    
    
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 它有效,但有没有更接近我的方法的解决方案?通过用于创建 radioV2 的 uiOutput 在其他选项卡中创建了其他小部件。我刚刚发布了分钟。需要示例,但我需要更改几乎所有代码以适应它。
    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 2021-06-27
    • 1970-01-01
    • 2016-11-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多