gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:47:19 24 4
gpt4 key购买 nike

亲爱的,

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

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

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

我根本不懂 Java 和 JavaScript。

有类似的包吗?或做什么?

提前致谢。

最佳答案

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

  • 应用.R
    -- www/
    ------ stars.css

您的 stars.css 文件将包含 HTML 标记规则,在 header 中使用 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)

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

enter image description here

关于r - 有没有办法在 R Shiny 中*输出*星级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com