简体   繁体   中英

Reactive filtering and adding in shiny

I'm trying to create a reactive count based off distinctive values.

So let's say you adjust the age slide to Age >= 50 and a Current Score >= 10 it returns a count of 1571 unique customer IDs, which are then showed in the table. Then you click the Add to List button and those 1571 are added. But at the same time, those 1571 are also removed from the filtered dataset you're working with. Now all of the inputs reset themselves after you make that add. Then let's say you want to add all hispanic people with a Current Score >= 20 , so you move The way I have it setup, it would return a value of 310, but with the filtering setup to what I'm trying to achieve, it would be returning only the unique Customer IDs that haven't been filtered out yet, and those would be added to the total count/table.

Does this make sense?

df <- read.csv('https://raw.githubusercontent.com/gooponyagrinch/sample_data/master/datasheet.csv')

ui <- fluidPage(
  fluidRow(
    column("",
           width = 10, offset = 1,
           tags$h3("Select Area"),
           panel(
             sliderInput("current", "Current Score", min = 0, max = 100, value = 20),
             sliderInput("projected", "Projected Score", min = 0, max = 100, value = 20),
             sliderInput("age", "Age", min = 18, max = max(df$age), value = c(18,24)),
             checkboxGroupInput("ethnicity", label = "Ethnicity", 
                                choices = list("Caucasian" = "Caucasian",
                                               "African-American" = "African-American",
                                               "Hispanic" = "Hispanic",
                                               "Other" = "Other")),
             checkboxInput('previous', label = "Previous Sale"),
             checkboxInput('warm', label = "Warm Lead"),
             actionButton("button", "Add to List")), 
           textOutput("counter"),
           DT::dataTableOutput("table")
    )
  )
)

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

  filtered_df <- reactive({

    res <- df %>% filter(current_grade >= input$current)
    res <- res %>% filter(projected_grade >= input$projected)
    res <- res %>% filter(age >= input$age[1] & age <= input$age[2])
    res <- res %>% filter(ethnicity %in% input$ethnicity | is.null(input$ethnicity))

    if(input$previous == TRUE)
      res <- res %>% filter(previous_sale == 1)

    if(input$warm == TRUE)
      res <- res %>% filter(warm_lead == 1)

    res
  })

  output$counter <- renderText({
    res <- filtered_df() %>% select(customer_id) %>% n_distinct()
    res
  })

  output$table <- renderDataTable({
    res <- filtered_df() %>% distinct(customer_id)
    res
  })
}

shinyApp(ui, server)

This should do it

library(shiny)
library(tidyverse)
library(DT)
df <- read.csv("https://raw.githubusercontent.com/gooponyagrinch/sample_data/master/datasheet.csv")

ui <- fluidPage(
  fluidRow(
    column("",
      width = 10, offset = 1,
      tags$h3("Select Area"),
      div(
        sliderInput("current", "Current Score",
          min = 0, max = 100, value = 20
        ),
        sliderInput("projected", "Projected Score",
          min = 0, max = 100, value = 20
        ),
        sliderInput("age", "Age",
          min = 18, max = max(df$age), value = c(18, 24)
        ),
        checkboxGroupInput("ethnicity",
          label = "Ethnicity",
          choices = list(
            "Caucasian" = "Caucasian",
            "African-American" = "African-American",
            "Hispanic" = "Hispanic",
            "Other" = "Other"
          )
        ),
        checkboxInput("previous", label = "Previous Sale"),
        checkboxInput("warm", label = "Warm Lead"),
        actionButton("button", "Add to List")
      ),
      textOutput("counter"),
      p("Remaining Input Table"),
      DT::dataTableOutput("input_table"),
      p("Filtered Table"),
      DT::dataTableOutput("filtered_table"),
      p("Accumulated Table"),
      DT::dataTableOutput("accumulated_table")
    )
  )
)

accumulated_df <- reactiveVal(NULL)
df <- reactiveVal(df)

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



  filtered_df <- reactive({
    res <- df() %>% filter(current_grade >= input$current)
    res <- res %>% filter(projected_grade >= input$projected)
    res <- res %>% filter(age >= input$age[1] & age <= input$age[2])
    res <- res %>% filter(ethnicity %in% input$ethnicity | is.null(input$ethnicity))

    if (input$previous == TRUE) {
      res <- res %>% filter(previous_sale == 1)
    }

    if (input$warm == TRUE) {
      res <- res %>% filter(warm_lead == 1)
    }

    res
  })

  output$counter <- renderText({
    res <- filtered_df() %>%
      select(customer_id) %>%
      n_distinct()

    res
  })

  observeEvent(input$button, {

    if(! is.null(accumulated_df()))
    accumulated_df(
      union(
        accumulated_df(),
        filtered_df()
      )
    ) else 
      accumulated_df( filtered_df())



    df(setdiff(df(),
               filtered_df())

    )

  })

  output$input_table <- renderDataTable({
    df()
  })
  output$filtered_table <- renderDataTable({
    filtered_df()
  })
  output$accumulated_table <- renderDataTable({
    accumulated_df()
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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