admin管理员组文章数量:1332602
My shiny app has a sliderInput
, but want to replace values as character labels. How could I implement it? Thanks for any suggestions.
This is my example code:
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)
My shiny app has a sliderInput
, but want to replace values as character labels. How could I implement it? Thanks for any suggestions.
This is my example code:
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)
Share
Improve this question
edited Feb 1, 2016 at 1:56
Bangyou
asked Feb 1, 2016 at 1:51
BangyouBangyou
9,81616 gold badges65 silver badges99 bronze badges
8
- What is the second line doing? Converting character vectors to dates? That does not work. – Gopala Commented Feb 1, 2016 at 1:56
- @Gopala. Sorry I have fixed the wrong codes – Bangyou Commented Feb 1, 2016 at 1:57
- If you are really trying to use categorical inputs, you should use selectInput (drop down) or radio buttons. Slider input is really for numeric values, and trying to fit character labels is not the way it is designed to work. – Gopala Commented Feb 1, 2016 at 2:04
- I understand selectInput is another option, but prefer to use sliderInput in this case. IonRangeSlider, shiny used, can show character in slider. So I think we can do the same thing in shiny, but just not sure how to implement it (May need to write some js script). – Bangyou Commented Feb 1, 2016 at 2:08
-
2
See my answer here: stackoverflow./questions/30502870/… . Basically change the
.update({'values':vals})
to the array of values you want. I just tried changingvals
to['one', 'two', 'three', 'four' ,'five']
and it worked. – DeanAttali Commented Feb 1, 2016 at 7:48
2 Answers
Reset to default 4This can be easily done using sliderTextInput function in shiny. No need to add all this plex js function. Just a few lines of code will do the trick.Install the shinywidgets package which contains the sliderTextInput function. Do the following :
sliderTextInput("foo","Animation Duration" ,
choices = c("Label 1", "Label 3", "Label 3"),
selected = c("Label 1", "Label 3", "Label 3"), #incase you want all values by default
animate = FALSE, grid = FALSE,
hide_min_max = FALSE, from_fixed = FALSE,
to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
post = NULL, dragRange = TRUE)
Thanks @daattli for pointing me the right direction and letting me know how to use js
to change the shiny element.
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
.
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)
本文标签: javascriptSet sliderInput values as characters in shinyStack Overflow
版权声明:本文标题:javascript - Set sliderInput values as characters in shiny - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268203a2443806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论