I'm trying to create a categorical (as opposed to numeric) slider in shiny using sliderInput
. Thanks to Dean Atali's answer here (Shiny slider on logarithmic scale), I'm able to create the slider.
However, I need to create the slider in server
and pass it to the UI via renderUI
and uiOutput
. But, when I move the sliderInput
into a renderUI
call on the server-side, it no longer works. Here are two examples: the first one showing how the categorical slider works (when not using renderUI
/uiOutput
), and the second one showing how the categorical slider does not work (when using renderUI
/uiOutput
).
WORKING EXAMPLE (slider created in the UI)
library(shiny)
JScode <-
"$(function() {
setTimeout(function(){
var names = ['Unrated', 'Emerging', ' ', 'Formative', ' ', ' ', 'Developed', ' '];
var vals = [];
for (i = 0; i < names.length; i++) {
var val = names[i];
vals.push(val);
}
$('#pvalue').data('ionRangeSlider').update({'values':vals})
}, 7)})"
runApp(shinyApp(
ui = fluidPage(
tags$head(tags$script(HTML(JScode))),
textOutput('texty'),
sliderInput("pvalue",
"PValue:",
min = 0,
max = 7,
value = 0
)
),
server = function(input, output, session) {
output$texty <- renderText({
input$pvalue
})
}
))
Non-working example (slider created in server
)
library(shiny)
JScode <-
"$(function() {
setTimeout(function(){
var names = ['Unrated', 'Emerging', ' ', 'Formative', ' ', ' ', 'Developed', ' '];
var vals = [];
for (i = 0; i < names.length; i++) {
var val = names[i];
vals.push(val);
}
$('#pvalue').data('ionRangeSlider').update({'values':vals})
}, 7)})"
runApp(shinyApp(
ui = fluidPage(
tags$head(tags$script(HTML(JScode))),
textOutput('texty'),
uiOutput('uu')
),
server = function(input, output, session) {
output$texty <- renderText({
input$pvalue
})
output$uu <- renderUI({
sliderInput("pvalue",
"PValue:",
min = 0,
max = 7,
value = 0
)
})
}
))
How do I make the slider show the categories (rather than numbers) when the slider is generated in server
?