复位输入与光泽反应性的应用(Reset inputs with reactive app in sh

2019-10-29 08:50发布

我想补充一个actionbutton重置我的投入。 例如在这里,我想用“全名”,“所有性别”和“所有年龄”的所有输入...

我试着用shinyjs但遗憾的是不工作

任何帮助将不胜感激

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)

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

Answer 1:

下面是应为你工作的代码:

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)

有迹象表明,正在创建一个问题的两种东西,它不是包shinyjs ,而您的代码:

1.你没有一个selected在widgets值,您有例如: selected = input$age而不是固定值重置(我把它改为selected = "All ages" ,等等)

2.您应使用shinyjs::reset()函数与微件的ID,如你的情况:姓名,年龄,性别,而不是因为你有:BOX1,BOX2,BOX3



文章来源: Reset inputs with reactive app in shiny
标签: r shiny shinyjs