sammerk sammerk - 2 months ago 7
R Question

Reset ratingInput in shiny app

I want to use a rating Input in conjunction with the

shinyjs::reset()
function. Everthing works fine except the reset functionality. Any hints?

Here is my minimal example:

library(shiny)
devtools::install_github("stefanwilhelm/ShinyRatingInput")
library(ShinyRatingInput)
library(shinyjs)

ui <- shinyUI(bootstrapPage(
useShinyjs(),
ratingInput("movieRating", label="Rate this movie...", dataStop=5),
htmlOutput("movieRatingout"),
actionButton("resetbtn", "reset")
))

#the corresponding server.R
server <- shinyServer(function(input, output, session) {
output$movieRatingout <- renderText({
paste("The movie was rated ",input$movieRating)
})

observeEvent(input$resetbtn, {
reset("movieRating")
})

})


shinyApp(ui, server)

Answer

You can create reset action manualy

1) Add js to reset icons ( set width of foreground ==0)

jsCode <-"shinyjs.reset_1 = function(params){$('.rating-symbol-foreground').css('width', params);}"

2) add this js to app using extendShinyjs

3) add session$sendInputMessage to reset input ( set value == NULL)

Working example

jsCode <-"shinyjs.reset_1 = function(params){$('.rating-symbol-foreground').css('width', params);}"
ui <- shinyUI(bootstrapPage(
  useShinyjs(),
  extendShinyjs(text = jsCode),
  ratingInput("movieRating", label="Rate this movie...", dataStop=5),
    htmlOutput("movieRatingout"),
  actionButton("resetbtn", "reset")
))

#the corresponding server.R
server <- shinyServer(function(input, output, session) {
  output$movieRatingout <- renderText({
    paste("The movie was rated ",input$movieRating)
  })

  observeEvent(input$resetbtn, {
    session$sendInputMessage("movieRating", list(value = NULL))
    js$reset_1(0)

  })

})