Reset inputs with reactive app in shiny

孤者浪人 提交于 2020-01-07 03:53:07

问题


I want to add an actionbutton to reset my inputs. For example here, I want all inputs with "All name" "All gender" and "All age" ...

I tried with shinyjs but unfortunately doesnt work

Any help will be appreciated

l <- NULL
l$name <- c('b','e','d','b','b','d','e','e','b','b')
l$age <- c(20,20,21,21,20,22,22,30,21,32)
l$gender <- c('Female', 'Female', 'Male', 'Female', 'Male','Male', 
              'Female','Male',"Female","Male")

l <- as.data.frame(l)
l$name <- as.character(l$name)
l$age <- as.numeric(l$age)
l$gender <- as.character(l$gender)


library(shiny)
server <- shinyServer(function(input,output){

  observeEvent(input$reset, {
    reset("Box1") 
    reset("Box2") 
    reset("Box3")

  })

  # DEfinition of all values of my variables
  assign('All Names',unique(sort(l$name)))
  assign("All Ages", unique(sort(l$age)))
  assign('All Genders', unique(sort(l$gender)))


  data1 <- reactive({
    l[which(l$name %in% if(exists(input$name))
    {get(input$name)}else{input$name}),]
  })




  data2 <- reactive(data1()[which(data1()$age %in% if(exists(input$age))
  {get(input$age)}else{input$age}),])


  data3 <- eventReactive(input$baba, {
    data2()[which(data2()$gender %in% if(exists(input$gender))
    {get(input$gender)}else{input$gender}),]
  })

  output$table3 <- renderTable({
    data3()
  })


  output$Box1 =  renderUI(
    if((is.null(input$age)) & (is.null(input$gender))){
      selectInput("name", "Choose Name", choices=c("All Names",unique(sort(l$name))), selected = input$name)
    } else{selectInput("name", "Choose Name", choices=c("All Names",
                                                        unique(l[l$gender %in% (if(exists(input$gender)){
                                                          get(input$gender)
                                                          }else{
                                                            input$gender}) & 
                                                            l$age %in% (if(exists(input$age)){
                                                              get(input$age)
                                                              }else{input$age}), "name"])), selected = input$name)
    }
  )



  output$Box2 =  renderUI(
    if((is.null(input$name)) & (is.null(input$gender))){
      selectInput("age", "Choose Age", choices=c("All Ages", unique(sort(l$age))), selected = input$age)
    }else{selectInput("age", "Choose Age", choices=c("All Ages",unique(l[l$gender %in% (if(exists(input$gender)){get(input$gender)}else{input$gender}) & l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) , "age"])), selected = input$age)}
  )


  output$Box3 =  renderUI(
    if((is.null(input$name)) & (is.null(input$age))){
      selectInput("gender", "Choose Gender", choices=c("All Genders", unique(sort(l$gender))), selected = input$gender)
    }else{
      selectInput("gender", "Choose Gender", choices=c("All Genders", unique(l[l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) & l$age %in% (if(exists(input$age)){get(input$age)}else{input$age}), "gender"])), selected = input$gender)
    }
  )



})

ui <-shinyUI(fluidPage(
  useShinyjs(),
  uiOutput("Box1"),
  uiOutput("Box2"),
  uiOutput("Box3"),
  actionButton("baba", "GO !"),
  actionButton("reset", "reset"),
  tableOutput("table3")
))

shinyApp(ui,server)

................................................................................................................................................


回答1:


Here is the code that should work for You:

l <- NULL
l$name <- c('b','e','d','b','b','d','e','e','b','b')
l$age <- c(20,20,21,21,20,22,22,30,21,32)
l$gender <- c('Female', 'Female', 'Male', 'Female', 'Male','Male', 
              'Female','Male',"Female","Male")

l <- as.data.frame(l)
l$name <- as.character(l$name)
l$age <- as.numeric(l$age)
l$gender <- as.character(l$gender)


library(shiny)
library(shinyjs)
server <- shinyServer(function(input,output){

  observeEvent(input$reset, {
    shinyjs::reset("name") 
    shinyjs::reset("age") 
    shinyjs::reset("gender") 

  })

  # DEfinition of all values of my variables
  assign('All Names',unique(sort(l$name)))
  assign("All Ages", unique(sort(l$age)))
  assign('All Genders', unique(sort(l$gender)))


  data1 <- reactive({
    l[which(l$name %in% if(exists(input$name))
    {get(input$name)}else{input$name}),]
  })




  data2 <- reactive(data1()[which(data1()$age %in% if(exists(input$age))
  {get(input$age)}else{input$age}),])


  data3 <- eventReactive(input$baba, {
    data2()[which(data2()$gender %in% if(exists(input$gender))
    {get(input$gender)}else{input$gender}),]
  })

  output$table3 <- renderTable({
    data3()
  })


  output$Box1 =  renderUI(
    if((is.null(input$age)) & (is.null(input$gender))){
      selectInput("name", "Choose Name", choices=c("All Names",unique(sort(l$name))), selected = "All Names")
    } else{selectInput("name", "Choose Name", choices=c("All Names",
                                                        unique(l[l$gender %in% (if(exists(input$gender)){
                                                          get(input$gender)
                                                        }else{
                                                          input$gender}) & 
                                                          l$age %in% (if(exists(input$age)){
                                                            get(input$age)
                                                          }else{input$age}), "name"])), selected = "All Names")
    }
  )



  output$Box2 =  renderUI(
    if((is.null(input$name)) & (is.null(input$gender))){
      selectInput("age", "Choose Age", choices=c("All Ages", unique(sort(l$age))), selected = "All Ages")
    }else{selectInput("age", "Choose Age", choices=c("All Ages",unique(l[l$gender %in% (if(exists(input$gender)){get(input$gender)}else{input$gender}) & l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) , "age"])), selected = "All Ages")}
  )


  output$Box3 =  renderUI(
    if((is.null(input$name)) & (is.null(input$age))){
      selectInput("gender", "Choose Gender", choices=c("All Genders", unique(sort(l$gender))), selected = "All Genders")
    }else{
      selectInput("gender", "Choose Gender", choices=c("All Genders", unique(l[l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) & l$age %in% (if(exists(input$age)){get(input$age)}else{input$age}), "gender"])), selected = "All Genders")
    }
  )



})

ui <-shinyUI(fluidPage(
  useShinyjs(),
  uiOutput("Box1"),
  uiOutput("Box2"),
  uiOutput("Box3"),
  actionButton("baba", "GO !"),
  actionButton("reset", "reset"),
  tableOutput("table3")
))

shinyApp(ui,server)

There are two things that were creating a problem, and it is not the package shinyjs, rather Your code:

1. You did not have a selected value in the widgets, You had for example: selected = input$age but not fixed value to reset to (i have changed it to selected = "All ages", etc.)

2. You should use shinyjs::reset() function with the id of the widget, such as in Your case: name, age and gender and not as You have: Box1,Box2,Box3



来源:https://stackoverflow.com/questions/44779775/reset-inputs-with-reactive-app-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!