在Shiny上设置“重置值”按钮

时间:2019-12-27 18:41:51

标签: r shiny shiny-server shinyjs shiny-reactivity

我整理了一个Shiny应用程序,该应用程序以反应方式创建列表,同时从您要选择的列表中删除这些选择。我正在尝试组合一个功能,在其中单击“重置”按钮,它会执行以下操作:

1。)取消选择所有输入选项

2。)将“年龄范围”设置为18-104(以便捕获所有值)

3。)将其他两个滑块移到零

我正在尝试使用shinyjs::reset函数,但是它似乎没有用。看看:

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

library(shiny)
library(shinyWidgets)
library(shinyjs)
library(tidyverse)
library(DT)

ui <- fluidPage(
  div(id = "myapp",
  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"),
             actionButton("reset", "Reset form")), 
           textOutput("counter"),
           tags$h2("Data to filter"),
           DT::dataTableOutput("table"),
           tags$h2("IDs added to list"),
           DT::dataTableOutput("addedToList")
    )
  )
)
)

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

  filterData = reactiveVal(df %>% mutate(key = 1:nrow(df)))
  addedToList = reactiveVal(data.frame())

  filtered_df <- reactive({

    res <- filterData() %>% 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

  })

  observeEvent(input$button, {

    addedToList(rbind(addedToList(), 
                      filterData() %>% filter(key %in% filtered_df()$key) %>% 
                        select(customer_id) %>% distinct() ))

    filterData(filterData() %>% filter(!key %in% filtered_df()$key))

  })

  observeEvent(input$reset, {
    shinyjs::reset("myapp")
  })

  output$addedToList <- renderDataTable({
    addedToList()
  })

}

shinyApp(ui,server)

我想念什么吗?

1 个答案:

答案 0 :(得分:1)

您需要做的就是确保您的应用程序正在侦听应用程序中对“ ShinyJS”的调用。在用户界面中,添加useShinyJS()调用!

ui <- fluidPage(
  useShinyJS()
  div(id = "myapp",
  fluidRow(...)
)

我还应该注意,这看起来像是这个问题的重复。 'Reset inputs' button in shiny app