【问题标题】:Multiple reactive selectinput in R shinyR闪亮中的多个反应选择输入
【发布时间】:2018-10-09 21:48:04
【问题描述】:

我一直在使用以下代码来允许多个选择输入相互反应。因此,当其中一个被更改时,其他框中的值也会更新:

l <- NULL
l$name <- c('b','e','d','b','b','d','e')
l$age <- c(20,20,21,21,20,22,22)
l <- as.data.frame(l)
l$name <- as.character(l$name)
l$age <- as.numeric(l$age)
library(shiny)

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

  data1 <- reactive({
    if(input$Box1 == "All"){
      l
    }else{
      l[which(l$name == input$Box1),]
    }
  })

  data2 <- reactive({
    if (input$Box2 == "All"){
      l
    }else{
      l[which(l$age == input$Box2),]
    }
  })

  observe({

    if(input$Box1 != "All"){
      updateSelectInput(session,"Box2","Choose an age", choices = c("All",unique(data1()$age)))
    }

    else if(input$Box2 != 'All'){
      updateSelectInput(session,"Box1","Choose a name", choices = c('All',unique(data2()$name)))
    }

    else if (input$Box1 == "All" & input$Box2 == "All"){
      updateSelectInput(session,"Box2","Choose an age", choices = c('All',unique(l$age)))
      updateSelectInput(session,"Box1","Choose a name", choices = c('All',unique(l$name)))
    }
  })


  data3 <- reactive({
    if(input$Box2 == "All"){
      data1()
    }else if (input$Box1 == "All"){
      data2()
    }else if (input$Box2 == "All" & input$Box1 == "All"){
      l
    }
    else{
      l[which(l$age== input$Box2 & l$name == input$Box1),]
    }
  })

  output$table1 <- renderTable({
    data3()
  })


})



ui <-shinyUI(fluidPage(
  selectInput("Box1","Choose a name", choices = c("All",unique(l$name))),
  selectInput("Box2","Choose an age", choices = c("All",unique(l$age))),
  tableOutput("table1")
))

shinyApp(ui,server)

这适用于 2 个选择输入框,但我不知道如何添加更多。

我总共有 4 个选择输入需要相互反应(以及更新反应数据框)。

我是 R 和 Shiny 的新手。

【问题讨论】:

  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
  • 感谢不会再发生
  • 您能解释一下为什么要对表格进行子集化吗?无论您只是想显示过滤后的数据还是在后续计算中实际使用它,DT 包都具有该功能 - 无需自己做
  • 所以我只需要过滤表格中显示的内容。 I have a number of call out boxes and a table that are dynamically updated when one of the options in the select inputs is selected.
  • 过滤完成后不进行任何计算。值只是显示。

标签: r shiny subset dropdown reactive


【解决方案1】:

如果您只是想获取子集数据和/或过滤值,那么您的工作就太辛苦了。 DT 包使用过滤行的索引填充 input value,并自动提供适合类的过滤器。请注意,我们在不同的渲染中使用 subsetted_data() 来生成更多的 ui 元素。

library("shiny")
library("DT")

ldata <- data.frame(
  name = c('b','e','d','b','b','d','e'),
  age  = c(20,20,21,21,20,22,22)
)

#

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

  output$ldata_table <- renderDataTable({
    datatable(ldata, filter = "top")
  })

  subsetted_data <- reactive({
    # the input$<shiny-id>_rows_all populated by the DT package,
    # gets the indices of all the filtered rows
    req(length(input$ldata_table_rows_all) > 0)

    ldata[as.numeric(input$ldata_table_rows_all),]
  })

  output$state <- renderPrint({
    summary(subsetted_data())
  })
})

ui <- fluidPage(
  dataTableOutput("ldata_table"),
  verbatimTextOutput("state")
)

shinyApp(ui, server)

【讨论】:

  • 谢谢,我会试试这个并报告
猜你喜欢
  • 2020-08-01
  • 2020-07-04
  • 2017-11-24
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
相关资源
最近更新 更多