在 R Shiny 应用程序中重用输入

标签 r shiny

我想在 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)

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

谢谢!

最佳答案

这是一个使用 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)

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

进行过滤

这个系统有几个优点。添加更多带有更多 selecteInput 的选项卡并为它们添加新的 observeEvent 语句会很容易。如果你有一堆这样的东西,那么研究 Shiny 的模块可能是值得的。

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

关于在 R Shiny 应用程序中重用输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866046/

相关文章:

R 工具提示数据点

r - 准备要传递给curlPerform的字符串

r - 如何计算R中低于特定阈值的2个坐标之间的距离?

php - 如何将这个 "R"公式翻译成MySQL/PHP?

r 基于选择的 Shiny 用户输入

r - Highcharter map 点击事件在 Shiny 模块中不起作用

r - 生成下拉通知的函数 Shiny R

r - 从分类变量创建新的虚拟变量列

r - 在 R 中计算年龄

r - Shiny 的响应式(Reactive)下拉菜单行为不当(带有数字输入)