【问题标题】:Updating filters in shiny app在闪亮的应用程序中更新过滤器
【发布时间】:2017-11-18 02:51:08
【问题描述】:

我有一个更新过滤器的应用程序,但似乎无法正常工作,我无法修复它。我希望在更改依赖过滤器时更新所有过滤器 我认为问题在于observeEvent 感谢您的帮助

library(shiny)
library(DT)
library(dplyr)

VG <- c("A", "A", "B", "B", "B", "C", "A")
AG <- c(1, 2, 1, 3, 4, 2, 1)
AP <- letters[1:7]
AK <- paste(VG, AG, AP, sep = "-")
data <- data.frame(VG, AG, AP, AK)

ui <- fluidPage(
  column(3,
         selectInput("VG", label = h4("VG.ETD"),choices = unique(data$VG)),
         selectInput("AG", label = h4("AG.ETD"),choices = unique(data$AG))),
  column(3,
         selectInput("AP", label = h4("AP.ETD"),choices = unique(data$AP)),
         selectInput("AK", label = h4("AK.ETD"),choices = unique(data$AK)),
         actionButton("go", "GO")),
  column(6,DT::dataTableOutput("dtt"))
)

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

  observeEvent(input$VG,{
    updateSelectInput(session, 'AG', choices = unique(data$AG[data$VG %in% input$VG]))
  })

  observeEvent(input$AG,{
    updateSelectInput(session, 'AP', choices = unique(data$AP[data$AG %in% input$AG &
                                                                data$VG %in% input$VG]))
  })

  observeEvent(input$AP,{
    updateSelectInput(session, 'AK', choices = unique(data$AK[data$AP %in% input$AP &
                                                                data$AG %in% input$AG &
                                                                data$VG %in% input$VG]))
  })

  df <- eventReactive(input$go, {
    data %>% filter(VG %in% input$VG, 
                    AG %in% input$AG,
                    AP %in% input$AP,
                    AK %in% input$AK)

  })

  output$dtt <- DT::renderDataTable({
    df()

  })

}

shinyApp(ui=ui,server=server)

【问题讨论】:

  • 究竟什么是“依赖过滤器”?你想要这里的行为是什么?我不确定我是否理解您描述的问题。
  • 例如当我更改VG或“B”时,AK ETD没有更新,仍然给我A-1-a
  • 我需要'obserEvent(input$AP | input$AG | input$VG, ...)'之类的东西
  • 不要编辑问题来提出不同的问题。而是开始一个新帖子。

标签: r shiny


【解决方案1】:

你在我的帖子上发表评论说你和我有同样的问题。它看起来有点不同,但我找到了解决问题的方法,所以我在下面发布了代码,以防它对您有所帮助。

l <- NULL
l$name <- c('b','e','d','b','b','d','e','e','b','b')
l$age <- c(20,20,21,21,20,22,22,30,21,32)
l$gender <- c('Female', 'Female', 'Male', 'Female', 'Male','Male', 
'Female','Male',"Female","Male")
l <- as.data.frame(l)
l$name <- as.character(l$name)
l$age <- as.numeric(l$age)
l$gender <- as.character(l$gender)


library(shiny)
server <- shinyServer(function(input,output){

assign('All Names',unique(sort(l$name)))
assign("All Ages", unique(sort(l$age)))
assign('All Genders', unique(sort(l$gender)))
data1 <- reactive(l[which(l$name %in% if(exists(input$name))
{get(input$name)}else{input$name}),])

output$table1 <- renderTable(data1())
output$text1 <- renderPrint(input$name)
data2 <- reactive(data1()[which(data1()$age %in% if(exists(input$age))
{get(input$age)}else{input$age}),])
output$table2 <- renderTable(data2())
data3 <- reactive(data2()[which(data2()$gender %in% if(exists(input$gender))
{get(input$gender)}else{input$gender}),])

output$table3 <- renderTable(data3())


output$Box1 =  renderUI(
if((is.null(input$age)) & (is.null(input$gender))){
  selectInput("name", "Choose Name", choices=c("All Names",unique(sort(l$name))), selected = input$name)
} else{selectInput("name", "Choose Name", choices=c("All Names",unique(l[l$gender %in% (if(exists(input$gender)){get(input$gender)}else{input$gender}) & l$age %in% (if(exists(input$age)){get(input$age)}else{input$age}) , "name"])), selected = input$name)
}
)



output$Box2 =  renderUI(
if((is.null(input$name)) & (is.null(input$gender))){
  selectInput("age", "Choose Age", choices=c("All Ages", unique(sort(l$age))), selected = input$age)
}else{selectInput("age", "Choose Age", choices=c("All Ages",unique(l[l$gender %in% (if(exists(input$gender)){get(input$gender)}else{input$gender}) & l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) , "age"])), selected = input$age)}
)

output$Box3 =  renderUI(
  if((is.null(input$name)) & (is.null(input$age))){
    selectInput("gender", "Choose Gender", choices=c("All Genders", unique(sort(l$gender))), selected = input$gender)
  }else{

    selectInput("gender", "Choose Gender", choices=c("All Genders", unique(l[l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) & l$age %in% (if(exists(input$age)){get(input$age)}else{input$age}), "gender"])), selected = input$gender, multiple = TRUE)
  }
)



})

ui <-shinyUI(fluidPage(
uiOutput("Box1"),
uiOutput("Box2"),
uiOutput("Box3"),
tableOutput("table3")
))

shinyApp(ui,server)

【讨论】:

  • 谢谢,我想要 selectinput 反应式,你的代码做得很好,但我需要一个“go”按钮来更新输出表(因为我有一些问题来修复 actionbutton 的反应性)你有什么想法为了这 ?谢谢
  • ` data3
  • 我编辑了代码以允许多个 = TRUE。输出$Box3 需要添加一个 if 语句
  • 不选择输入时能否避免报错:invalid first argument
【解决方案2】:

我用这个解决方案回答了你评论过的一个类似问题(说你有同样的问题):

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)

【讨论】:

  • 如何在 selectInput 中获得 multiple = True 原因似乎不起作用?
  • 任何其他方法,因为它似乎是 2 或 3 个过滤器的完美解决方案,但如果我们有超过 5 个过滤器,代码变得非常复杂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2020-10-30
  • 1970-01-01
  • 2021-06-01
  • 2017-12-05
  • 2020-01-12
  • 2020-06-24
相关资源
最近更新 更多