2016-08-13 1 views
3

내 반짝이는 앱을 동적 탭으로 만들고 싶습니다. 아래 코드를 시도했습니다.조건부 패널을 사용하여 반짝이는 대시 보드에 동적 탭 추가

## app.R ## 
library(shiny) 
library(shinydashboard) 

ui <- dashboardPage(
    dashboardHeader(), 
    dashboardSidebar(
    checkboxGroupInput("Tabs", 
         label = h4("tabpanel"), 
         choices = list("tabs" = "tabs"), 
         selected = NULL), 
    checkboxGroupInput("moreTabs", 
         label = h4("moretabpanel"), 
         choices = list("moretabs" = "moretabs"), 
         selected = NULL) 
), 
    dashboardBody(
    conditionalPanel(
     condition = "input.Tabs == 'tabs'", 
     tabBox(
     title = "intro", 
     id= "ttabs", width = 8, height = "420px", 
     tabPanel("Files", dataTableOutput("Files")), 
     conditionalPanel(
      condition = "input.moreTabs == 'moretabs'", 
      tabPanel("Files1", dataTableOutput("Files1")) 
     ) 
) 
) 
) 
) 
server <- function(input, output) { } 

shinyApp(ui, server) 

그러나 동적으로 탭 패널을 사용하지 못했습니다. 그것은 단지 하나의 탭을 보여 주며, 확인시 두 번째 탭을 보여 주어야합니다.

답변

6

아래와 같이 ui을 동적으로 만들 수 있습니다.

예 1 : RenderUI

rm(list = ls()) 
library(shiny) 
library(shinydashboard) 

ui <- dashboardPage(
    dashboardHeader(), 
    dashboardSidebar(
    checkboxGroupInput("Tabs", label = h4("tabpanel"), choices = list("tabs" = "tabs"),selected = NULL), 
    checkboxGroupInput("MoreTabs", label = h4("moretabpanel"), choices = list("moretabs" = "moretabs"),selected = NULL) 
), 
    dashboardBody(
    uiOutput("ui") 
) 
) 
server <- function(input, output) { 

    output$ui <- renderUI({ 

    check1 <- input$Tabs == "tabs" 
    check2 <- input$MoreTabs == "moretabs" 

    if(length(check1)==0){check1 <- F} 
    if(length(check2)==0){check2 <- F} 

    if(check1 && check2){ 
     tabBox(title = "intro",id= "ttabs", width = 8, height = "420px", 
      tabPanel("Files", dataTableOutput("Files")), 
      tabPanel("Files1", dataTableOutput("Files1")) 
    ) 
    } 
    else if(check1){ 
     tabBox(title = "intro",id= "ttabs", width = 8, height = "420px",tabPanel("Files", dataTableOutput("Files"))) 
    } 
    else{return(NULL)} 
    }) 
} 

shinyApp(ui, server) 
를 사용

enter image description here

예 2 : conditionalPanel 답변에 대한

rm(list = ls()) 
library(shiny) 
library(shinydashboard) 

ui <- dashboardPage(
    dashboardHeader(), 
    dashboardSidebar(
    checkboxGroupInput("Tabs", label = h4("tabpanel"), choices = list("tabs" = "tabs"),selected = NULL), 
    checkboxGroupInput("MoreTabs", label = h4("moretabpanel"), choices = list("moretabs" = "moretabs"),selected = NULL) 
), 
    dashboardBody(

    conditionalPanel(
     condition = "input.MoreTabs == 'moretabs' && input.Tabs == 'tabs'", 
     tabBox(
     title = "intro", 
     id= "ttabs", width = 8, height = "420px", 
     tabPanel("Files",value=1, dataTableOutput("Filesa")), 
     tabPanel("Files1",value=2, dataTableOutput("Files1a")) 
    ) 
    ), 

    conditionalPanel(
     condition = "input.Tabs == 'tabs' && input.MoreTabs != 'moretabs'", 
     tabBox(
     title = "intro", 
     id= "ttabs", width = 8, height = "420px", 
     tabPanel("Files",value=3, dataTableOutput("Files")) 
    )) 
)) 
server <- function(input, output) { } 
shinyApp(ui, server) 
+0

감사 사용,하지만 난'renderUI를 사용하는 관심이 아니다 ' –

+1

'renderUI'가 이것을하는 방법입니다. – jdharrison

+1

@AaghazHussain, 나는'renderUI' 옵션을 선호하지만,'conditionalPanel'도 함께 사용할 수는 있지만'tabPanel'의'value'를 변경해야합니다. –

관련 문제