Shiny侧边栏中的条件面板

时间:2013-10-25 00:25:53

标签: r shiny conditional

我知道有一些关于这个问题的帖子,但我不知道为什么我的代码无效。我有一个Shiny的应用程序,我希望包含一个条件侧边栏面板,根据用户当前选择的主面板中的哪个面板显示不同的控件。我认为下面的代码可以工作,但应用程序只显示条件面板1(定义如下)。任何人都可以给我任何建议吗?谢谢。

我的ui.R:

library(shiny)


shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Spatially Balanced Sampling Tool"),

  sidebarPanel(
    tags$head(
      tags$style(type="text/css", "select { max-width: 175px; }"),
      tags$style(type="text/css", "textarea { max-width: 100px; }"),
      tags$style(type="text/css", ".jslider { max-width: 120px; }"),
      tags$style(type='text/css', ".well { max-width: 200px; }"),
      tags$style(type='text/css', ".span4 { max-width: 200px; }")
    ),

conditionalPanel(condition="input.conditionedPanels == 1",       
                 fileInput('file1', 'Choose .ZIP Shapefile:', multiple=TRUE,
                           accept=c('binary'))
),

conditionalPanel(condition="input.conditionedPanels == 2", 
                   numericInput("pts", "# of sampling points per stratum:", 50),
                   numericInput("oversamppts", "# to OVER sample (optional):", 5),
                   submitButton("Generate Points"),
                 helpText("WORK DAMMIT")

)),

mainPanel(

tabsetPanel(

  tabPanel("Instructions",
           includeHTML("instructions.html"), 
           div(id="linkToMap", tags$a("Click here to see a map of your input data and create points")), 
           div(id="linkToPoints", tags$a("Click here to see table of created points")),
           value=1
  ),

  tabPanel("plot",  helpText("Map of input polygons"),
           plotOutput("plot"),
           p(paste("polygons by strata")),

           value=2
  ),
  tabPanel("View Points", helpText("suggested sampling points"),
           tableOutput("pointdata"),


           HTML("<script>$('#linkToMap').click(function() {
                tabs = $('.tabbable .nav.nav-tabs li')
                tabs.each(function() {
                $(this).removeClass('active')
                })
                $(tabs[1]).addClass('active')
                tabsContents = $('.tabbable .tab-content .tab-pane')
                tabsContents.each(function() {
                $(this).removeClass('active')
                })
                $(tabsContents[1]).addClass('active')

                $('#plot').trigger('change').trigger('shown')

                })</script>
                "),
           HTML("<script>$('#linkToPoints').click(function() {
                tabs = $('.tabbable .nav.nav-tabs li')
                tabs.each(function() {
                $(this).removeClass('active')
                })
                $(tabs[2]).addClass('active')
                tabsContents = $('.tabbable .tab-content .tab-pane')
                tabsContents.each(function() {
                $(this).removeClass('active')
                })
                $(tabsContents[2]).addClass('active')

                $('#pointdata').trigger('change').trigger('shown')

           })</script>
                "),
           value=2
           ),


id = "conditionedPanels"))))

1 个答案:

答案 0 :(得分:8)

看起来,conditionalPanel语句正在查找tabPanel的名称

 # This will not work
 condition="input.conditionedPanels == 1" #wrong

当我在conditionalPanel语句中切换条件以测试选项卡的名称时,而不是该值,它可以正常工作。

我挖出了一切无关的东西,让你的UI.R按照预期有条件地工作。您可以将此作为起点,并从此处开始。

UI.R

library(shiny)
shinyUI(pageWithSidebar(  
  headerPanel("Spatially Balanced Sampling Tool"),
  sidebarPanel(
    conditionalPanel(condition="input.conditionedPanels == 'Tab1'",       
                     helpText("TAB 1")
    ),    
    conditionalPanel(condition="input.conditionedPanels == 'Tab2'", 
                     helpText("TAB 2 SELECTED")
                    )
    ),

  mainPanel(
    tabsetPanel(
      tabPanel("Tab1",
               p(paste("Tab 1 text")                 
          )
      ),
      tabPanel("Tab2",  helpText("Map of input polygons"),
               p(paste("polygons by strata")
                 )
      ), 
      id = "conditionedPanels"                
        )
    )  
))