【发布时间】:2020-04-11 15:52:46
【问题描述】:
我正在尝试根据独特的值创建一个反应性计数。
假设您将年龄滑块调整为 Age >= 50 和 Current Score >= 10,它返回 1571 个唯一客户 ID 的计数,然后显示在表格中。然后您单击Add to List 按钮并添加那些1571。但与此同时,那些 1571 也会从您正在使用的过滤数据集中删除。现在,在您进行添加后,所有输入都会自行重置。然后假设你想用Current Score >= 20添加所有西班牙裔人,所以你移动我设置它的方式,它会返回一个值 310,但是通过过滤设置到我想要实现的目标,它会仅返回尚未过滤掉的唯一客户 ID,这些 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)
【问题讨论】:
-
您可以通过任何方式共享数据或使您的代码与示例数据一起工作。会更容易理解你在追求什么。
-
@teofil 啊,我以为我之前做了那个编辑。抱歉,它现在在新脚本中。另外:
df <- read.csv('https://raw.githubusercontent.com/gooponyagrinch/sample_data/master/datasheet.csv') -
@gooponyagrinch 你的问题很不清楚。也许发布您想要实现的每个步骤的 R 闪亮应用程序的屏幕截图
标签: r shiny shiny-server shiny-reactivity shinyapps