If you ever need to reset inputs to their original value , now there's a function for it

3,775 views
Skip to first unread message

Dean Attali

unread,
May 23, 2015, 10:47:14 PM5/23/15
to shiny-...@googlegroups.com
I hope this self-promotion is ok, but I've seen this asked several times so I implemented a function that allows you to reset any input element (or a tag containing input elements) to their original value.
It's the "reset" function of my "shinyjs" package https://github.com/daattali/shinyjs

Example:
library(shinyjs)
runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    resettable(
      div(id = "form",
        textInput("name", "Name", "Dean"),
        numericInput("age", "Age", 26),
        selectInput("letter", "Favourite letter", LETTERS)
      )
    ),
    actionButton("resetName", "Reset name"),
    actionButton("resetAll", "Reset all")
  ),
  server = function(input, output, session) {
    observeEvent(input$resetName, {
      reset("name")
    })
    observeEvent(input$resetAll, {
      reset("form")
    })
  }
))


I hope this can be helpful

Joe Cheng

unread,
May 25, 2015, 1:05:18 AM5/25/15
to Dean Attali, shiny-...@googlegroups.com
I haven't looked at the code but I sure like the intent! Thanks for sharing, on-topic self promotion is highly encouraged on this list ;)
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/a77a26dd-ce9b-42c4-aadf-c2eac9c0fbcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dean Attali

unread,
May 25, 2015, 3:07:47 AM5/25/15
to shiny-...@googlegroups.com, daat...@gmail.com
I'm sure you can implement it in a better/more efficient way than I did: on page load the initial value of resettable elements is recorded as an attribute on the html tag, and when a command to reset an input is given then I use javascript to fetch the input type and original value, send that message back to R, and use the appropriate update function.  (if anyone was wondering how this was achieved) 
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.

Vincent

unread,
May 25, 2015, 3:15:13 AM5/25/15
to shiny-...@googlegroups.com
Interesting. What is the advantage over browser refresh?

Dean Attali

unread,
May 25, 2015, 3:19:56 AM5/25/15
to Vincent, shiny-...@googlegroups.com
Well if you want to reset absolutely everything and get back to the initial state of the app, then browser refresh is definitely a viable option. This is for when you want to reset a single input or a group of inputs. For example if you have several panels, and after submitting one, you want to reset its inputs.  Although even if you just have a form and you want to reset the inputs after submission, I'd argue it's more user friendly to reset the inputs rather than seeing the page refresh itself.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/88OTO7p035o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/6be604dc-c697-4fcb-ad62-437466507670%40googlegroups.com.

Norman Packard

unread,
Jun 1, 2015, 8:37:59 PM6/1/15
to shiny-...@googlegroups.com

Very cool.
I was hoping that this could reset an action button (cf. updateActionButton discussion).
But alas, actionButtons are not resettable, apparently.

Could they possibly be, via the mechanisms of shinyjs?



Dean Attali

unread,
Jun 1, 2015, 8:51:12 PM6/1/15
to shiny-...@googlegroups.com
Hi Norman,

The function doc for `reset` says "Buttons are not supported, meaning that you cannot use this function to reset the value of an action button back to 0."

This is because I don't consider buttons as typical inputs, for me they don't really hold a meaningful value; rather, you only care when they change, not what their new/old value is.  Being so different from all other inputs, they also literally don't have a "value" attribute in HTML. I briefly thought about whether or not I should include them in the reset function and decided against it because they feel so different. I also couldn't think of any practical cases where you'd want to reset an actionButton.  

Let me know if you have any good reasons to reset an actionButton

Norman Packard

unread,
Jun 26, 2015, 7:51:15 PM6/26/15
to shiny-...@googlegroups.com
Hi Dean,

This thread suggests a reason for updateActionButton that even made Joe Cheng consider softening;  there Joe made a suggestion for communicating between server and UI, obviating the need.  But I still haven't quite got it all working, as reported in this thread.  

Dean Attali

unread,
Jun 26, 2015, 10:01:57 PM6/26/15
to shiny-...@googlegroups.com
Hi Norman

Correct me if I'm wrong, but it seems to me that that thread is discussing a technique to create a variable that would actas a binary variable, but it doesn't actually deal with buttons at all. I don't think it's something that can be incorporated into a "reset" function for an actionButton since that technique doesn't have anything to do with buttons.
The main reason shinyjs::reset doesn't support resetting action buttons is simply because there is no implementation of resetting them :) The fact that I find resetting buttons to be weird is only a secondary reason.  If shiny will support it, I'll definitely add it to shinyjs.

FYI - I'm trying to get "reset" functionality incorporated into core shiny, in which case resetting any input will be builtin and resetting actionbuttons will also be super trivial. You can look here https://github.com/rstudio/shiny/issues/870 to tell me what you think (by "I'm trying" I mean I want to but not sure if shiny team will agree that it makes sense)
Reply all
Reply to author
Forward
0 new messages