简体   繁体   English

R Shiny中的多个电抗选择输入

[英]Multiple reactive selectinput in R shiny

I have been using the following code to allow multiple select inputs to be reactive to each other. 我一直在使用以下代码来允许多个选择输入相互反应。 So when one is changed the values in the other boxes are updated: 因此,当一个更改时,其他框中的值也会更新:

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)

This works great for 2 select input boxes but I am at a loss on how to add more. 这非常适合2个选择输入框,但我对如何添加更多内容一无所知。

I have a total of 4 selectinputs that need to be reactive to each other (as well as update a reactive dataframe). 我总共有4个selectinput,它们需要彼此反应(以及更新反应数据框)。

I am new to R and Shiny. 我是R和Shiny的新手。

If you're just trying to get the subsetted data and/or filter values then you're working way too hard. 如果您只是想获取子集数据和/或过滤器值,那么您的工作方式就太辛苦了。 The DT package populates a input value with the indices of the filtered rows and automatically provides class appropriate filters. DT包使用过滤后的行的索引填充输入值 ,并自动提供适用于类的过滤器。 Note we use the subsetted_data() in a different render to produce more ui elements. 注意,我们在不同的渲染器中使用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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM