Search code examples
rsemantic-uishiny

Rating Stars in R shiny app


I am trying to add some elements to my shiny app to make it look better. Therefore, I am using the new shiny.semantic package which allows to add semantic UI elements in an easy way. One finds examples for shiny semantic elements here: http://demo.appsilondatascience.com/shiny.semantic/components/

I wanted to add a rating stars UI with the following code:

library(shiny)
#devtools::install_github("Appsilon/shiny.semantic")
library(shiny.semantic)

ui <- function() {
  shinyUI(
    semanticPage(
      title = "My page",
      suppressDependencies("bootstrap"),
      div(class = "ui star rating")
    )
  )
}

server <- shinyServer(function(input, output) {
})

shinyApp(ui = ui(), server = server)

Unfortunately, the rating stars are not appearing in the app. Is there another way to add such rating stars to shiny apps?


Solution

  • You need to initialize with Javascript code

    ui.R:

    library(shiny)
    library(shiny.semantic)
    
    shinyUI(semanticPage(
      shinyjs::useShinyjs(),
      div(class = "ui star rating")
    ))
    

    server.R:

    library(shiny)
    library(shinyjs)
    jsCode <- "
    $('.ui.rating')
      .rating({
        initialRating: 3,
        maxRating: 5
      })
    ;
    "
    
    shinyServer(function(input, output) {
      runjs(jsCode)
    })