最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Reset Multiinput - choices don't clear - Stack Overflow

programmeradmin4浏览0评论

I have a Shiny app with a selectinput where users select a dataset. The variables of the dataset then appear in a shinywidgets multiInput as choices. I also have a reset button that is supposed to clear both input fields. However, it does not clear the non selected wrapper of the multi input (left side). I also tried to solve this without a shinyjs reset function and rather take another updateMultiInput approach but that didn't work either.

Here is a minimal reprex:

library(shiny)
library(shinyWidgets)
library(shinyjs)
library(purrr)

dfs <- keep(ls("package:datasets"), ~ is.data.frame(get(.x, "package:datasets")))


ui <- fluidPage(
  shinyjs::useShinyjs(),
  selectizeInput("dataset", "Select Dataset", choices = dfs, selected = NULL, multiple = TRUE, options = list(maxItems = 1)),
  multiInput("varselect", "Select Variables", choices = c("")),
  actionButton("reset_variables", "Reset Vars", style = "width: 145px; height: 40px")
)

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

  data <- reactive({
    req(input$dataset)
    get(input$dataset, "package:datasets")
  })
  
  vars <- reactive(names(data()))
  
  observeEvent(input$dataset, {
    updateMultiInput(session, "varselect", choices = vars())

  })


  # reset inputs and lists if button is clicked
  observeEvent(input$reset_variables, {
    shinyjs::reset("dataset")
    shinyjs::reset("varselect")
  })
  
}

shinyApp(ui, server)

I have a Shiny app with a selectinput where users select a dataset. The variables of the dataset then appear in a shinywidgets multiInput as choices. I also have a reset button that is supposed to clear both input fields. However, it does not clear the non selected wrapper of the multi input (left side). I also tried to solve this without a shinyjs reset function and rather take another updateMultiInput approach but that didn't work either.

Here is a minimal reprex:

library(shiny)
library(shinyWidgets)
library(shinyjs)
library(purrr)

dfs <- keep(ls("package:datasets"), ~ is.data.frame(get(.x, "package:datasets")))


ui <- fluidPage(
  shinyjs::useShinyjs(),
  selectizeInput("dataset", "Select Dataset", choices = dfs, selected = NULL, multiple = TRUE, options = list(maxItems = 1)),
  multiInput("varselect", "Select Variables", choices = c("")),
  actionButton("reset_variables", "Reset Vars", style = "width: 145px; height: 40px")
)

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

  data <- reactive({
    req(input$dataset)
    get(input$dataset, "package:datasets")
  })
  
  vars <- reactive(names(data()))
  
  observeEvent(input$dataset, {
    updateMultiInput(session, "varselect", choices = vars())

  })


  # reset inputs and lists if button is clicked
  observeEvent(input$reset_variables, {
    shinyjs::reset("dataset")
    shinyjs::reset("varselect")
  })
  
}

shinyApp(ui, server)

Share Improve this question edited Jan 20 at 19:08 Jan 8,8916 gold badges19 silver badges33 bronze badges asked Jan 20 at 10:08 HelbischHelbisch 153 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Perhaps shinyjs::reset() doesn't understand shinyWidgets::multiInput(). You can use updateMultiInput() instead:

observeEvent(input$reset_variables, {
        shinyjs::reset("dataset")
        updateMultiInput(session, "varselect", choices = c(""))
    })
发布评论

评论列表(0)

  1. 暂无评论