在 Rshiny 应用程序中重复使用输入

phi*_*ooo 2 r shiny

我想在选项卡式闪亮应用程序中重用输入字段。我的代码如下。

library(shiny)

ui <- navbarPage("Iris data browser",
    tabPanel("Panel 1",
             selectInput("species", "Species",
                         unique(iris$Species)),
             sliderInput("sepal.length", "Sepal length",
                         4.3,7.9,4.5,.1),
             tableOutput("table1")),

    tabPanel("Panel 2",
             selectInput("species", "Species",
                         unique(iris$Species)),
             tableOutput("table2")))


server <- function(input, output) {
    output$table1 <- renderTable({
        iris[iris$Species == input$species & iris$Sepal.Length <= input$sepal.length,c("Sepal.Length","Sepal.Width")]
    })

    output$table2 <- renderTable({
        iris[iris$Species == input$species,c("Petal.Length","Petal.Width")]
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

我想selectInput()在两个面板上使用相同的。预期的结果是,当我更改“面板 1”中的输入值时,它将在“面板 2”中采用相同的值,反之亦然。当然,过滤也应该应用于两个面板上的表格。此外,物种的输入在两个面板上共享,但萼片长度的滑块只能出现在面板 1 上。因此,sidebarLayout() 不是解决方案。

谢谢!

Joh*_*aul 5

这是一个使用 2 的解决方案selectInput,但将它们链接起来,以便它们选择相同的选项。更改说明位于代码下方:

library(shiny)

ui <- navbarPage("Iris data browser",
                 tabPanel("Panel 1",
                          selectInput("species1", "Species", choices=unique(iris$Species)),
                          sliderInput("sepal.length", "Sepal length",
                                      4.3,7.9,4.5,.1),
                          tableOutput("table1")),

                 tabPanel("Panel 2",
                          selectInput("species2", "Species", choices=unique(iris$Species) ),
                          uiOutput("select2"),
                          tableOutput("table2")))


server <- function(session, input, output) {

  Selected<-reactiveValues(Species=NULL)



  observeEvent(input$species1, Selected$Species<-(input$species1))
  observeEvent(input$species2, Selected$Species<-(input$species2))

  observeEvent(Selected$Species, updateSelectInput(session, "species1", selected=Selected$Species))
  observeEvent(Selected$Species, updateSelectInput(session, "species2", selected=Selected$Species))

  output$table1 <- renderTable({
    iris[iris$Species == Selected$Species & iris$Sepal.Length <= input$sepal.length,c("Sepal.Length","Sepal.Width")]
  })

  output$table2 <- renderTable({
    iris[iris$Species == Selected$Species ,c("Petal.Length","Petal.Width")]
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

1)在 中ui,我将inputIds 更改为“species1”和“species2”
2)我将session参数添加到您的server函数中。
3)我创建了一个reactiveValues名为 的对象,Selected其中包含一个名为 的元素Species来存储当前选定的物种,它开始为NULL.
4)observeEvents当用户选择一个物种并将该选择存储在 中时,前两个将触发Selected$Species。使用哪个选择器并不重要,并且始终具有最后选择的值。
5)接下来的两个observeEvents 更新这两个selectInputs 以具有选定的选项,以便Selected$Species当您更改一个选项卡中的值时,它会在另一个选项卡中自动更改。您需要session在此处使用该参数,这就是我之前添加它的原因。
6)我更改了表格以根据Selected$Species

该系统有一些优点。添加更多带有更多 s 的选项卡并为其selecteInput添加新语句将很容易。observeEvent如果你有很多这样的模块,那么可能值得你花时间研究一下闪亮的模块。

在这里,表格只是使用Selected$Species,但如果您愿意,您可以添加更多逻辑,它们有时会更新,有时不会更新,如果这对您的应用程序有意义的话。这允许您产生复杂的行为 - 例如,如果某些值对于您的其中一个显示没有意义,您可以提前捕获并提醒用户或显示其他内容。