【问题标题】:Conditional panels in sidebar of ShinyShiny侧边栏中的条件面板
【发布时间】:2013-11-03 22:27:30
【问题描述】:

我知道有一些关于此问题的帖子,但我不明白为什么我的代码无法正常工作。我在 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"))))

【问题讨论】:

    标签: r shiny conditional


    【解决方案1】:

    看起来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"                
            )
        )  
    ))
    

    【讨论】:

    • 谢谢你,Ram.. 但这与我看到的文档不符?但是,我确实尝试了您的建议,但我仍然遇到与以前相同的错误行为。我怀疑现在我的其他代码中出现了某种错误。我有一个更简单的应用程序,我能够让条件侧边栏与我原始帖子的语法/格式一起使用。
    • 文档确实说 conditionalPanel 将评估 JavaScript 表达式。你是对的,文档可能更清晰,但同时 Shiny Google Group 很棒。是的,我怀疑其余代码中的某些其他行导致它无法工作。
    • 我注意到将 submitButton() 添加到任一条件面板中都会中断交互。例如:conditionalPanel(condition="input.conditionPanels == 'Tab1'", helpText("TAB 1"), submitButton()),
    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 2021-06-09
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多