重置ratingInput在闪亮的应用程序中

时间:2016-09-19 12:31:14

标签: r shiny shinyjs

我想将评级输入与shinyjs::reset()功能结合使用。除重置功能外,Everthing工作正常。任何提示?

这是我的最小例子:

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)

1 个答案:

答案 0 :(得分:1)

您可以手动创建重置动作

1)添加js以重置图标(设置前景的宽度== 0)

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

2)使用extendShinyjs

将此js添加到app

3)添加session$sendInputMessage以重置输入(设置value == NULL

工作示例

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)

  })

})