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

问题描述 投票:0回答:1

逗人,

在我的应用中,用户对某些内容进行评分。

我想像IMDB一样根据他们的等级输出5星级。

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

我一点也不懂Java或JavaScript。

是否有类似包装的东西?或做什么?

提前感谢。

r user-interface shiny rating
1个回答
0
投票

您需要创建两个文件,一个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]-->

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

<div class="ratings">
  <div class="empty-stars"></div>
  <div class="full-stars" style="width:70%"></div>
</div>

因此,我们使用UI静态元素的组合,然后是uiOutput,它与服务器端的renderUI相匹配:

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)

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.