有没有办法在 R Shiny 中*输出*星级?

Am9*_*m95 1 user-interface r rating shiny

亲爱的,

在我的应用中,用户会对一些东西进行评分。

我想根据他们的评分输出 5 星评分,就像 IMDB 中的评分一样。

我的数字中有分数,我希望星星能够容纳它们。

我根本不懂 Java 或 JavaScript。

有没有类似的包?或者怎么办?

提前致谢。

Car*_*eri 6

您需要创建两个文件,一个css然后是您的应用程序...即:

  • app.R
    -- www/
    ------ stars.css

您的stars.css文件将包含 HTML 标记规则,该规则将includeCSS在标题中使用后根据我们的应用程序更新:

.ratings {
  position: relative;
  vertical-align: middle;
  display: inline-block;
  color: #b1b1b1;
  overflow: hidden;
}

.full-stars{
  position: absolute;
  left: 0;
  top: 0;
  white-space: nowrap;
  overflow: hidden;
  color: #fde16d;
}

.empty-stars:before,
.full-stars:before {
  content: "\2605\2605\2605\2605\2605";
  font-size: 44pt; /* Make this bigger or smaller to control size of stars*/
}

.empty-stars:before {
  -webkit-text-stroke: 1px #848484;
}

.full-stars:before {
  -webkit-text-stroke: 1px orange;
}

/* Webkit-text-stroke is not supported on firefox or IE */
/* Firefox */
@-moz-document url-prefix() {
  .full-stars{
    color: #ECBE24;
  }
}
/* IE */
<!--[if IE]>
  .full-stars{
    color: #ECBE24;
  }
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

在我们的应用程序中,我们希望最终标记如下所示:

<div class="ratings">
  <div class="empty-stars"></div>
  <div class="full-stars" style="width:70%"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

所以为了做到这一点,我们使用了 UI 静态元素和 then 的组合uiOutput,它与renderUI服务器端的a 匹配:

library(shiny)


ui <- fluidPage(
    includeCSS("www/stars.css"),
    sliderInput(inputId = "n_stars", label = "Ratings", min = 0,  max = 5, value = 3, step = .15),
    tags$div(class = "ratings",
             tags$div(class = "empty-stars",
                      uiOutput("stars_ui")
             )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

    output$stars_ui <- renderUI({
        # to calculate our input %
        n_fill <- (input$n_stars / 5) * 100
        # element will look like this: <div class="full-stars" style="width:n%"></div>
        style_value <- sprintf("width:%s%%", n_fill)
        tags$div(class = "full-stars", style = style_value)
    })

}

# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

然后我们的应用程序使用滑块输入来创建星星的填充百分比。

在此处输入图片说明