R Shiny:conditionalPanel 在 select 面板上共享侧边栏

R Shiny: conditionalPanel sharing sidebars on select panels

我正在尝试在不同的面板上显示反应图。其中一些基于相同的输入,这些输入来自侧边栏上的滑块。我想在这些面板之间保留相同的滑块(和输入)。在其他面板中,我想要独特的条件输入。这是一个例子:

##server
shinyServer(function(input, output) {
  output$scatterPlotA <- renderPlot({
    x <- rnorm(input$slider1)
    y <- rnorm(input$slider1)
    plot(x, y)
  })
   output$scatterPlotB <- renderPlot({
    x <- rnorm(input$slider2)
    y <- rnorm(input$slider2)
    plot(x, y)
  })
     output$scatterPlotC <- renderPlot({
    x <- rnorm(input$slider2)
    y <- rnorm(input$slider2)
    plot(x, y,col="red")
  })
})

这 ui 保留面板 2-3 之间的滑块,但它不会控制面板 3。

##ui
shinyUI(pageWithSidebar(
  headerPanel("Conditional Panels"),
  sidebarPanel(
    conditionalPanel(condition="input.conditionedPanels==1",
                     helpText("Content Panel 1"),
                     sliderInput("slider1", "Number of points: S1", min = 10, max = 200, value = 50, step = 10)

    ),
    conditionalPanel(condition="input.conditionedPanels==2",
                     helpText("Content Panel 2"),
                     sliderInput("slider2", "Number of points: S2", min = 10, max = 200, value = 50, step = 10)            
    ),
    conditionalPanel(condition="input.conditionedPanels==3",
                     helpText("Content Panel 2"),
                     sliderInput("slider2", "Number of points: S2", min = 10, max = 200, value = 50, step = 10)            
    ) 
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Panel 1", value=1, 
      plotOutput("scatterPlotA", height = 300)),
      tabPanel("Panel 2", value=2,
      plotOutput("scatterPlotB", height = 300)),
       tabPanel("Panel 3", value=3,
      plotOutput("scatterPlotC", height = 300)),
      id = "conditionedPanels"
    )
  )
))

这 ui 做了我想要的,除了面板 2 的滑块最终出现在面板 1 上。

##ui
shinyUI(pageWithSidebar(
  headerPanel("Conditional Panels"),
  sidebarPanel(
    conditionalPanel(condition="input.conditionedPanels==1",
                     helpText("Content Panel 1"),
                     sliderInput("slider1", "Number of points: S1", min = 10, max = 200, value = 50, step = 10)

    ),
    conditionalPanel(condition="input.conditionedPanels==2||3",
                     helpText("Content Panel 2"),
                     sliderInput("slider2", "Number of points: S2", min = 10, max = 200, value = 50, step = 10)            
    ) 
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Panel 1", value=1, 
      plotOutput("scatterPlotA", height = 300)),
      tabPanel("Panel 2", value=2,
      plotOutput("scatterPlotB", height = 300)),
       tabPanel("Panel 3", value=3,
      plotOutput("scatterPlotC", height = 300)),
      id = "conditionedPanels"
    )
  )
))

任何 gui在面板之间共享一些侧边栏同时使其他侧边栏独一无二的方法吗?

第一个 ui.R 的问题在于您仍在定义三个不同的滑块。您真正想要的是第二个滑块在两种情况之一下出现。因此,更改第二个滑块的条件以反映该逻辑,并删除第三个滑块。以下代码对我有用。

sidebarPanel(
    conditionalPanel(condition="input.conditionedPanels==1",
                     helpText("Content Panel 1"),
                     sliderInput("slider1", "Number of points: S1", min = 10, max = 200, value = 50, step = 10)

    ),
    conditionalPanel(condition="input.conditionedPanels==2 || input.conditionedPanels==3",
                     helpText("Content Panel 2"),
                     sliderInput("slider2", "Number of points: S2", min = 10, max = 200, value = 50, step = 10)            
    ) 
  ),