R 反应过滤与添加

R 反应过滤与添加,r,shiny,shiny-server,shiny-reactivity,shinyapps,R,Shiny,Shiny Server,Shiny Reactivity,Shinyapps,我试图根据不同的值创建一个反应性计数 假设您将年龄幻灯片调整为age>=50,并且当前分数>=10它返回1571个唯一客户ID的计数,然后在表中显示。然后单击添加到列表按钮,这些1571将被添加。但同时,这些1571也将从您正在处理的过滤数据集中删除。现在,所有输入在您进行添加后都会自动重置。然后,假设您想添加当前分数>=20的所有西班牙裔人,那么按照我设置的方式,它将返回值310,但按照我尝试实现的过滤设置,它将只返回尚未过滤掉的唯一客户ID,这些将被添加到总计数/表中 这有意义吗 df &l

我试图根据不同的值创建一个反应性计数

假设您将年龄幻灯片调整为
age>=50
,并且
当前分数>=10
它返回1571个唯一客户ID的计数,然后在表中显示。然后单击
添加到列表
按钮,这些1571将被添加。但同时,这些1571也将从您正在处理的过滤数据集中删除。现在,所有输入在您进行添加后都会自动重置。然后,假设您想添加当前分数>=20的所有西班牙裔人,那么按照我设置的方式,它将返回值310,但按照我尝试实现的过滤设置,它将只返回尚未过滤掉的唯一客户ID,这些将被添加到总计数/表中

这有意义吗

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)
df这应该可以

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)
库(闪亮)
图书馆(tidyverse)
图书馆(DT)
df这应该可以

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)
库(闪亮)
图书馆(tidyverse)
图书馆(DT)

df您可以通过任何方式共享数据或使代码与示例数据一起工作。这样更容易理解你想要的是什么。@teofil-Ah我想我之前做过编辑。对不起,新的剧本里有。另外:
df@gooponyagrinch您的问题很不清楚。也许发布你想要实现的每个步骤的R Shining应用程序的屏幕截图,你可以通过任何方式共享数据或使你的代码与示例数据一起工作。这样更容易理解你想要的是什么。@teofil-Ah我想我之前做过编辑。对不起,新的剧本里有。另外:
df@gooponyagrinch您的问题很不清楚。也许发布你想要实现的每一步的R Shining应用程序的屏幕截图,问你答案。我可以问一下这部分是做什么的,为什么它不在服务器端:
我没有写任何“df2”?但是一般来说,reactiveVal实例化了一个反应变量,您可以在服务器端引用它。我不认为它需要在服务器端启动,但如果它需要,或者您喜欢,请将它移到那里。
contracted_df()
reactiveVal正在初始化为一个全局变量,而不是一个UI变量,如果这是产生混淆的原因的话。我建议您在服务器功能中对其进行初始化,原因很简单,在全局中存储一个reactive意味着reactive由应用程序的所有会话共享。我认为演示解决方案避免了由此带来的任何后果,但您应该养成习惯,只在服务器函数中使用反应式谢谢您的回答。我可以问一下这部分是做什么的,为什么它不在服务器端:
我没有写任何“df2”?但是一般来说,reactiveVal实例化了一个反应变量,您可以在服务器端引用它。我不认为它需要在服务器端启动,但如果它需要,或者您喜欢,请将它移到那里。
contracted_df()
reactiveVal正在初始化为一个全局变量,而不是一个UI变量,如果这是产生混淆的原因的话。我建议您在服务器功能中对其进行初始化,原因很简单,在全局中存储一个reactive意味着reactive由应用程序的所有会话共享。我认为演示解决方案避免了由此带来的任何后果,但您应该养成只在服务器函数中使用反应式的习惯