【问题标题】:ReactiveUI based on a ReactiveUI IssuesReactiveUI 基于一个 ReactiveUI 问题
【发布时间】:2013-09-06 14:31:18
【问题描述】:

我的代码如下,演示站点在这里:http://glimmer.rstudio.com/kdavenport/test1/

第一个输入(下拉菜单)加载定义在 global.R 中的数据框

第二个输入(下拉菜单)通过 df 的“服务”列过滤此数据帧

第三个输入(复选框)通过 df 的“Round”列进一步过滤数据框

问题是显示了第一步中数据框可用的所有复选框,而不是在步骤 2 中按“服务”过滤后可用的复选框。下面我有 1.clientData > serviceFiltered_clientData > roundFiltered_clientData

shinyServer(function(input, output) {

  # First UI input (Service column) filter clientData 
  output$serviceControls <- renderUI({
    if (is.null(clientData()))
      return("No client selected")
    selectInput("service_select", 
                "Choose Service:", 
                choices = as.character(levels(clientData()$Service.Name)),
                selected = input$service_select
    )
  })

  # Second UI input (Rounds column) filter service-filtered clientData  
  output$roundControls <- renderUI({
    if (is.null(serviceFiltered_clientData()))
      return("No service selected")
    checkboxGroupInput("round_select", 
                       "Choose Round:", 
                       choices = as.character(levels(serviceFiltered_clientData()$Round)))
  })  

  # First data load (client data)
  clientData <- reactive({
    if (is.null(input$client_select))
      return(NULL)
    get(input$client_select)
  })

  # Second data load (filter by service column)
  serviceFiltered_clientData <- reactive({
    dat <- clientData()
    if (is.null(dat))
      return(NULL)
    if (!is.null(input$service_select)) # !
      dat <- dat[dat$Service.Name %in% input$service_select,]
    return(dat)
  })

  # Third data load (filter by round column)
  roundFiltered_clientData <- reactive({
    dat <- serviceFiltered_clientData()
    if (is.null(dat))
      return(NULL)
    if (!is.null(input$round_select)) # !
      dat <- dat[dat$Round %in% input$round_select,]
    return(dat)
  })


  # Audit count panel
  output$auditsCount <- renderText({
    if (is.null(roundFiltered_clientData()))
      return("No client selected")
    paste("Total Audits:",nrow(roundFiltered_clientData()))
  })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    反应性菊花链一直在工作,问题在于“圆形”列被加载为一个因素,而因素在子集后作为数据框属性持续存在。因此,我需要在对数据进行子集化后使用 droplevels() 。我在下面添加了dat &lt;- droplevels(dat)

      # Second data load (filter by service column)
      serviceFiltered_clientData <- reactive({
        dat <- clientData()
        if (is.null(dat))
          return(NULL)
        if (!is.null(input$service_select)) # !
          dat <- dat[dat$Service.Name %in% input$service_select,]
        dat <- droplevels(dat) # This is what fixed it
        return(dat)
      })
    

    【讨论】:

      【解决方案2】:

      您可以通过将 checkboxInputs 分组并使用 conditionalPanel 选择性地显示和隐藏来解决此问题。

      步骤:

      1. 对于按服务过滤的每种情况,制作一个动态列表,列出哪些复选框是有意义的。
      2. 根据您所做的下拉 #2(服务)选择创建一个条件。
      3. 仅显示符合条件的复选框选项。

      伪代码

      # Only show this panel if a certain Service is selected
            conditionalPanel(
               condition = "input.service.option == 'filter2'",
               checkboxInput(inputId = "opt.col1", label = "label1", value = FALSE),
               checkboxInput(inputId = "opt.col3", label = "label3", value = FALSE),
               )
      

      See this example 由 RStudio 人员提供,accompanying code.(尤其是 UI.R 中使用多个条件面板的代码部分。)

      【讨论】:

      • 感谢拉姆的回复。如果我明确列出基于服务可用的回合,界面不再是动态的,对吗?随着数据的更新(随着时间的推移),会添加更多轮次,或者现有的轮次可能会对其名称进行编辑。我需要根据数据动态选择复选框。
      • 是的,同意。你能动态形成列表吗?我的建议是首先创建所有复选框的超集(如果可能的话)并使用 conditionalPanel 显示正确的复选框。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多