R闪亮:conditionalPanel在选定面板上共享边栏

R Shiny: conditionalPanel sharing sidebars on select panels

本文关键字:共享 闪亮 conditionalPanel      更新时间:2023-09-26

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

##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"
    )
  )
))

在面板之间共享一些边栏,同时使其他边栏变得独特,有什么指导吗?

第一个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)            
    ) 
  ),