将sliderInput值设置为闪亮的字符 [英] Set sliderInput values as characters in shiny
问题描述
我的闪亮应用有一个 sliderInput
,但想要将值替换为字符标签。我怎么能实现它?感谢您的任何建议。
My shiny app has a sliderInput
, but want to replace values as character labels. How could I implement it? Thanks for any suggestions.
这是我的示例代码:
library(shiny)
values <- as.factor(c('Label 1', 'Label 3', 'Label 3'))
ui <- shinyUI(bootstrapPage(
headerPanel("test"),
sliderInput("foo", "Animation duration",
min = 1,
max = length(values),
value = values)
))
server <- shinyServer(function(input, output, session) {
})
shinyApp(ui = ui, server = server)
推荐答案
感谢@daattli指出我正确的方向并让我知道如何使用 js
来更改闪亮元素。
Thanks @daattli for pointing me the right direction and letting me know how to use js
to change the shiny element.
我已经实现了一个解决方案来更改 sliderInput
和 selectInput $的标签c $ c>切换不同的值(和长度)。我认为这个功能应该实现为使用
ionRangeSlider
的闪亮。
I have implemented a solution to change labels of sliderInput
and a selectInput
to switch different values (and length). I think this feature should be implemented into shiny which uses ionRangeSlider
.
如果您认为有,请改进我的代码一种更好的实现方式,因为它是我的第一个 js
脚本。
Please improve my codes if you think there is a better way to implement it, as it is my first js
script.
library(shiny)
values <- list(A = c('A1', 'A2', 'A3'),
B = c('B1', 'B2', 'B3', 'B4'))
ui <- shinyUI(bootstrapPage(
selectInput('selection', 'selection', c('A', 'B'), 'A'),
uiOutput('selectUI'),
sliderInput(inputId = "target", label = "Target",
min = 0, max = length(values$A) - 1,
step = 1,
value = length(values$A) - 1),
verbatimTextOutput('summary')
))
server <- shinyServer(function(input, output, session) {
output$summary <- renderPrint({
print(input$target)
print(values[[input$selection]][input$target + 1])
})
output$selectUI <- renderUI({
sel_values <- paste(paste0('"', values[[input$selection]], '"'), collapse = ',')
print(sel_values)
list(
(HTML(
sprintf('
<script type="text/javascript">
$(document).ready(function() {
var vals = [%s];
$(\'#target\').data(\'ionRangeSlider\').update(
{values:vals,
min: 0,
max: %s,
from:%s})
})
</script>
', sel_values,
length(values[[input$selection]]) - 1,
length(values[[input$selection]]) - 1)))
)}
)}
)
shinyApp(ui = ui, server = server)
这篇关于将sliderInput值设置为闪亮的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!